Post Reply 
Leap Year Test
10-13-2017, 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)
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
Visit this user's website Find all posts by this user
Quote this message in a reply
10-13-2017, 03:06 AM
Post: #2
RE: Leap Year Test
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;
Find all posts by this user
Quote this message in a reply
10-13-2017, 08:33 AM
Post: #3
RE: Leap Year Test
It even can be an user defined function.
   

Geocaching Coords Solver, Number of triangles Puzzle
Find all posts by this user
Quote this message in a reply
10-13-2017, 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)
BEGIN
DDAYS(y+0.0101,y+1.0101)==366;
END;
Find all posts by this user
Quote this message in a reply
10-13-2017, 06:37 PM (This post was last modified: 10-13-2017 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:


 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.

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
10-13-2017, 08:32 PM
Post: #6
RE: Leap Year Test
(10-13-2017 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
Find all posts by this user
Quote this message in a reply
10-13-2017, 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)
Find all posts by this user
Quote this message in a reply
10-13-2017, 10:35 PM
Post: #8
RE: Leap Year Test
(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).

Stephen Lewkowicz (G1CMZ)
Visit this user's website Find all posts by this user
Quote this message in a reply
10-14-2017, 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.


Attached File(s) Thumbnail(s)
   

Gérard.
Find all posts by this user
Quote this message in a reply
10-15-2017, 07:45 PM (This post was last modified: 10-15-2017 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)
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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