-
Notifications
You must be signed in to change notification settings - Fork 1
/
euler.go
74 lines (65 loc) · 1.2 KB
/
euler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"flag"
"fmt"
"strconv"
"github.com/bwbeach/euler/integer"
)
func problem2() int {
fib := integer.FibonacciGenerator()
result := 0
for n := fib(); n < 4000000; n = fib() {
if n%2 == 0 {
result += n
}
}
return result
}
func problem4() int {
result := 1
for a := 100; a < 1000; a++ {
for b := a; b < 1000; b++ {
product := a * b
if integer.IsPalindrome(product) && result < product {
result = product
}
}
}
return result
}
func problem5() int {
factors := integer.NewIntBag()
for i := 2; i < 20; i++ {
factors = integer.Union(factors, integer.PrimeFactors(i))
}
result := 1
for _, f := range factors.ToSlice() {
result = result * f
}
return result
}
func getAnswer(problemNumber int) int {
switch problemNumber {
case 1:
return integer.SumMultiples([]int{3, 5}, 1000)
case 2:
return problem2()
case 3:
return integer.LargestPrimeFactor(600851475143)
case 4:
return problem4()
case 5:
return problem5()
default:
return -1
}
}
func main() {
flag.Parse()
problemNumber, err := strconv.Atoi(flag.Arg(0))
if err != nil {
fmt.Printf("Bad problem number: %v\n", flag.Arg(0))
return
}
fmt.Printf("%v\n", getAnswer(problemNumber))
}