Code Problem

Found this problem in a Facebook group. It is likely a homework problem from an undergraduate computer science class.

problem

Solution: Python 3.10

PYTHON
import math

chef = [2,2,5]
customer = [4,5,6,30,123456789012]

lcm_C = math.lcm(*chef)
fx = []
for i in range(lcm_C // min(chef)):
    for ti in chef:
        nti = i * ti
        if (nti < lcm_C):
            fx.append(nti)
fx.sort()
print(lcm_C)
print(fx)
len_fx = len(fx)
for ci in customer:
    k = (ci-1) // len_fx
    x = (ci-1) % len_fx
    print(f'{ci}: k={k} x={x} ans={k*lcm_C+fx[x]}')
Click to expand and view more

Copyright Notice

Author: Vorasilp K.

Link: https://vorasilp.xom/posts/code-problem/

License: CC BY-NC-SA 4.0

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Please attribute the source, use non-commercially, and maintain the same license.

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut