27: quadratic primes

This commit is contained in:
VicRen
2020-10-27 09:51:19 +08:00
parent 0b3b448000
commit c4ebf2571a
2 changed files with 112 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
package main
import "fmt"
func main() {
maxN := 0
maxA := 0
maxB := 0
cm := 0
for a := -999; a < 1000; a += 2 {
for b := 3; b < 1000; b += 2 {
fmt.Println("a:", a, "b:", b)
cm = findMaxN(a, b)
if cm > maxN {
maxN = cm
maxA = a
maxB = b
}
}
}
fmt.Println("product is:", maxA*maxB)
}
func findMaxN(a, b int) int {
max := 0
n := 2
for isPrime(n*n + a*n + b) {
if n > max {
max = n
}
n++
}
return max
}
func isPrime(n int) bool {
if n <= 1 {
return false
}
divider := 2
for divider < n {
if n%divider == 0 {
return false
}
divider++
}
return true
}

View File

@@ -0,0 +1,62 @@
package main
import "testing"
func Test_isPrime(t *testing.T) {
type args struct {
n int
}
tt := []struct {
name string
args args
want bool
}{
{
"1",
args{1},
false,
},
{
"2",
args{2},
true,
},
{
"4",
args{4},
false,
},
{
"17",
args{17},
true,
},
{
"37",
args{37},
true,
},
{
"73",
args{73},
true,
},
{
"13 * 3 * 5 * 5 * 17 * 31 * 737",
args{3 * 3 * 5 * 5 * 17 * 31 * 73},
false,
},
{
"large_one",
args{2876*2876 - 63*2876 + 977},
false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if got := isPrime(tc.args.n); got != tc.want {
t.Errorf("isPrime() = %v, want %v", got, tc.want)
}
})
}
}