Last active
October 13, 2015 09:57
-
-
Save Koitaro/4178133 to your computer and use it in GitHub Desktop.
Go/goroutine: Project Euler 1-9
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" | |
"time" | |
) | |
func sum(n int) (out chan int) { | |
out = make(chan int) | |
go func() { | |
answer := 0 | |
for i := n; i < 1000; i += n { | |
answer += i | |
} | |
out <- answer | |
}() | |
return | |
} | |
func problem1() { | |
x, y, z := sum(3), sum(5), sum(15) | |
fmt.Println(<-x + <-y - <-z) | |
} | |
func main() { | |
start := time.Now() | |
problem1() | |
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" | |
"time" | |
) | |
const buf = 50 | |
type chanInt chan int | |
func fib() (out chanInt) { | |
out = make(chanInt, buf) | |
go func() { | |
a, b := 1, 2 | |
for a < 4000000 { | |
out <- a | |
a, b = b, a+b | |
} | |
close(out) | |
}() | |
return | |
} | |
func (in chanInt) even() (out chanInt) { | |
out = make(chanInt, buf) | |
go func() { | |
for n := range in { | |
if n%2 == 0 { | |
out <- n | |
} | |
} | |
close(out) | |
}() | |
return | |
} | |
func (in chanInt) sum() (out chanInt) { | |
out = make(chanInt) | |
go func() { | |
m := 0 | |
for n := range in { | |
m += n | |
} | |
out <- m | |
}() | |
return | |
} | |
func problem2() { | |
fmt.Println(<-fib().even().sum()) | |
} | |
func main() { | |
start := time.Now() | |
problem2() | |
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" | |
"time" | |
) | |
type chanInt64 chan int64 | |
func facts(n int64) (out chanInt64) { | |
out = make(chanInt64) | |
go func() { | |
p := int64(2) | |
for n%p == 0 { | |
out <- p | |
n /= p | |
} | |
p = 3 | |
for { | |
if p*p > n { | |
break | |
} | |
for n%p == 0 { | |
out <- p | |
n /= p | |
} | |
p += 2 | |
} | |
if n > 1 { | |
out <- n | |
} | |
close(out) | |
}() | |
return | |
} | |
func (in chanInt64) max() (out chanInt64) { | |
out = make(chanInt64) | |
go func() { | |
max := int64(0) | |
for n := range in { | |
if n > max { | |
max = n | |
} | |
} | |
out <- max | |
}() | |
return | |
} | |
func problem3() { | |
fmt.Println(<-facts(600851475143).max()) | |
} | |
func main() { | |
start := time.Now() | |
problem3() | |
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" | |
"time" | |
) | |
func primes() (ch chan int) { | |
ch = make(chan int) | |
go func() { | |
ch <- 2 | |
ch <- 3 | |
ps := []int{3} | |
n := 5 | |
for { | |
for _, p := range ps { | |
if p*p > n { | |
ps = append(ps, n) | |
ch <- n | |
break | |
} | |
if n%p == 0 { | |
break | |
} | |
} | |
n += 2 | |
} | |
}() | |
return | |
} | |
func problem7() { | |
ch := primes() | |
for n := 1; n < 10001; n++ { | |
<-ch | |
} | |
fmt.Println(<-ch) | |
} | |
func main() { | |
start := time.Now() | |
problem7() | |
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" | |
"strconv" | |
"strings" | |
"time" | |
) | |
func init() { | |
runtime.GOMAXPROCS(8) | |
} | |
const ( | |
str = "73167176531330624919225119674426574742355349194934" + | |
"96983520312774506326239578318016984801869478851843" + | |
"85861560789112949495459501737958331952853208805511" + | |
"12540698747158523863050715693290963295227443043557" + | |
"66896648950445244523161731856403098711121722383113" + | |
"62229893423380308135336276614282806444486645238749" + | |
"30358907296290491560440772390713810515859307960866" + | |
"70172427121883998797908792274921901699720888093776" + | |
"65727333001053367881220235421809751254540594752243" + | |
"52584907711670556013604839586446706324415722155397" + | |
"53697817977846174064955149290862569321978468622482" + | |
"83972241375657056057490261407972968652414535100474" + | |
"82166370484403199890008895243450658541227588666881" + | |
"16427171479924442928230863465674813919123162824586" + | |
"17866458359124566529476545682848912883142607690042" + | |
"24219022671055626321111109370544217506941658960408" + | |
"07198403850962455444362981230987879927244284909188" + | |
"84580156166097919133875499200524063689912560717606" + | |
"05886116467109405077541002256983155200055935729725" + | |
"71636269561882670428252483600823257530420752963450" | |
) | |
type digits []int | |
func (d digits) mul() (answer int) { | |
answer++ | |
for _, v := range d { | |
answer *= v | |
} | |
return | |
} | |
func data() (answer digits) { | |
s := strings.Split(str, "") | |
answer = make(digits, len(s)) | |
for i, v := range s { | |
answer[i], _ = strconv.Atoi(v) | |
} | |
return | |
} | |
type queue struct { | |
ch chan int | |
length int | |
} | |
func newQueue() queue { | |
ch, ps, xs := make(chan int, 1000), 0, data() | |
for i := 0; i <= len(xs)-5; i++ { | |
go func(j int) { | |
ch <- xs[j : j+5].mul() | |
}(i) | |
ps++ | |
} | |
return queue{ch, ps} | |
} | |
func problem8() { | |
answer := 0 | |
for q := newQueue(); q.length > 0; q.length-- { | |
if n := <-q.ch; n > answer { | |
answer = n | |
} | |
} | |
fmt.Println(answer) | |
} | |
func main() { | |
start := time.Now() | |
problem8() | |
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(8) | |
} | |
func solve(a int) (answer int) { | |
for b := a + 1; b < (1000-a)/2; b++ { | |
c := 1000 - a - b | |
if c*c == a*a+b*b { | |
return a * b * c | |
} | |
} | |
return | |
} | |
func problem9() { | |
max := 1000 / 3 | |
ch := make(chan int, max) | |
for a := 1; a <= max; a++ { | |
go func(n int) { | |
if answer := solve(n); answer > 0 { | |
ch <- answer | |
} | |
}(a) | |
} | |
fmt.Println(<-ch) | |
} | |
func main() { | |
start := time.Now() | |
problem9() | |
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