diff --git a/pe26/pe26.go b/pe26/pe26.go index a1f97e4..b93b273 100644 --- a/pe26/pe26.go +++ b/pe26/pe26.go @@ -1,82 +1,81 @@ package pe26 import ( - "fmt" - "strconv" + "fmt" + "strconv" ) - func max(arr []int) int { - result := 0 - for v := range arr { - if v > result { - result = v - } - } - return result + result := 0 + for v := range arr { + if v > result { + result = v + } + } + return result } func get_next_digit(numerator, denominator int) int { - if numerator < denominator { - numerator *= 10 - } - remainder := numerator % denominator - return remainder + if numerator < denominator { + numerator *= 10 + } + remainder := numerator % denominator + return remainder } func calc_digit_repeat(i int, c chan int) { - digit_length := map[int]int { - 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, - 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, - } - rem := 1 - for { - rem = get_next_digit(rem, i) - if rem == 0 { - // no repeats - c <- 0 - break - } - if digit_length[rem] > 0 { - // found a repeat - c <- digit_length[rem] - break - } else { - // new digit - // increment other non-zero digits - for k, v := range digit_length { - if v > 0 { - digit_length[k]++ - } - } - digit_length[rem]++ - } - } + digit_length := map[int]int{ + 0: 0, 1: 0, 2: 0, 3: 0, 4: 0, + 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, + } + rem := 1 + for { + rem = get_next_digit(rem, i) + if rem == 0 { + // no repeats + c <- 0 + break + } + if digit_length[rem] > 0 { + // found a repeat + c <- digit_length[rem] + break + } else { + // new digit + // increment other non-zero digits + for k, v := range digit_length { + if v > 0 { + digit_length[k]++ + } + } + digit_length[rem]++ + } + } } -func start_calc_digit_repeat(i int) (chan int) { - c := make(chan int) +func start_calc_digit_repeat(i int) chan int { + c := make(chan int) - go calc_digit_repeat(i, c) + go calc_digit_repeat(i, c) - return c + return c } func Solve(args []string) { - N, _ := strconv.Atoi(args[0]) + N, _ := strconv.Atoi(args[0]) - chan_list := make([]chan int, N) + chan_list := make([]chan int, N) - for i := 1; i <= N; i++ { - chan_list[i-1] = start_calc_digit_repeat(i) - } + for i := 1; i <= N; i++ { + chan_list[i-1] = start_calc_digit_repeat(i) + } - max, max_idx := 0, 1 - for i := 0; i < N; i++ { - val := <-chan_list[i] - if val > max { - max = val - max_idx = i + 1 - } - } - fmt.Println(max_idx) + max, max_idx := 0, 1 + for i := 0; i < N; i++ { + val := <-chan_list[i] + if val > max { + max = val + max_idx = i + 1 + } + } + fmt.Println(max_idx) } diff --git a/pe27/pe27.go b/pe27/pe27.go new file mode 100644 index 0000000..f665b7b --- /dev/null +++ b/pe27/pe27.go @@ -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)) + } + */ +} diff --git a/projecteuler.go b/projecteuler.go index 2f80426..7fefc37 100644 --- a/projecteuler.go +++ b/projecteuler.go @@ -4,21 +4,26 @@ import ( "fmt" "os" - "shillerben.com/gitlab/shillerben/projecteuler/pe26" + "shillerben.com/gitlab/shillerben/projecteuler-go/pe26" + "shillerben.com/gitlab/shillerben/projecteuler-go/pe27" ) var solve_funcs = map[string]func([]string){ "26": pe26.Solve, + "27": pe27.Solve, } func main() { - if len(os.Args) < 3 { + if len(os.Args) < 2 { fmt.Println("usage: projecteuler ") return } problem_number := os.Args[1] - problem_args := os.Args[2:] + var problem_args []string + if len(os.Args) > 2 { + problem_args = os.Args[2:] + } solve_funcs[problem_number](problem_args) }