Leap Year Test - Printable Version +- HP Forums ( https://www.hpmuseum.org/forum)+-- Forum: HP Software Libraries ( /forum-10.html)+--- Forum: HP Prime Software Library ( /forum-15.html)+--- Thread: Leap Year Test ( /thread-9285.html) |

Leap Year Test - Eddie W. Shore - 10-13-2017 01:36 AM
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/hp-prime-and-casio-fx-cg-50-leap-year.html RE: Leap Year Test - Didier Lachieze - 10-13-2017 03:06 AM
Another way to do it on the Prime : Code: `EXPORT ISLEAPYEAR(y)` RE: Leap Year Test - chromos - 10-13-2017 08:33 AM
It even can be an user defined function. [attachment=5243] RE: Leap Year Test - Didier Lachieze - 10-13-2017 11:31 AM
I've realized that the IFTE is not needed, so this can be simplified to: Code: `EXPORT ISLEAPYEAR(y)` RE: Leap Year Test - StephenG1CMZ - 10-13-2017 06:37 PM
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. RE: Leap Year Test - chromos - 10-13-2017 08:32 PM
(10-13-2017 06:37 PM)StephenG1CMZ Wrote: I've also avoided DDAYS... DDAYS at least pay attention to years before 1583. :-) RE: Leap Year Test - David Hayden - 10-13-2017 09:06 PM
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)` RE: Leap Year Test - StephenG1CMZ - 10-13-2017 10:35 PM
(10-13-2017 08:32 PM)chromos Wrote:(10-13-2017 06:37 PM)StephenG1CMZ Wrote: I've also avoided DDAYS... The DDAYS on-device 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). RE: Leap Year Test - ggauny@live.fr - 10-14-2017 09:33 AM
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. RE: Leap Year Test - StephenG1CMZ - 10-15-2017 07:45 PM
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 ) |