The year is 2525 and you've just embarked on a journey to visit all planets in the Solar System (Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus and Neptune). The first stop is Mercury, where customs require you to fill out a form (bureaucracy is apparently not Earth-specific). As you hand over the form to the customs officer, they scrutinize it and frown. "Do you really expect me to believe you're just 50 years old? You must be closer to 200 years old!"
Amused, you wait for the customs officer to start laughing, but they appear to be dead serious. You realize that you've entered your age in Earth years, but the officer expected it in Mercury years! As Mercury's orbital period around the sun is significantly shorter than Earth, you're actually a lot older in Mercury years. After some quick calculations, you're able to provide your age in Mercury Years. The customs officer smiles, satisfied, and waves you through. You make a mental note to pre-calculate your planet-specific age before future customs checks, to avoid such mix-ups.
If you're wondering why Pluto didn't make the cut, go watch this YouTube video.
Given an age in seconds, calculate how old someone would be on a planet in our Solar System.
One Earth year equals 365.25 Earth days, or 31,557,600 seconds. If you were told someone was 1,000,000,000 seconds old, their age would be 31.69 Earth-years.
For the other planets, you have to account for their orbital period in Earth Years:
Planet | Orbital period in Earth Years |
---|---|
Mercury | 0.2408467 |
Venus | 0.61519726 |
Earth | 1.0 |
Mars | 1.8808158 |
Jupiter | 11.862615 |
Saturn | 29.447498 |
Uranus | 84.016846 |
Neptune | 164.79132 |
The actual length of one complete orbit of the Earth around the sun is closer to 365.256 days (1 sidereal year). The Gregorian calendar has, on average, 365.2425 days. While not entirely accurate, 365.25 is the value used in this exercise. See Year on Wikipedia for more ways to measure a year.
In Cairo, where there's no native support for floating-point numbers, we represent fractional values using integers.
This approach is essential in blockchain development to maintain precision in calculations.
In this exercise, we use fixed-point arithmetic by converting orbital periods into microseconds.
For example, Mercury’s orbital period of 0.2408467
Earth years becomes 240,846,700
microseconds by multiplying by 1,000,000
.
To account for decimal precision, the test cases assume that the resulting age has two decimal places, represented as integers.
This means that an age of 31.69
years is stored as 3169
in the code.
To achieve this, we multiply by 100 before performing the division.
Here's an example:
let mercury_orbital_period = 240_846_700; // in microseconds
let age_microseconds = age_seconds * 1_000_000;
// multiplying with 100 to retain 2 decimal places
age_microseconds * 100 / mercury_orbital_period
By using this method, you ensure that fractional values are accurately represented as integers while maintaining the required two-decimal precision, which is crucial for the tests to pass.
Sign up to Exercism to learn and master Cairo with 66 exercises, and real human mentoring, all for free.