-
Notifications
You must be signed in to change notification settings - Fork 0
/
p27.go
84 lines (68 loc) · 1.27 KB
/
p27.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"math"
"time"
)
func checkPrime(prime []int, x int) bool {
if x < 2 {
return false
}
for _, p := range prime {
if p > int(math.Sqrt(float64(x))) {
return true
} else if x%p == 0 {
return false
}
}
return true
}
func main() {
start := time.Now()
prime, maxP := []int{2, 3, 5, 7, 11, 13}, 13
for maxP < 1000 {
maxP += 2
if checkPrime(prime, maxP) {
prime = append(prime, maxP)
}
}
listB := make([]int, len(prime)*2)
for i, p := range prime {
listB[2*i] = p
listB[2*i+1] = p * -1
}
maxMul, maxN := 0, 0
for _, b := range listB {
for a := -999; a < 1000; a += 2 {
for maxP < int(math.Sqrt(float64(maxN*(maxN+a)+b))) {
maxP += 2
if checkPrime(prime, maxP) {
prime = append(prime, maxP)
}
}
if !checkPrime(prime, maxN*(maxN+a)+b) {
continue
}
isPrime := true
n := 1
for ; isPrime; n++ {
curr := n*(n+a) + b
for maxP < int(math.Sqrt(float64(curr))) {
maxP += 2
if checkPrime(prime, maxP) {
prime = append(prime, maxP)
}
}
isPrime = checkPrime(prime, curr)
}
if n > maxN {
maxMul = a * b
maxN = n
fmt.Println(a, b, n)
}
}
}
fmt.Println(maxMul)
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}