Solved problem 27

This commit is contained in:
Ben Shiller 2021-06-05 15:48:09 -05:00
parent 28d65ed7c0
commit 53a5b94525
No known key found for this signature in database
GPG Key ID: DC46F01400846797
3 changed files with 136 additions and 63 deletions

View File

@ -1,82 +1,81 @@
package pe26 package pe26
import ( import (
"fmt" "fmt"
"strconv" "strconv"
) )
func max(arr []int) int { func max(arr []int) int {
result := 0 result := 0
for v := range arr { for v := range arr {
if v > result { if v > result {
result = v result = v
} }
} }
return result return result
} }
func get_next_digit(numerator, denominator int) int { func get_next_digit(numerator, denominator int) int {
if numerator < denominator { if numerator < denominator {
numerator *= 10 numerator *= 10
} }
remainder := numerator % denominator remainder := numerator % denominator
return remainder return remainder
} }
func calc_digit_repeat(i int, c chan int) { func calc_digit_repeat(i int, c chan int) {
digit_length := map[int]int { digit_length := map[int]int{
0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 0: 0, 1: 0, 2: 0, 3: 0, 4: 0,
5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0,
} }
rem := 1 rem := 1
for { for {
rem = get_next_digit(rem, i) rem = get_next_digit(rem, i)
if rem == 0 { if rem == 0 {
// no repeats // no repeats
c <- 0 c <- 0
break break
} }
if digit_length[rem] > 0 { if digit_length[rem] > 0 {
// found a repeat // found a repeat
c <- digit_length[rem] c <- digit_length[rem]
break break
} else { } else {
// new digit // new digit
// increment other non-zero digits // increment other non-zero digits
for k, v := range digit_length { for k, v := range digit_length {
if v > 0 { if v > 0 {
digit_length[k]++ digit_length[k]++
} }
} }
digit_length[rem]++ digit_length[rem]++
} }
} }
} }
func start_calc_digit_repeat(i int) (chan int) { func start_calc_digit_repeat(i int) chan int {
c := make(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) { 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++ { for i := 1; i <= N; i++ {
chan_list[i-1] = start_calc_digit_repeat(i) chan_list[i-1] = start_calc_digit_repeat(i)
} }
max, max_idx := 0, 1 max, max_idx := 0, 1
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
val := <-chan_list[i] val := <-chan_list[i]
if val > max { if val > max {
max = val max = val
max_idx = i + 1 max_idx = i + 1
} }
} }
fmt.Println(max_idx) fmt.Println(max_idx)
} }

69
pe27/pe27.go Normal file
View 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))
}
*/
}

View File

@ -4,21 +4,26 @@ import (
"fmt" "fmt"
"os" "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){ var solve_funcs = map[string]func([]string){
"26": pe26.Solve, "26": pe26.Solve,
"27": pe27.Solve,
} }
func main() { func main() {
if len(os.Args) < 3 { if len(os.Args) < 2 {
fmt.Println("usage: projecteuler <problem number> <problem args>") fmt.Println("usage: projecteuler <problem number> <problem args>")
return return
} }
problem_number := os.Args[1] 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) solve_funcs[problem_number](problem_args)
} }