# jmoiron/euler.go

some cleanups for old solutions, problems 32 & 34

1 parent 52154d0 commit 16aa96114e1c2646cd71599cb42ed6eb3c7d5fdf committed Apr 14, 2012
Showing with 80 additions and 7 deletions.
1. +1 −2 001.go
2. +4 −5 027.go
3. +34 −0 032.go
4. +41 −0 034.go
3 001.go
 @@ -4,11 +4,10 @@ import "fmt" func main() { sum := 0 - for i:=0; i<1000; i++ { + for i := 0; i < 1000; i++ { if (i%3 == 0) || (i%5 == 0) { sum += i } } fmt.Print(sum, "\n") } -
9 027.go
 @@ -20,9 +20,9 @@ func sieve(ch chan int, max int) { func init() { // create a map of primes up to a value; 1000^2 is 1000000, - // but it seems unlikely we'll reach primes that high.. + // but it's unlikely we'll reach very high values for n in := make(chan int) - go sieve(in, 100000) + go sieve(in, 5000) for next := range in { primes[next] = 1 } @@ -56,16 +56,15 @@ func main() { maxlen := 0 maxa, maxb := 0, 0 - for a:=-999 ; a < 1000; a++ { + for a:=-999 ; a < 1000; a+=2 { for _,b := range bvals { + if a > b { continue } ql := quad_length(a, b) if ql > maxlen { - fmt.Printf("New max len: n^2 + %dn + %d = %d primes\n", a, b, ql) maxlen = ql maxa, maxb = a, b } } } - fmt.Printf("Max: n^2 + %dn + %d = %d primes\n", maxa, maxb, maxlen) fmt.Printf("%d x %d = %d\n", maxa, maxb, maxa*maxb) }
34 032.go
 @@ -0,0 +1,34 @@ +package main + +import "fmt" + +func is_9_pandigital(a, b, c int) bool { + full := fmt.Sprintf("%d%d%d", a, b, c) + if len(full) != 9 { + return false + } + set := map[rune]int {} + for _,r := range full { + if r == '0' || set[r] > 0 { + return false + } + set[r] = 1 + } + return true +} + +func main() { + panprods := map[int]int{} + for i:=2; i<99; i++ { + for j:=i; j<9999/i; j++ { + if is_9_pandigital(i, j, i*j) { + panprods[i*j]++ + } + } + } + sum := 0 + for k,_ := range panprods { + sum += k + } + println(sum) +}
41 034.go
 @@ -0,0 +1,41 @@ +package main + +import "fmt" + +var digit_fact = map[rune]int {} + +//'0':1, '1':1, '2':2, '3':6, '4':24, '5':120, +//'6': 720, '7': 5040, '8':40320, '9':362880} + +func init() { + digit_fact['0'] = 1 + digit_fact['1'] = 1 + digit_fact['2'] = 2 + fact := 2 + for i:=3; i<10; i++ { + digit_fact[rune(i+48)] = i * fact + fact = i * fact + } +} + +func factsum(n int) int { + str := fmt.Sprintf("%d", n) + sum := 0 + for _,r := range str { + sum += digit_fact[r] + } + return sum +} + +func main() { + /* 326880 * 7 is a simple upper bound; better are available */ + sum := 0 + for i:=13; i < 7*362880; i++ { + fs := factsum(i) + if fs == i { + println(i, fs) + sum += i + } + } + println("Sum:", sum) +}