Leap Years. In a leap year, February has 29 days… | by Abhimanyu Singh | Reasoning Hub | Medium

Leap Years

Abhimanyu Singh
Reasoning Hub
Published in
4 min readSep 6, 2020

--

Image Source: Google Images

In a leap year, February has 29 days, thus the total number of days in a leap year is 366.

Leap year in the Julian calendar
The year which is divisible by 4 is a leap year.

Leap year in the Gregorian calendar
The year that satisfies one of the following conditions:

  • If the year is divisible by 4 and not divisible by 100.
  • If the year is divisible by 100 and also divisible by 400.

For example:

  • 1976 is a leap year in both the calendars.
  • 1900 is a leap year in the Julian calendar but not in the Gregorian calendar.
  • 2000 is a leap year in both the calendars.

Leap years between two years

We know that every fourth year from a leap year is a leap year as well. There is one exception to this when we are using the Gregorian calendar as defined above.

Question: In the Julian calendar, how many years between 2015 to 2026 are leap years?
Answer: 3

The nearest leap year to 2015 is 2016. Thus, 2016, 2020, and 2024 are leap years.

Question: In the Gregorian calendar, how many years between 1889 to 2137 are leap years?
Answer: 60

The nearest leap year to 1889 is 1892. Thus, 1892, 1896, 1904, 1908, 1912, 1916, 1920, 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056, 2060, 2064, 2068, 2072, 2076, 2080, 2084, 2088, 2092, 2096, 2104, 2108, 2112, 2116, 2120, 2124, 2128, 2132, and 2136.

Note that, 1900 and 2100 are not counted because these years are divisible by 100 but not divisible by 400.

We can see counting leap years this way is a cumbersome process.

Venn Diagram: Leap Years in the Gregorian calendar

Let’s re-iterate on the leap year condition in the Gregorian calendar:

  • If any number is divisible by 100, then it is definitely divisible by 4. For example, 1700, 1300, 2000, and 1800 are all divisible by 100 and also by 4 as well.
  • If any number is divisible by 400, then it is definitely divisible by both 4 and 100. For example, 1200, 1600, 2400, and 3200 are all divisible by 400 and also by both 4 and 100 as well.
Venn Diagram: Divisibility by 4, 100, and 400

From the Venn diagram, we can see leap years fall in Part-1 and Part-3. Suppose, we want to find the leap years between year 1 and year Y. So:

Venn Diagram: Divisibility by 4, 100, and 400

So,

Venn Diagram: Leap years count

Thus, the total number of leap years between year 1 and year Y is (Y / 4) — (Y / 100) + (Y / 400) in the Gregorian calendar. For the Julian calendar, the number is (Y / 4) only.

Also, to count the leap years between two years A and B, we can find:

  • The leap years between year 1 and year B.
  • The leap years between year 1 and year A — 1.

Finally, subtract the counts.

Question: In the Julian calendar, how many years between 2015 to 2026 are leap years?
Answer: 3

Here, A = 2015 and B = 2026. So,

  • The total number of leap years between year 1 and year 2026 is (2026 / 4) = 506.
  • The total number of leap years between year 1 and year 2014 is (2014 / 4) = 503.

So, the total number of leap years between 2015 and 2026 is 506 — 503 = 3.

Question: In the Gregorian calendar, how many years between 1889 to 2137 are leap years?
Answer: 60

Here, A = 1889 and B = 2137. So,

  • The total number of leap years between year 1 and year 2137 = (2137 / 4) — (2137 / 100) + (2137 / 400) = 518
  • The total number of leap years between year 1 and year 1888 = (1888 / 4) — (1888 / 100) + (1888 / 400) = 458

So, the total number of leap years between 1889 and 2137 is 518–458 = 60.

--

--

Abhimanyu Singh
Reasoning Hub

Lead Engineer, HackerRank