-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path003.go
54 lines (42 loc) · 791 Bytes
/
003.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
Project Euler
Problem 3 - Largest Prime Factor
Alex Wormuth (awormuth)
10/15/2017
Answer submitted: 6857
*/
package main
import (
"fmt"
"math"
)
// isPrime returns true if given integer, n, is prime. Otherwise, false.
func isPrime(n int) bool {
if n <= 1 {
return false
} else if n <= 3 {
return true
} else if n % 2 == 0 || n % 3 == 0 {
return false
}
i := 5
for i * i <= n {
if n % i == 0 || n % (i + 2) == 0 {
return false
}
i += 6
}
return true
}
func main() {
largestPrimeFactor := 1
num := math.Sqrt(600851475143)
for i := 1; i < int(num); i++ {
if isPrime(i) {
if 600851475143 % i == 0 {
largestPrimeFactor = i
}
}
}
fmt.Println(largestPrimeFactor)
}