-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem27.go
More file actions
83 lines (78 loc) · 1.76 KB
/
Copy pathproblem27.go
File metadata and controls
83 lines (78 loc) · 1.76 KB
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
package projecteuler
import (
"math"
"sync"
)
// holds coefficients (a, b) of quadratic equation
// of form ( n^2 + a*n + b ), and max number of primes
// generated from that equation for continuous values of
// `n`, starting at 0
type coefficient struct {
a int
b int
primeC int
}
// checks whether a given number is prime or not
func isPrime(n int) bool {
if n < 2 {
return false
}
target := int(math.Sqrt(float64(n)))
check := true
for i := 2; i <= target; i++ {
if n%i == 0 {
check = false
break
}
}
return check
}
// computes maximum number of primes generated from quadratic equation
// of given form, for continuous values of `n`, starting at 0
//
// passes value over channel, to listener go routine
func getPrimeCountFromQuadraticEq(a int, b int, channel chan coefficient) {
compute := func(n int) int {
return n*n + a*n + b
}
n := 0
for {
if !isPrime(compute(n)) {
break
}
n++
}
channel <- coefficient{a, b, n}
}
// QuadraticPrimes - Computes product of coefficients of quadratic
// expression, that produces maximum number of primes
// for continuous values of `n`, starting at 0
func QuadraticPrimes() int {
coeff := coefficient{}
channel := make(chan coefficient, 1) // communication channel with single value bufferring
var wg sync.WaitGroup
wg.Add(1)
// listener routine
go func() {
c := 0
totalC := 1999 * 2001
for i := range channel {
c++
if i.primeC > coeff.primeC {
coeff = i
}
if c == totalC {
break
}
}
close(channel)
wg.Done()
}()
for a := -999; a < 1000; a++ {
for b := -1000; b <= 1000; b++ {
go getPrimeCountFromQuadraticEq(a, b, channel) // spawning go routines for faster computation, leveraging power of multiple cores of CPU
}
}
wg.Wait()
return coeff.a * coeff.b
}