-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem3.go
More file actions
44 lines (41 loc) · 1.2 KB
/
Copy pathproblem3.go
File metadata and controls
44 lines (41 loc) · 1.2 KB
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
package projecteuler
import "math"
// GeneratePrimesUnderX - Generates all prime numbers under one given `X`,
// and returns a slice of them
//
// using dynamic programming strategy, to perform lesser number of checkings
func GeneratePrimesUnderX(x int) []int {
primeArr := []int{2}
for i := 3; i <= x; i++ {
sqrt := int(math.Sqrt(float64(i)))
check := true
for j := 0; j < len(primeArr) && primeArr[j] <= sqrt; j++ {
if i%primeArr[j] == 0 {
check = false
break
}
}
if check {
primeArr = append(primeArr, i)
}
}
return primeArr
}
// GetLargestPrimeFactor - First calculates square root of given number,
// and find out all primes which are under or equals to that sqrt value
//
// Now we'll simply iterate over that prime holder slice, from last to first,
// i.e. from higher value prime to lower value prime, cause finally, we need
// to find out maximum prime factor of `num`. That'll allow us to perform lesser
// number of checkings.
func GetLargestPrimeFactor(num int) int {
largest := 0
primeArr := GeneratePrimesUnderX(int(math.Sqrt(float64(num))))
for i := len(primeArr) - 1; i >= 0; i-- {
if num%primeArr[i] == 0 {
largest = primeArr[i]
break
}
}
return largest
}