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()


猜你喜欢

转载自blog.csdn.net/q745401990/article/details/38758305