Course of Raku / Addendum 🆕 / Flow and functions / Control flow and logic / Exercises / Leap years

Solution: Leap years

Here is a possible solution to the task.

Code

for 2000, 1900, 2024, 2023 -> $year {
    my $leap = $year %% 400 || ($year %% 4 && !($year %% 100));

    say "$year: { $leap ?? 'leap' !! 'common' }";
}

🦋 You can find the source code in the file leap-year.raku.

Output

2000: leap
1900: common
2024: leap
2023: common

Comments

  1. The rule reads directly as a Boolean expression: a leap year is one divisible by 400, or one divisible by 4 but not by 100. %% is the divisibility test.

  2. 1900 is divisible by 100 but not 400, so it is common; 2000 is divisible by 400, so it is leap.

Course navigation

Leap years   |   Kind of triangle