Solved problem 27
This commit is contained in:
69
pe27/pe27.go
Normal file
69
pe27/pe27.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package pe27
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
type Params struct {
|
||||
A int
|
||||
B int
|
||||
N int
|
||||
}
|
||||
|
||||
func is_prime(x int) bool {
|
||||
if x < 0 {
|
||||
x = -x
|
||||
}
|
||||
if x == 0 || x == 1 {
|
||||
return false
|
||||
}
|
||||
if x == 2 {
|
||||
return true
|
||||
}
|
||||
for i := 2; float64(i) <= math.Sqrt(float64(x)); i++ {
|
||||
if x%i == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func count_primes(a, b int, c chan Params) {
|
||||
num_primes := 0
|
||||
for n := 0; ; n++ {
|
||||
ans := n*n + a*n + b
|
||||
if !is_prime(ans) {
|
||||
break
|
||||
} else {
|
||||
num_primes++
|
||||
}
|
||||
}
|
||||
c <- Params{a, b, num_primes}
|
||||
}
|
||||
|
||||
func Solve(_ []string) {
|
||||
c := make(chan Params, 3999999)
|
||||
var a, b int
|
||||
for a = -999; a < 1000; a++ {
|
||||
for b = -1000; b < 1001; b++ {
|
||||
go count_primes(a, b, c)
|
||||
}
|
||||
}
|
||||
|
||||
max_a, max_b, max := 0, 0, 0
|
||||
for i := 0; i < 3999999; i++ {
|
||||
arr := <-c
|
||||
if arr.N > max {
|
||||
max_a = arr.A
|
||||
max_b = arr.B
|
||||
max = arr.N
|
||||
}
|
||||
}
|
||||
fmt.Println("A:", max_a, "B:", max_b, "Num Primes:", max, "A * B:", max_a*max_b)
|
||||
/*
|
||||
for i := 0; i < 100; i++ {
|
||||
fmt.Println("i:", i, "is prime:", is_prime(i))
|
||||
}
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user