-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (95 loc) · 2.37 KB
/
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"strconv"
"strings"
"time"
)
func IsDigitCancelling(numerator, denominator int, trivial bool) (bool, error) {
strNumerator := fmt.Sprintf("%d", numerator)
strDenominator := fmt.Sprintf("%d", denominator)
if len(strNumerator) != 2 || len(strDenominator) != 2 {
return false, nil
}
digits := make(map[rune]int)
for _, r := range strNumerator {
digits[r]++
}
for _, r := range strDenominator {
digits[r]++
}
if len(digits) != 3 {
return false, nil
}
var commonRune rune
for digit, count := range digits {
if count == 2 {
commonRune = digit
}
}
if !trivial {
if commonRune == '0' {
return false, nil
}
}
simpleStrNumerator := strings.ReplaceAll(strNumerator, string(commonRune), "")
simpleNumerator, err := strconv.Atoi(simpleStrNumerator)
if err != nil {
return false, err
}
simpleStrDenominator := strings.ReplaceAll(strDenominator, string(commonRune), "")
simpleDenominator, err := strconv.Atoi(simpleStrDenominator)
if err != nil {
return false, err
}
if simpleDenominator == 0 {
return false, nil
}
fraction := float64(numerator) / float64(denominator)
simpleFraction := float64(simpleNumerator) / float64(simpleDenominator)
if fraction != simpleFraction {
return false, nil
}
return true, nil
}
func simplifyFraction(fraction Fraction) Fraction {
outerLoop:
for {
for i := 2; i < fraction.denominator; i++ {
if fraction.numerator%i == 0 && fraction.denominator%i == 0 {
fraction.numerator /= i
fraction.denominator /= i
continue outerLoop
}
}
break
}
return fraction
}
type Fraction struct {
numerator int
denominator int
}
func (f Fraction) String() string {
return fmt.Sprintf("%d/%d", f.numerator, f.denominator)
}
func main() {
start := time.Now()
fractions := make([]Fraction, 0)
for i := 1; i < 100; i++ {
for j := i; j < 100; j++ {
isDigitalCancelling, err := IsDigitCancelling(i, j, false)
if isDigitalCancelling == true && err == nil {
fractions = append(fractions, Fraction{numerator: i, denominator: j})
}
}
}
result := Fraction{1, 1}
for _, fraction := range fractions {
result.numerator *= fraction.numerator
result.denominator *= fraction.denominator
}
elapsed := time.Since(start)
fmt.Printf("fractions = %v\n", fractions)
fmt.Printf("result = %v & simpleified version = %v (elapsed=%v)\n", result, simplifyFraction(result), elapsed)
}