The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
Here is my solution in Golang
package main
import (
"fmt"
)
func main() {
numBuff := make([]float64, 0)
denBuff := make([]float64, 0)
for numerator := 10.0; numerator < 100.0; numerator++ {
if (int(numerator) % 10) != 0 {
for denominator := numerator + 1; denominator < 100.0; denominator++ {
if (int(denominator) % 10) != 0 {
if digitCancelCheck(numerator, denominator) {
fmt.Printf("\"Pair %f and %f\"\n", numerator, denominator)
numBuff = append(numBuff, numerator)
denBuff = append(denBuff, denominator)
}
}
}
}
}
fractionCalc(denBuff, numBuff)
}
func digitSplit(passedNumb int) (SeparatedDigits [2]float64) {
for i := 0; passedNumb != 0; i++ {
modBuff := passedNumb % 10
SeparatedDigits[i] = float64(modBuff)
passedNumb /= 10
}
return SeparatedDigits
}
func digitCancelCheck(numerator float64, denominator float64) bool {
numSeparate := digitSplit(int(numerator))
denSeparate := digitSplit(int(denominator))
divBuff := numerator / denominator
if numSeparate[0] == denSeparate[0] {
if (numSeparate[1] / denSeparate[1]) == divBuff {
return true
}
} else if numSeparate[0] == denSeparate[1] {
if (numSeparate[1] / denSeparate[0]) == divBuff {
return true
}
} else if numSeparate[1] == denSeparate[0] {
if (numSeparate[0] / denSeparate[1]) == divBuff {
return true
}
} else if numSeparate[1] == denSeparate[1] {
if (numSeparate[0] / denSeparate[0]) == divBuff {
return true
}
}
return false
}
func fractionCalc(denominators []float64, numerators []float64) {
numBuff := 1
denBuff := 1
for i := 0; i < 4; i++ {
denBuff *= int(denominators[i])
numBuff *= int(numerators[i])
}
fmt.Printf("Which equals %d/%d, or %f", numBuff, denBuff, float64(numBuff)/float64(denBuff))
return
}