# Problem link: http://projecteuler.net/index.php?section=problems&id=33 from myMath import gcd numerator = 1 denominator = 1 for i in range(10, 100): iTen = i / 10 iUnit = i % 10 for j in range(i + 1, 100): jTen = j / 10 jUnit = j % 10 ok = False if iTen == jTen and i * jUnit == j * iUnit: ok = True if iTen == jUnit and i * jTen == j * iUnit: ok = True if iUnit == jTen and i * jUnit == j * iTen: ok = True # This case considered to be trivial, so ignore it # if iUnit == jUnit and i * jTen == j * iTen: # ok = True if ok: numerator *= i denominator *= j print denominator / gcd(numerator, denominator)
import myMath def specialCancel(num,den): num_s = str(num) den_s = str(den) for index,c in enumerate(num_s): if c == den_s[0] and den_s[1] != '0': if float(num_s[(index+1)%2])/float(den_s[1]) == float(num)/den: return True if c == den_s[1] and den_s[0] != '0': if float(num_s[(index+1)%2])/float(den_s[0]) == float(num)/den: return True return False exp = (specialCancel(i,k) for i in range(10,100) for k in range(10,100) if i/k < 1.0) finalI = 1 finalK = 1 for i in range(10,100): for k in range(10,100): if i/k < 1.0 and i%10 != 0 and k % 10 != 0 and specialCancel(i,k): finalI *= i finalK *= k greatest = myMath.gcd(finalI,finalK) print greatest,finalI,finalK print finalI/greatest,finalK/greatest