码迷,mamicode.com
首页 > 其他好文 > 详细

projecteuler---->problem=33----Digit canceling fractions

时间:2014-08-22 16:17:19      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:projecteuler   python   

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

标签:projecteuler   python   

原文地址:http://blog.csdn.net/q745401990/article/details/38758305

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!