def universe_repeats(moons): # find when there is a cycle in the x position and velocity of all moons, # and also for the y and z coordinates too seenx, seeny, seenz = set(), set(), set() findx, findy, findz = True, True, True while findx or findy or findz: x = tuple([(m[0], m[3]) for m in moons]) y = tuple([(m[1], m[4]) for m in moons]) z = tuple([(m[2], m[5]) for m in moons]) if x in seenx: findx = False if y in seeny: findy = False if z in seenz: findz = False seenx.add(x); seeny.add(y); seenz.add(z) update(moons) # now we can say that the first cycle of all three elements is # the lowest common multiple of the three independent cycles return lcm(lcm(len(seenx), len(seeny)), len(seenz))
def earliest(ids): time, interval = ids[0] for offset, period in ids[1:]: while True: if (time + offset) % period == 0: break time += interval interval = lcm(interval, period) return time
def cycle_product(m1: Monomial, m2: Monomial) -> Monomial: """ Given two monomials (from the cycle index of a symmetry group), compute the resultant monomial in the cartesian product corresponding to their merging. """ assert isinstance(m1, Monomial) and isinstance(m2, Monomial) A = m1.variables B = m2.variables result_variables = dict() for i in A: for j in B: k = lcm(i, j) g = (i * j) // k if k in result_variables: result_variables[k] += A[i] * B[j] * g else: result_variables[k] = A[i] * B[j] * g return Monomial(result_variables, Fraction(m1.coeff * m2.coeff, 1))
def test_lcm(self): self.assertEqual(24, lcm(8, 12))