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)`

BEGIN

IF FP(y/4) ≠ 0 OR (FP(y/100) == 0 AND FP(y/400) ≠ 0)

THEN

RETURN 0;

ELSE

RETURN 1;

END;

END;

Link to blog entry:

http://edspi31415.blogspot.com/2017/10/h...-year.html
Another way to do it on the Prime :

Code:

`EXPORT ISLEAPYEAR(y)`

BEGIN

IFTE(DDAYS(y+0.0101,y+1.0101)==366,1,0);

END;

I've realized that the IFTE is not needed, so this can be simplified to:

Code:

`EXPORT ISLEAPYEAR(y)`

BEGIN

DDAYS(y+0.0101,y+1.0101)==366;

END;

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:

EXPORT IsLeapYear(Year)

BEGIN

LOCAL YY:=IP(Year);

IF FP(YY/4) ≠ 0 OR (FP(YY/100) == 0 AND FP(YY/400) ≠ 0)

THEN

RETURN 0;

ELSE

RETURN 1;

END;

END;

And can be simplified to:

Code:

EXPORT IsLeapYear(Year)

BEGIN

LOCAL YY:=IP(Year);

RETURN NOT(FP(YY/4) ≠ 0 OR (FP(YY/100) == 0 AND FP(YY/400) ≠ 0));

END;

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.

(10-13-2017 06:37 PM)StephenG1CMZ Wrote: [ -> ]I've also avoided DDAYS...

DDAYS at least pay attention to years before 1583. :-)

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)`

(10-13-2017 08:32 PM)chromos Wrote: [ -> ] (10-13-2017 06:37 PM)StephenG1CMZ Wrote: [ -> ]I've also avoided DDAYS...

DDAYS at least pay attention to years before 1583. :-)

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).

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.

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 )