示例#1
0
    def test0033(self):
        from id_0033 import gcd, is_curious

        self.assertEqual(gcd(15, 10), 5)
        self.assertEqual(gcd(21, 10), 1)
        self.assertEqual(gcd(21, 15), 3)
        self.assertEqual(gcd(8, 3), 1)
        self.assertEqual(gcd(1264, 465), 1)
        self.assertEqual(is_curious((49, 98)), True)
示例#2
0
def generate_fraction(lst):
    f = Fraction(lst[-1], 1)
    for i in range(len(lst) - 2, -1, -1):
        n = lst[i]
        f = n + 1/f
        n = f.numerator
        d = f.denominator
        nd = gcd(d, n)
        n = n/nd
        d = d/nd
        f = Fraction(n, d)
    #print f, n, d, nd
    return f
示例#3
0
def continued_fraction_sqrt(n):
    a = 1
    b = 0
    m = 1
    s = int(sqrt(n))
    while True:
        l = (a*s + b)/m
        yield l
        b = b - l*m
        aa = m*a
        bb = -m*b
        m = n*a*a - b*b
        a = aa
        b = bb
        am = gcd(a, m)
        bm = gcd(b, m)
        abm = gcd(am, bm)
        if abm > 1:
            a = a/abm
            b = b/abm
            m = m/abm
        if m == 0:
            raise StopIteration
示例#4
0
def relatively_prime(n, m):
    return gcd(n, m) == 1
示例#5
0
#
# Przemyslaw Kaminski <*****@*****.**>
# Time-stamp: <>
######################################################################
#from fractions import Fraction
from id_0033 import gcd

if __name__ == '__main__':
    M = 10**6
    d = 2
    # look for proper fractions close and less-than 3/7
    #fr = Fraction(0, 1)
    # tolerance
    tol = 5*10**-5
    best_n = 0
    best_d = 1
    best_num = 0.
    while True:
        for n in range(3*d/7 - 2, 3*d/7 + 1):
            num = float(n)/float(d)
            if num < 3./7 and best_num < num:
                best_n = n
                best_d = d
                best_num = num
                print "New best approximation found: n = %d, d = %d, n/d = %f" % (n, d, num)
        if d == M:
            break
        d += 1
    g = gcd(best_n, best_d)
    print "The fraction: n/d = %d/%d" % (best_n/g, best_d/g)