cookchar

Digit Cancelling Fractions

Aug 27th, 2017
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import fractions as frc
  2.  
  3. def digify(n):
  4.     digits = []
  5.     temp = n
  6.     while temp >= 10:
  7.         digits.insert(0, temp % 10)
  8.         temp = temp // 10
  9.  
  10.     digits.insert(0, temp)
  11.  
  12.     return digits
  13.  
  14. def hasLikeDigits(a, b):
  15.     dA = digify(a)
  16.     dB = digify(b)
  17.  
  18.     for d in dA:
  19.         if d in dB:
  20.             return True
  21.  
  22.     return False
  23.  
  24. def getUniqueDigits(a, b):
  25.     dA = digify(a)
  26.     dB = digify(b)
  27.  
  28.     for d in dA:
  29.         if d in dB:
  30.             dA.remove(d)
  31.             dB.remove(d)
  32.             break
  33.  
  34.     return str(dA[0]) + '/' + str(dB[0])
  35.  
  36. fracs = []
  37.  
  38. for n in range(10, 100):
  39.     for m in range(n + 1, 100):
  40.         if hasLikeDigits(n,m) and n % 10 != 0 and m % 10 != 0 and frc.Fraction(getUniqueDigits(n,m)) == frc.Fraction(n,m):
  41.             fracs.append([n,m,str(frc.Fraction(n,m))])
  42.  
  43. print(len(fracs), ':', fracs)
  44.  
  45. prod = frc.Fraction(1,1)
  46.  
  47. for f in fracs:
  48.     prod *= frc.Fraction(f[2])
  49.  
  50. print('Product of Fractions: ', prod)
RAW Paste Data