In order to find leap years, why must the year be indivisible by 100 and divisible by 400? I understand why it must be divisible by 4. Please explain the algorithm.
相关问题
- Finding k smallest elements in a min heap - worst-
- binary search tree path list
- High cost encryption but less cost decryption
- How to get a fixed number of evenly spaced points
- Space complexity of validation of a binary search
相关文章
- What are the problems associated to Best First Sea
- Coin change DP solution to keep track of coins
- Algorithm for partially filling a polygonal mesh
- Robust polygon normal calculation
- Algorithm for maximizing coverage of rectangular a
- How to measure complexity of a string?
- Select unique/deduplication in SSE/AVX
- How to smooth the blocks of a 3D voxel world?
The length of a year is (more or less) 365.242196 days. So we have to subtract, more or less, a quarter of a day to make it fit :
365.242196 - 0.25 = 364.992196 (by adding 1 day in 4 years) : but oops, now it's too small!! lets add a hundreth of a day (by not adding that day once in a hundred year :-))
364.992196 + 0,01 = 365.002196 (oops, a bit too big, let's add that day anyway one time in about 400 years)
365.002196 - 1/400 = 364.999696
Almost there now, just play with leapseconds now and then, and you're set.
(Note : the reason no more corrections are applied after this step is because a year also CHANGES IN LENGTH!!, that's why leapseconds are the most flexible solution, see for examlple here)
That's why i guess
Will it not be much better if we make one step further. Assuming every 3200 year as no leap year, the length of the year will come
and after this the adjustment will be required after around 120000 years.
There's an algorithm on wikipedia to determine leap years:
There's a lot of information about this topic on the wikipedia page about leap years, inclusive information about different calendars.
There are on average, roughly 365.2425 days in a year at the moment (the Earth is slowing down but let's ignore that for now).
The reason we have leap years every 4 years is because that gets us to 365.25 on average
[(365+365+365+366) / 4 = 365.25, 1461 days in 4 years]
.The reason we don't have leap years on the 100-multiples is to get us to 365.24 `[(1461 x 25 - 1) / 100 = 365.24, 36,524 days in 100 years.
Then the reason we once again have a leap year on 400-multiples is to get us to 365.2425
[(36,524 x 4 + 1) / 400 = 365.2425, 146,097 days in 400 years]
.I believe there may be another rule at 3600-multiples but I've never coded for it (Y2K was one thing but planning for one and a half thousand years into the future is not necessary in my opinion - keep in mind I've been wrong before).
So, the rules are, in decreasing priority:
In the Gregorian calendar 3 criteria must be taken into account to identify leap years:
You really should try to google first.
Wikipedia has a explanation of leap years. The algorithm your describing is for the Proleptic Gregorian calendar.
More about the math around it can be found in the article Calendar Algorithms.