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: 45
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?

def find_next_departure(arrival_timestamp, first_departure, interval): if interval == 0: return None, None # Handle division by zero next_departure = ((arrival_timestamp - first_departure) // interval + 1) * interval + first_departure waiting_time = max(0, next_departure - arrival_timestamp) return next_departure, waiting_time # Train schedule data counties = [ {"county": "Bács-Kiskun", "seat": "Kecskemét", "first_departure": 50, "interval": 59}, {"county": "Baranya", "seat": "Pécs", "first_departure": 47, "interval": 137}, {"county": "Békés", "seat": "Békéscsaba", "first_departure": 60, "interval": 139}, {"county": "Borsod-Abaúj-Zemplén", "seat": "Miskolc", "first_departure": 55, "interval": 127}, {"county": "Csongrád-Csanád", "seat": "Szeged", "first_departure": 35, "interval": 113}, {"county": "Fejér", "seat": "Székesfehérvár", "first_departure": 56, "interval": 43}, {"county": "Győr-Moson-Sopron", "seat": "Győr", "first_departure": 20, "interval": 83}, {"county": "Hajdú-Bihar", "seat": "Debrecen", "first_departure": 41, "interval": 157}, {"county": "Heves", "seat": "Eger", "first_departure": 45, "interval": 89}, {"county": "Jász-Nagykun-Szolnok", "seat": "Szolnok", "first_departure": 30, "interval": 73}, {"county": "Komárom-Esztergom", "seat": "Tatabánya", "first_departure": 51, "interval": 41}, {"county": "Nógrád", "seat": "Salgótarján", "first_departure": 46, "interval": 79}, {"county": "Pest", "seat": "Budapest", "first_departure": 0, "interval": 0}, # Handle division by zero {"county": "Somogy", "seat": "Kaposvár", "first_departure": 38, "interval": 131}, {"county": "Szabolcs-Szatmár-Bereg", "seat": "Nyíregyháza", "first_departure": 22, "interval": 151}, {"county": "Tolna", "seat": "Szekszárd", "first_departure": 52, "interval": 101}, {"county": "Vas", "seat": "Szombathely", "first_departure": 38, "interval": 149}, {"county": "Veszprém", "seat": "Veszprém", "first_departure": 40, "interval": 67}, {"county": "Zala", "seat": "Zalaegerszeg", "first_departure": 19, "interval": 163}, ] # Arrival at Budapest central station at timestamp 200,000 arrival_timestamp = 200000 # Finding the next departure for each county next_departure_seat = None min_waiting_time = float('inf') for county in counties: next_departure, waiting_time = find_next_departure(arrival_timestamp, county["first_departure"], county["interval"]) if next_departure is not None and waiting_time < min_waiting_time: min_waiting_time = waiting_time next_departure_seat = county["seat"] print(f"Next departure is to {next_departure_seat} seat with a wait time of {min_waiting_time} timestamps.")
Next departure is to Kecskemét seat with a wait time of 1 timestamps.

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

import math def find_next_departure(arrival_timestamp, first_departure, interval): if interval == 0: return None, None # Handle division by zero next_departure = ((arrival_timestamp - first_departure) // interval + 1) * interval + first_departure waiting_time = max(0, next_departure - arrival_timestamp) return next_departure, waiting_time # Train schedule data counties = [ {"county": "Bács-Kiskun", "seat": "Kecskemét", "first_departure": 50, "interval": 59}, {"county": "Baranya", "seat": "Pécs", "first_departure": 47, "interval": 137}, {"county": "Békés", "seat": "Békéscsaba", "first_departure": 60, "interval": 139}, {"county": "Borsod-Abaúj-Zemplén", "seat": "Miskolc", "first_departure": 55, "interval": 127}, {"county": "Csongrád-Csanád", "seat": "Szeged", "first_departure": 35, "interval": 113}, {"county": "Fejér", "seat": "Székesfehérvár", "first_departure": 56, "interval": 43}, {"county": "Győr-Moson-Sopron", "seat": "Győr", "first_departure": 20, "interval": 83}, {"county": "Hajdú-Bihar", "seat": "Debrecen", "first_departure": 41, "interval": 157}, {"county": "Heves", "seat": "Eger", "first_departure": 45, "interval": 89}, {"county": "Jász-Nagykun-Szolnok", "seat": "Szolnok", "first_departure": 30, "interval": 73}, {"county": "Komárom-Esztergom", "seat": "Tatabánya", "first_departure": 51, "interval": 41}, {"county": "Nógrád", "seat": "Salgótarján", "first_departure": 46, "interval": 79}, {"county": "Pest", "seat": "Budapest", "first_departure": 0, "interval": 0}, # Handle division by zero {"county": "Somogy", "seat": "Kaposvár", "first_departure": 38, "interval": 131}, {"county": "Szabolcs-Szatmár-Bereg", "seat": "Nyíregyháza", "first_departure": 22, "interval": 151}, {"county": "Tolna", "seat": "Szekszárd", "first_departure": 52, "interval": 101}, {"county": "Vas", "seat": "Szombathely", "first_departure": 38, "interval": 149}, {"county": "Veszprém", "seat": "Veszprém", "first_departure": 40, "interval": 67}, {"county": "Zala", "seat": "Zalaegerszeg", "first_departure": 19, "interval": 163}, ] # Calculate the least common multiple (LCM) of departure intervals departure_intervals = [county["interval"] for county in counties if county["interval"] != 0] lcm = math.lcm(*departure_intervals) print(f"The calculated LCM is: {lcm}")
The calculated LCM is: 618098729470538661175838260621338857