-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem58.go
51 lines (47 loc) · 1.49 KB
/
problem58.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
package projecteuler
// given prime number count & total number count,
// we need to compute precentage ratio of
// prime numbers to all numbers, as integer
func primeToAllRatio(primeC int, allC int) int {
return primeC * 100 / allC
}
// given a slice of numbers, we'll find out number of primes
func getPrimeCount(buffer []int) int {
c := 0
for _, v := range buffer {
if isPrime(v) {
c++
}
}
return c
}
// we're asked to generate only diagonal elements
// of a clockwise rotating spiral, where last element generated
// in previous iteration of spiral is given, along with current side length of spiral
// side length of spiral is same as width of square matrix, currently under consideration
// returns four elements generated in this run, present on four corners i.e. on diagonals
// of square matrix
func generateDiagonalElementsOfClockWiseSpiral(lastV *int, sideL int, initSideL int) []int {
buffer := make([]int, 4)
incrBy := sideL - initSideL
for i := 0; i < 4; i++ {
*lastV += incrBy
buffer[i] = *lastV
}
return buffer
}
// SpiralPrimes - Finds out at which side length value of clockwise spiral
// we'll get a prime ratio of value < 10%
// avoid prime checking recomputation, by precaching count of already
// checked primes
func SpiralPrimes() int {
lastV, sideL, primeC, totalC := 1, 3, 0, 1
for ; ; sideL += 2 {
primeC += getPrimeCount(generateDiagonalElementsOfClockWiseSpiral(&lastV, sideL, 1))
totalC += 4
if primeToAllRatio(primeC, totalC) < 10 {
break
}
}
return sideL
}