def __add__(self, other_fraction): new_denom = number_theory.lcm(self.denominator, other_fraction.denominator) partial_num_1 = (new_denom // self.denominator) * self.numerator partial_num_2 = (new_denom // other_fraction.denominator) * other_fraction.numerator return Fraction(partial_num_1 + partial_num_2, new_denom)
def lcmTest(): for i in range(0, 5): x = secrets.randbelow(2000) y = secrets.randbelow(2000) if x > y: greater = x else: greater = y while(True): if((greater % x == 0) and (greater % y == 0)): lcm = greater break greater += 1 assert(lcm == nt.lcm(x,y))
def test_other(self): self.assertEqual(lcm(1071, 1029), 52479)
def test_one(self): self.assertEqual(lcm(7, 1), 7)
def test_zero(self): self.assertEqual(lcm(7, 0), 0)
def test_lcm_of_large_two_primes(self): assert number_theory.lcm(1013, 4297) == 4_352_861
def test_lcm_normal(self): assert number_theory.lcm(4, 6) == 12 assert number_theory.lcm(3, 5) == 15 assert number_theory.lcm(8, 20) == 40
def test_lcm_b_multiple_b(self): assert number_theory.lcm(3, 9) == 9
def test_lcm_a_multiple_b(self): assert number_theory.lcm(9, 3) == 9
def test_lcm_same(self): assert number_theory.lcm(1, 1) == 1
from number_theory import gcd, lcm lcm_final = 1 for i in range(20): lcm_final = lcm(lcm_final, i + 1) print('%d -> %d' % (i + 1, lcm_final))