def problem_5():
    """ Attempt to solve the problem... """

    answer = lcm(1, 2)
    # We can just continue to recalculate the lcm for each new number in the
    # list because: lcm(a,b,c) = lcm(a,lcm(b,c))
    for number in xrange(2, 20+1):
        print number
        answer = lcm(number, answer)

    return answer
Пример #2
0
    def count(self, name, value, split):
        value = self.validate(value)

        file_groups = file_scanner(config.file_in_path)
        file_group = file_groups[self.type][value]

        file_path_name, skip_records_name, count_records_name = name.split(':')

        if len(file_group.files) == split:
            return split
        elif len(file_group.files) >= split:
            return len(file_group.files)
        else:
            return lcm(split, len(file_group.files))
Пример #3
0
def Part2(lines):
    moons = readMoons(lines)

    previousStates = [{} for axis in range(3)]
    foundRepeat = [False for axis in range(3)]

    i = 1
    while True:
        applyGravity(moons)
        applyVelocity(moons)
        moonIdx = 0

        for axis in range(3):
            if 0 == reduce(
                    operator.add,
                [abs(vel[axis]) for vel in [moon[1] for moon in moons]]):
                foundRepeat[axis] = i
        i += 1
        if reduce(lambda x, y: bool(x) and bool(y), foundRepeat):
            break
    lcm = com.lcm(foundRepeat)
    print(
        'Took ', lcm, ' iterations or maybe ', lcm * 2
    )  # not sure why I have to multiply by 2 sometimes but it works on the tests
Пример #4
0
states = set()
initial_state = tuple(moon.state() for moon in moons)

steps = 0

def get_state_of_axis(state, axis):
    return tuple((moon[axis], moon[axis + 3]) for moon in state)
cycles = {}
while len(cycles) < 3:
    # gravity
    for m1, m2 in combinations(moons, 2):
        for i in range(3):
            if m1.pos[i] < m2.pos[i]:
                m1.vel[i] += 1
                m2.vel[i] -= 1
            elif m1.pos[i] > m2.pos[i]:
                m1.vel[i] -= 1
                m2.vel[i] += 1
    # velocity
    for moon in moons:
        moon.pos = tadd(moon.pos, moon.vel)
    steps += 1
    current_state = tuple(moon.state() for moon in moons)

    for i in range(3):
        if get_state_of_axis(current_state, i) == get_state_of_axis(initial_state, i):
            if i not in cycles:
                cycles[i] = steps

print(lcm(lcm(cycles[0], cycles[1]), cycles[2]))
Пример #5
0
def test_lcm():
    """Tests covering the lcm function"""

    assert lcm(12, 30) == 60
    assert lcm(24, 300) == 600
Пример #6
0
def run():
    nums = (x for x in xrange(1,21))
    return lcm(*nums)