Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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.
- #See http://en.wikipedia.org/wiki/Anomalous_cancellation
- #See http://en.wikipedia.org/wiki/Euclidean_algorithm for finding the Greatest
- #Common Divisor.
- def gcd(a,b):
- if b == 0:
- return a
- else:
- return gcd(b,a%b)
- fractions = []
- for i in range(10,99):
- for j in range(i+1,100):
- iStr,jStr = str(i),str(j)
- if jStr[1] != '0' and iStr[1] == jStr[0] and \
- float(iStr)/float(jStr) == float(iStr[0])/float(jStr[1]):
- fractions.append((i,j))
- numProduct = 1
- domProduct = 1
- for i in fractions:
- numProduct *= i[0]
- domProduct *= i[1]
- print domProduct/gcd(numProduct,domProduct)
Advertisement
Add Comment
Please, Sign In to add comment