Last active
October 13, 2015 23:08
-
-
Save Koitaro/4270167 to your computer and use it in GitHub Desktop.
Go/goroutine: Project Euler 20-29
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
func init() { | |
runtime.GOMAXPROCS(8) | |
} | |
type nums []int | |
func (xs nums) member(n int) bool { | |
for _, v := range xs { | |
if n == v { | |
return true | |
} | |
} | |
return false | |
} | |
func cycle(n int, ch chan int) { | |
xs := nums{} | |
for x := 10 % n; !xs.member(x); x = 10 * x % n { | |
xs = append(xs, x) | |
} | |
ch <- len(xs) | |
} | |
type queue struct { | |
ch chan int | |
length int | |
} | |
func newQueue() queue { | |
ch, ps := make(chan int, 1000), 0 | |
for n := 2; n < 1000; n++ { | |
go cycle(n, ch) | |
ps++ | |
} | |
return queue{ch, ps} | |
} | |
func problem26() { | |
max := 0 | |
for q := newQueue(); q.length > 0; q.length-- { | |
if n := <-q.ch; n > max { | |
max = n | |
} | |
} | |
fmt.Println(max) | |
} | |
func main() { | |
start := time.Now() | |
problem26() | |
fmt.Println(time.Now().Sub(start).Seconds()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"runtime" | |
"time" | |
) | |
func init() { | |
runtime.GOMAXPROCS(1000) | |
} | |
func isPrime(n int) bool { | |
switch { | |
case n < 2: | |
return false | |
case n == 2: | |
return true | |
case n%2 == 0: | |
return false | |
} | |
for p := 3; p*p <= n; p += 2 { | |
if n%p == 0 { | |
return false | |
} | |
} | |
return true | |
} | |
type pair struct { | |
a, b, length int | |
} | |
func (x pair) mul() int { | |
return x.a * x.b | |
} | |
func primeLength(a int, ch chan pair) { | |
answer := pair{0, 0, 0} | |
for b := 3; b < 1000; b += 2 { | |
length := 0 | |
for n := 0; isPrime(n*n + a*n + b); n++ { | |
length++ | |
} | |
if length > answer.length { | |
answer = pair{a, b, length} | |
} | |
} | |
ch <- answer | |
} | |
type queue struct { | |
ch chan pair | |
length int | |
} | |
func newQueue() queue { | |
ch, ps := make(chan pair, 1000), 0 | |
for a := -999; a < 1000; a += 2 { | |
go primeLength(a, ch) | |
ps++ | |
} | |
return queue{ch, ps} | |
} | |
func problem27() { | |
answer := pair{0, 0, 0} | |
for q := newQueue(); q.length > 0; q.length-- { | |
if tmp := <-q.ch; tmp.length > answer.length { | |
answer = tmp | |
} | |
} | |
fmt.Println(answer.mul()) | |
} | |
func main() { | |
start := time.Now() | |
problem27() | |
fmt.Println(time.Now().Sub(start).Seconds()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment