Leap Year Test

10132017, 01:36 AM
Post: #1




Leap Year Test
Introduction
The presented program tests whether a year is a leap year. A leap year has 366 days instead of 365, with the extra day given to February (February 29). The criteria for a leap year are: * Leap years are year numbers evenly divisible by 4. Example: 1992, 2016 * Exception: years that are divisible by 100 but not divisible by 400. Example: 2000 is a leap year, but 1900 and 2100 aren’t. HP Prime Program ISLEAPYEAR Code: EXPORT ISLEAPYEAR(y) Link to blog entry: http://edspi31415.blogspot.com/2017/10/h...year.html 

10132017, 03:06 AM
Post: #2




RE: Leap Year Test
Another way to do it on the Prime :
Code: EXPORT ISLEAPYEAR(y) 

10132017, 08:33 AM
Post: #3




RE: Leap Year Test
Geocaching Coords Solver, Number of triangles Puzzle 

10132017, 11:31 AM
Post: #4




RE: Leap Year Test
I've realized that the IFTE is not needed, so this can be simplified to:
Code: EXPORT ISLEAPYEAR(y) 

10132017, 06:37 PM
(This post was last modified: 10132017 10:38 PM by StephenG1CMZ.)
Post: #5




RE: Leap Year Test
Although years are often considered integer, sometimes it is useful in astronomy to use a real value. Both of the above suggestions fail with 1984.2 (representing 2/10 of the way through 1984) and 1984.1122 (in YYYY.MMDD format).
To avoid this I'd take the integer part of a year. This works for YYYY, YYYY.decimal and YY.MMDD. Code:
And can be simplified to: Code:
I've also avoided DDAYS, which is more complicated (to calculate days it must evaluate a calendar), and might not work with years in real format rather than Prime format. Stephen Lewkowicz (G1CMZ) ANDROID HP Prime App broken offline on some mobiles 

10132017, 08:32 PM
Post: #6




RE: Leap Year Test
(10132017 06:37 PM)StephenG1CMZ Wrote: I've also avoided DDAYS... DDAYS at least pay attention to years before 1583. :) Geocaching Coords Solver, Number of triangles Puzzle 

10132017, 09:06 PM
Post: #7




RE: Leap Year Test
I'm not fluent in HPPPL and don't have my prime with me, so the following should be taken generically.
The leap year calculation is also a good example of using the XOR function. If a year is divisible by 4 then it's a leap year, but if divisible by 100 then it isn't, but if divisible by 400 then it is: Code: boolean isLeapYear = (year%4 == 0) XOR (year%100 == 0) XOR (year%400 == 0) 

10132017, 10:35 PM
Post: #8




RE: Leap Year Test
(10132017 08:32 PM)chromos Wrote:(10132017 06:37 PM)StephenG1CMZ Wrote: I've also avoided DDAYS... The DDAYS ondevice help doesn't specify that  it could be useful. Of course, my code isnt aware of when the calendar changed locally or when centuries were handled differently... On the other hand, it extrapolates the Julian calendar backwards so can handle negative years too (if you remember year 0). Stephen Lewkowicz (G1CMZ) ANDROID HP Prime App broken offline on some mobiles 

10142017, 09:33 AM
Post: #9




RE: Leap Year Test
What a good idea !
And of course, before 1583, only test if the year is exactely divisibility by 4 so it is leap. Good day. Gérard. 

10152017, 07:45 PM
(This post was last modified: 10152017 08:01 PM by StephenG1CMZ.)
Post: #10




RE: Leap Year Test
I earlier remarked that I regarded DDAYS as more complicated than the other solutions, assuming that working out the number of days would be slower.
I have just timed the various solutions, and am surprised to find that DDAYS is quicker. I am also disappointed that simply returning a value seems slightly slower than the evaluating an IF condition. In seconds for 1 million iterations on Android I see DDAYS 20 IF Integer 30 RETURN real 35 (I Need to repeat to see if timings are consistent ) Stephen Lewkowicz (G1CMZ) ANDROID HP Prime App broken offline on some mobiles 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)