Editor

Count the Leap Years

#539 · Loops · Beginner · 30 XP · about 5 min

Write count_leap_years(start, end) that returns how many leap years there are from the year start to the year end, counting both of them. If start is later than end, there are none, so return 0.

The leap year rules:

  • A year divisible by 4 is a leap year,
  • except a year divisible by 100, which is not,
  • except a year divisible by 400, which is a leap year after all.

Examples

count_leap_years(2000, 2010)  # returns 3
count_leap_years(1896, 1904)  # returns 2
count_leap_years(2021, 2023)  # returns 0

From 1896 to 1904 the leap years are 1896 and 1904: 1900 is divisible by 100 but not by 400, so it was not one.

Note

Both years are from 1 to 9999.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.