-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (97 loc) · 1.87 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"fmt"
"math"
"time"
)
func CreateSpiral(size int) [][]int {
values := make([][]int, 0, size)
for i := 0; i < size; i++ {
values = append(values, make([]int, size))
}
i, j := size-1, size-1
iDelta, jDelta := 0, -1
for n := size * size; n > 0; n-- {
values[i][j] = n
if i+iDelta < 0 || i+iDelta >= size ||
j+jDelta < 0 || j+jDelta >= size || values[i+iDelta][j+jDelta] != 0 {
if iDelta == 1 || iDelta == -1 {
iDelta *= -1
}
iDelta, jDelta = jDelta, iDelta
}
i += iDelta
j += jDelta
}
return values
}
func getSpiralPrimeRatio(spiral [][]int) float64 {
size := len(spiral)
primeCount := 0
iMainDiag := 0
iAntiDiag := size - 1
for j := 0; j < size; j++ {
if iMainDiag != iAntiDiag {
if isPrime(spiral[iMainDiag][j]) {
primeCount++
}
if isPrime(spiral[iAntiDiag][j]) {
primeCount++
}
} else {
if isPrime(spiral[iMainDiag][j]) {
primeCount++
}
}
iMainDiag += 1
iAntiDiag -= 1
}
return float64(primeCount) / (2*float64(size) - 1)
}
func isPrime(x int) bool {
if x < -1 {
x *= -1
}
root := math.Sqrt(float64(x))
if x == 0 || x == 1 {
return false
}
for i := 2; i <= int(root); i++ {
if x%i == 0 {
return false
}
}
return true
}
func main() {
start := time.Now()
n := 5
primeCount := 3
for {
e4 := n * n
e3 := e4 - (n - 1)
e2 := e3 - (n - 1)
e1 := e2 - (n - 1)
if isPrime(e4) {
primeCount++
}
if isPrime(e3) {
primeCount++
}
if isPrime(e2) {
primeCount++
}
if isPrime(e1) {
primeCount++
}
spiralPrimeRatio := float64(primeCount) / (2*float64(n) - 1)
fmt.Printf("n=%d, prime ratio=%f\n", n, spiralPrimeRatio)
if spiralPrimeRatio < 0.1 {
break
}
n += 2
}
elapsed := time.Since(start)
fmt.Printf("side length of the square spiral for which the ratio of primes along both diagonals first falls below = %d (elapsed : %v)\n",
n, elapsed)
}