Problem leap year calculation

Joined
May 29, 2024
Messages
1
Reaction score
0
Leap year logic. Is it correct? Basic rule of leap year rule is it comes once every 4 years .
everywhere online when checking the leap years between 1695 and 1705 its showing as 2 and the years are 1696 and 1704 which are having 8 years of gap between them. it's happening between 1795 and 1805 too .its showing 1796 and 1804 which are again having 8 years of gap. the logic is giving this kind of gap between around 1900 too . its is showin 1896 and 1904 as leap years . again gap of 8 years to get a leap year . time to change the logic which is incorrect
 
Joined
Sep 21, 2022
Messages
134
Reaction score
17
Leap, if divisible by 4, but there's an exception to that (Y%100) and an exception to the exception (Y%400).
Code:
pseudocode

FUNCTION IS_LEAP(Y)
L=FALSE
IF (Y%4)==0
  L=TRUE
IF (Y%100)==0
  L=FALSE
IF (Y%400)==0
  L=TRUE
RETURN L
 
Joined
Jul 4, 2023
Messages
416
Reaction score
50
You must apply all rules concerning the definition of a leap year, as shown below:

According to this:
Every year that is exactly divisible by 4 is a leap year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400. The years 1600, 2000 and 2400 are leap years, but 1700, 1800, 1900, 2100, 2200 and 2300 are not. By this rule, an entire leap cycle is 400 years which total 146,097 days, and the average number of days per year is 365 + 1⁄4 − 1⁄100 + 1⁄400 = 365 + 97⁄400 = 365.2425 (This rule could be applied to years before the Gregorian reform to create a proleptic Gregorian calendar, though the result would not match any historical records.)

Historical background are:
In the Gregorian calendar, the standard calendar in most of the world, almost every fourth year is a leap year. Each leap year, the month of February has 29 days instead of 28. Adding one extra day in the calendar every four years compensates for the fact that a period of 365 days is shorter than a tropical year by almost 6 hours. However, this correction is excessive and the Gregorian reform modified the Julian calendar's scheme of leap years as follows as described above.

1717021203417.png


Python:
from calendar import isleap

def is_a_leap_1(year):
    list_ = [ (year % 4 == 0), (year % 400 == 0), (year % 100 == 0) ]
    return False if list_.count(True) in [0,2] else True

def is_a_leap_2(year):
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)


years = [ 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500 ]

# for demonstration purposes
reset = "\033[0m"
bg_color_true = "\033[42m" # green color

for year in years:
    col1 = f"{year} is a leap year: {is_a_leap_1(year)}"
    col2 = f"{year} is a leap year: {is_a_leap_2(year)}"
    col3 = f"{year} is a leap year: {isleap(year)}"
 
    bg_color = bg_color_true if "True" in col1 else ""
    print(f"{bg_color}{col1:<26}  |  {col2:<26}  |  {col3:<26}{reset}")

input("\n")

1717028118785.png
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,824
Messages
2,569,755
Members
45,746
Latest member
DeangeloPo

Latest Threads

Top