Solved problem 26

This commit is contained in:
Ben Shiller 2021-06-05 00:05:13 -05:00
parent f704a73e74
commit 2e617ff708
No known key found for this signature in database
GPG Key ID: DC46F01400846797
4 changed files with 107 additions and 11 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
projecteuler

View File

@ -1,11 +0,0 @@
package pe26
import (
"fmt"
)
func solve() {
for i := 1; i <= N; i++ {
}
}

82
pe26/pe26.go Normal file
View File

@ -0,0 +1,82 @@
package pe26
import (
"fmt"
"strconv"
)
func max(arr []int) int {
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
}
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]++
}
}
}
func start_calc_digit_repeat(i int) (chan int) {
c := make(chan int)
go calc_digit_repeat(i, c)
return c
}
func Solve(args []string) {
N, _ := strconv.Atoi(args[0])
chan_list := make([]chan int, N)
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)
}

24
projecteuler.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"fmt"
"os"
"shillerben.com/gitlab/shillerben/projecteuler/pe26"
)
var solve_funcs = map[string]func([]string){
"26": pe26.Solve,
}
func main() {
if len(os.Args) < 3 {
fmt.Println("usage: projecteuler <problem number> <problem args>")
return
}
problem_number := os.Args[1]
problem_args := os.Args[2:]
solve_funcs[problem_number](problem_args)
}