예제 #1
0
def problem33():
    """ 
    The fraction ^(49)/_(98) is a curious fraction, as an inexperienced mathematician 
    in attempting to simplify it may incorrectly believe that ^(49)/_(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.
    """
    nr = 10
    dr = 10
    vals = []
    for i in range(10, 100):
        for j in range(i + 1, 100):
            vals.append((i, j))

    nr_sum = 1
    dr_sum = 1
    for pair in vals:
        (nr, dr) = pair
        if nr % 10 == 0 or dr % 10 == 0:
            continue
        if nr % 11 == 0 or dr % 11 == 0:
            continue
        if str(nr)[1] == str(dr)[0]:
            if nr * (dr % 10) == dr * (nr / 10):
                nr_sum *= nr
                dr_sum *= dr

    return dr_sum / mlib.gcd(nr_sum, dr_sum)
예제 #2
0
def problem73():
    max = 1/2.0
    min = 1/3.0
    frac_map = {}
    for b in range(1, 10**4+1):
        for a in range(1, b):
            dec = float(a) / b
            if dec <= min or dec >= max:
                continue
            gcd = mlib.gcd(a, b)
            frac_map[(a/gcd, b/gcd)] = 1
        print b

    return len(frac_map)
예제 #3
0
def problem5():
    """ 
    2520 is the smallest number that can be divided by each of the 
    numbers from 1 to 10 without any remainder.
    What is the smallest number that is evenly divisible by all of 
    the numbers from 1 to 20?
    """
    sol = 1
    divs = range(1, 21)
    while len(divs) != 0:
        n = divs.pop()
        k = mlib.gcd(sol, n)
        sol *= n / k

    return sol
예제 #4
0
def problem71():
    top = 3.0
    bot = 7.0
    fraclist = []
    while bot < 10**6:
        n = top/bot
        if n >= (3.0/7):
            bot += 1
        elif n <= (2.0/5):
            top += 1
        else:
            fraclist.append((n, top, bot))
            top += 1

    fraclist.sort()
    a,b = fraclist[-1][1:]
    return a / mlib.gcd(a,b)
예제 #5
0
 def loop(tot, pos):
     while pos>0:
         if mlib.gcd(pos,n)==1:  return loop(tot+1,pos-1)
         else:              return loop(tot,  pos-1)
     return tot