-
Notifications
You must be signed in to change notification settings - Fork 0
/
87.go
63 lines (57 loc) · 1.18 KB
/
87.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
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"math"
)
func main() {
end := int(math.Ceil(math.Sqrt(50000000)))
composites := make([]bool, end)
for i := 2; i < end; i++ {
if composites[i] {
continue
}
for j := 2 * i; j < end; j += i {
composites[j] = true
}
}
var prime2, prime3, prime4 []int
// Look at potential x^2
for i := 2; i < end; i++ {
if !composites[i] {
prime2 = append(prime2, i)
}
}
// Look at potential x^3
end = int(math.Ceil(math.Pow(50000000, 1.0/3)))
for _, value := range prime2 {
if value > end {
break
}
prime3 = append(prime3, value)
}
// Look at potential x^4
end = int(math.Ceil(math.Pow(50000000, 1.0/4)))
for _, value := range prime3 {
if value > end {
break
}
prime4 = append(prime4, value)
}
// Loop through prime4 first, then prime3, then prime2. Once we exceed 50M,
// break.
data := make(map[int]bool)
for _, value4 := range prime4 {
for _, value3 := range prime3 {
for _, value2 := range prime2 {
product := value4 * value4 * value4 * value4
product += value3 * value3 * value3
product += value2 * value2
if product > 50000000 {
break
}
data[product] = true
}
}
}
fmt.Println(len(data))
}