diff --git a/46_goldbachs_other_conjecture/main.go b/46_goldbachs_other_conjecture/main.go new file mode 100644 index 0000000..bc91df2 --- /dev/null +++ b/46_goldbachs_other_conjecture/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "math" +) + +func main() { + primeList := generatePrimeList(10000) + result := 1 + notFound := true + for notFound { + result += 2 + j := 0 + notFound = false + for result >= primeList[j] { + if isTwiceSquare(int64(result - primeList[j])) { + notFound = true + break + } + j++ + } + } + fmt.Println("result:", result) +} + +func generatePrimeList(n int) []int { + var ret []int + for i := 1; i < n; i++ { + if isPrime(i) { + ret = append(ret, i) + } + } + return ret +} + +func isTwiceSquare(number int64) bool { + x := math.Sqrt(float64(number / 2)) + return x == float64(int64(x)) +} + +func isPrime(n int) bool { + res := int(math.Sqrt(float64(n))) + divider := 2 + for divider <= res { + if n%divider == 0 { + return false + } + divider++ + } + return true +} diff --git a/46_goldbachs_other_conjecture/main_test.go b/46_goldbachs_other_conjecture/main_test.go new file mode 100644 index 0000000..06ac154 --- /dev/null +++ b/46_goldbachs_other_conjecture/main_test.go @@ -0,0 +1,59 @@ +package main + +import ( + "reflect" + "testing" +) + +func Test_isTwiceSquare(t *testing.T) { + type args struct { + number int64 + } + tests := []struct { + name string + args args + want bool + }{ + { + "8", + args{8}, + true, + }, + { + "10", + args{10}, + false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := isTwiceSquare(tt.args.number); got != tt.want { + t.Errorf("isTwiceSquare() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_generatePrimeList(t *testing.T) { + type args struct { + n int + } + tests := []struct { + name string + args args + want []int + }{ + { + "10", + args{10}, + []int{2, 3, 5, 7}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := generatePrimeList(tt.args.n); !reflect.DeepEqual(got, tt.want) { + t.Errorf("generatePrimeList() = %v, want %v", got, tt.want) + } + }) + } +}