Last active
October 13, 2015 18:27
-
-
Save Koitaro/4237306 to your computer and use it in GitHub Desktop.
Go/goroutine: Project Euler 10-19
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 primeFactors map[int]int | |
func newPrimeFactors(n int) (answer primeFactors) { | |
answer = make(primeFactors) | |
for p := 2; p*p <= n; p++ { | |
for n%p == 0 { | |
answer[p]++ | |
n /= p | |
} | |
} | |
if n > 1 { | |
answer[n]++ | |
} | |
return | |
} | |
var countFactors func(int) int = newCountFactors() | |
func newCountFactors() func(int) int { | |
mp := make(map[int]int) | |
f := func(n int) (answer int) { | |
if v, ok := mp[n]; ok { | |
return v | |
} | |
answer++ | |
for _, v := range newPrimeFactors(n) { | |
answer *= 1 + v | |
} | |
mp[n] = answer | |
return | |
} | |
return f | |
} | |
type triangle struct { | |
a int | |
b int | |
count int | |
} | |
func (x triangle) num() (answer int) { | |
return x.a * x.b | |
} | |
func gen() (ch chan triangle) { | |
ch = make(chan triangle) | |
go func() { | |
a := 0 | |
var b int | |
for { | |
a++ | |
b = 2*a - 1 | |
ch <- triangle{a, b, countFactors(a) * countFactors(b)} | |
b = 2*a + 1 | |
ch <- triangle{a, b, countFactors(a) * countFactors(b)} | |
} | |
}() | |
return | |
} | |
func problem12() { | |
for x := range gen() { | |
if x.count >= 500 { | |
fmt.Println(x.num()) | |
break | |
} | |
} | |
} | |
func main() { | |
start := time.Now() | |
problem12() | |
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(64) | |
} | |
func steps(m int) (answer int) { | |
n := int64(m) | |
answer++ | |
for n > 1 { | |
if n%2 == 0 { | |
n /= 2 | |
} else { | |
n = 3*n + 1 | |
} | |
answer++ | |
} | |
return | |
} | |
type collatz struct { | |
num int | |
step int | |
} | |
type queue struct { | |
ch chan collatz | |
length int | |
} | |
func newQueue() queue { | |
ch := make(chan collatz, 250000) | |
ps := 0 | |
for n := 500001; n < 1000000; n += 2 { | |
go func(num int) { | |
ch <- collatz{num, steps(num)} | |
}(n) | |
ps++ | |
} | |
return queue{ch, ps} | |
} | |
func problem14() { | |
answer := collatz{0, 0} | |
for q := newQueue(); q.length > 0; q.length-- { | |
if tmp := <-q.ch; tmp.step > answer.step { | |
answer = tmp | |
} | |
} | |
fmt.Println(answer.num) | |
} | |
func main() { | |
start := time.Now() | |
problem14() | |
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(4) | |
} | |
func spell(n int) (s string) { | |
s0 := []string{ | |
"", "One", "Two", "Three", "Four", "Five", | |
"Six", "Seven", "Eight", "Nine", "Ten", | |
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", | |
"Sixteen", "Seventeen", "Eighteen", "Nineteen", | |
} | |
s1 := []string{ | |
"", "", "Twenty", "Thirty", "Forty", "Fifty", | |
"Sixty", "Seventy", "Eighty", "Ninety", | |
} | |
for { | |
switch { | |
case n < 20: | |
s += s0[n] | |
return | |
case n < 100: | |
s += s1[n/10] + s0[n%10] | |
return | |
case n == 1000: | |
return "OneThousand" | |
case n%100 == 0: | |
return s0[n/100] + "Hundred" | |
default: | |
s += s0[n/100] + "HundredAnd" | |
n %= 100 | |
} | |
} | |
return | |
} | |
func spellLength(n int) int { | |
return len(spell(n)) | |
} | |
type queue struct { | |
ch chan int | |
length int | |
} | |
func newQueue() queue { | |
ch, ps := make(chan int, 1000), 0 | |
for n := 1; n <= 1000; n++ { | |
go func(num int) { | |
ch <- spellLength(num) | |
}(n) | |
ps++ | |
} | |
return queue{ch, ps} | |
} | |
func problem17() { | |
answer := 0 | |
for q := newQueue(); q.length > 0; q.length-- { | |
answer += <-q.ch | |
} | |
fmt.Println(answer) | |
} | |
func main() { | |
start := time.Now() | |
problem17() | |
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(100) | |
} | |
func fstDay(year, month int) time.Time { | |
return time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC) | |
} | |
func count(year int) (answer int) { | |
for month := 1; month <= 12; month++ { | |
t := fstDay(year, month) | |
if t.Weekday() == 0 { | |
answer++ | |
} | |
} | |
return | |
} | |
type queue struct { | |
ch chan int | |
length int | |
} | |
func newQueue() queue { | |
ch, ps := make(chan int, 100), 0 | |
for year := 1901; year <= 2000; year++ { | |
go func(y int) { | |
ch <- count(y) | |
}(year) | |
ps++ | |
} | |
return queue{ch, ps} | |
} | |
func problem19() { | |
answer := 0 | |
for q := newQueue(); q.length > 0; q.length-- { | |
answer += <-q.ch | |
} | |
fmt.Println(answer) | |
} | |
func main() { | |
start := time.Now() | |
problem19() | |
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