首页 > 代码库 > projecteuler---->problem=33----Digit canceling fractions
projecteuler---->problem=33----Digit canceling fractions
Problem 33
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that49/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.
def isOk(i, j): a = str(i) b = str(j) if a[1]==b[1] and a[1]==0: return False if a[1]==b[0]: if b[1] == 0: return False try: if float(a[0])/float(b[1]) == float(i)/float(j): return True except: pass return False def gcd(a,b): if a < b: a=a+b b=a-b a=a-b if b==0: return a else : return gcd(b,a%b) def main(): resuA = 1 resuB = 1 for i in range(10,100): for j in range(i+1,100): if isOk(i,j): resuA *= i resuB *= j print resuB/gcd(resuA,resuB) if __name__ == "__main__": main()
projecteuler---->problem=33----Digit canceling fractions
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。