-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (46 loc) · 992 Bytes
/
main.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
package main
import (
"fmt"
"math"
"time"
)
func gcd(a, b int64) int64 {
if b == 0 {
return a
}
return gcd(b, a%b)
}
type Fraction struct {
Numerator int64
Denominator int64
}
func New(n, d int64) Fraction {
return Fraction{n, d}
}
func (f Fraction) String() string {
return fmt.Sprintf("%d/%d", f.Numerator, f.Denominator)
}
func (f Fraction) Reduce() Fraction {
v := gcd(f.Numerator, f.Denominator)
if v == 1 {
return f
}
return New(f.Numerator/v, f.Denominator/v)
}
func main() {
start := time.Now()
fractions := map[Fraction]struct{}{}
for d := int64(12000); d > 1; d-- {
startN := int64(float64(d) / 3)
if startN == d/3 {
startN += 1
}
endN := int64(math.Ceil(float64(d) / 2))
for n := int64(startN); n < endN; n++ {
reducedFraction := New(n, d).Reduce()
fractions[reducedFraction] = struct{}{}
}
}
elapsed := time.Since(start)
fmt.Printf("fractions between 1/3 and 1/2 (d<= 12000) = %v (elapsed = %v)", len(fractions), elapsed)
}