CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Views: 55
Image: ubuntu2204
Kernel: SageMath 10.1

In a parallel universe we measure time in timestamps from a specific epoch 0. In this universe in Hungary every train departs from Budapest central train station and makes a round trip. Every train is always on time. After a train arrives to Budapest central station, it departs in the same timestamp.

In the following table you can see the counties of Hungary with their seat. The values in the First departure column tells us when the first train departs after the epoch and Return time tells us when it returns back.

CountySeatReturn timeFirst departure
Bács-KiskunKecskemét5950
BaranyaPécs13747
BékésBékéscsaba13960
Borsod-Abaúj-ZemplénMiskolc12755
Csongrád-CsanádSzeged11335
FejérSzékesfehérvár4356
Győr-Moson-SopronGyőr8320
Hajdú-BiharDebrecen15741
HevesEger8945
Jász-Nagykun-SzolnokSzolnok7330
Komárom-EsztergomTatabánya4151
NógrádSalgótarján7946
PestBudapestN/AN/A
SomogyKaposvár13138
Szabolcs-Szatmár-BeregNyíregyháza15122
TolnaSzekszárd10152
VasSzombathely14938
VeszprémVeszprém6740
ZalaZalaegerszeg16319

For example to Miskolc in Borsod-Abaúj-Zemplén county, the first train departs at timestamp 55 and the subsequent departures are 182, 309, etc.

Let us assume that you arrive at Budapest central station at timestamp 200000, and you hop on the next train that departs from the station. To which city do you travel and how many timestamps do you need to wait until departure?

# List of return times return_times = [59, 139, 137, 127, 113, 43, 83, 157, 89, 73, 41, 79, 131, 151, 101, 149, 67, 163] # List of first departures first_departures = [50, 47, 60, 55, 35, 56, 20, 41, 45, 30, 51, 46, 38, 22, 52, 58, 40, 19] # Calculate next departures and wait times next_departures = [next_departure_time(rt, fd, 200000) for rt, fd in zip(return_times, first_departures)] # Find the minimum wait time and corresponding city min_wait_time, min_wait_time_index = min((wt, idx) for idx, (_, wt) in enumerate(next_departures)) # List of city names city_names = ["Kecskemét", "Pécs", "Békéscsaba", "Miskolc", "Szeged", "Székesfehérvár", "Győr", "Debrecen", "Eger", "Szolnok", "Tatabánya", "Salgótarján", "Kaposvár", "Nyíregyháza", "Szekszárd", "Szombathely", "Veszprém", "Zalaegerszeg"] # Get the next city and wait time next_city = city_names[min_wait_time_index] print(next_city, min_wait_time)
Kecskemét 1

What is the first timestamp when every train arrives and departs from Budapest central station at the same time?

from sympy.ntheory.modular import crt train_schedules = { "Kecskemét": {"return_time": 59, "first_departure": 50}, "Pécs": {"return_time": 137, "first_departure": 47}, "Békéscsaba": {"return_time": 139, "first_departure": 60}, "Miskolc": {"return_time": 127, "first_departure": 55}, "Szeged": {"return_time": 113, "first_departure": 35}, "Székesfehérvár": {"return_time": 43, "first_departure": 56}, "Győr": {"return_time": 83, "first_departure": 20}, "Debrecen": {"return_time": 157, "first_departure": 41}, "Eger": {"return_time": 89, "first_departure": 45}, "Szolnok": {"return_time": 73, "first_departure": 30}, "Tatabánya": {"return_time": 41, "first_departure": 51}, "Salgótarján": {"return_time": 79, "first_departure": 46}, "Kaposvár": {"return_time": 131, "first_departure": 38}, "Nyíregyháza": {"return_time": 151, "first_departure": 22}, "Szekszárd": {"return_time": 101, "first_departure": 52}, "Szombathely": {"return_time": 149, "first_departure": 38}, "Veszprém": {"return_time": 67, "first_departure": 40}, "Zalaegerszeg": {"return_time": 163, "first_departure": 19} } return_time = [schedule["return_time"] for schedule in train_schedules.values()] first_departure = [schedule["first_departure"] for schedule in train_schedules.values()] same_time = crt(return_time, first_departure)[0] print(same_time)
409609906862243868805951026779670658