-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpb12.go
53 lines (49 loc) · 1 KB
/
pb12.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
package main
import (
"fmt"
"math"
)
func removeDuplicates(a []int) []int {
result := []int{}
seen := map[int]int{}
for _, val := range a {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = val
}
}
return result
}
func findDivisors(num int) []int{
divisors := []int{}
// Speed optimization: Find all factors until root
for i:=1;i<=int(math.Sqrt(float64(num)))+1;i++{
if num%i==0{
divisors = append(divisors, i)
}
}
// Then calculate complimentary factors and add them.
hdiv := []int{}
for _,v := range divisors{
if (num/v) != v{
hdiv = append(hdiv, (num/v))
}
}
for _,v := range hdiv{
divisors = append(divisors, v)
}
// And clean up so we only have unique factors.
return removeDuplicates(divisors)
}
func main(){
lim = 500
triangle_num := 0
for i:=1;;i++{
triangle_num += i
divisors := findDivisors(triangle_num)
if len(divisors) >= lim{
fmt.Println(triangle_num)
break
}
}
}