05-12-2017, 08:04 PM
Post: #21
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
(05-10-2017 01:28 PM)Vtile Wrote:  I have been in seach of simple algorithm to calculate dDays between two dates.

Now that various aspects of the algorithm have been discussed, here are three complete program listings for the 15C. Enter day [ENTER] month [ENTER] year, call [D] and get a serial number that allows to calculate the number of days between two dates by simple subtraction.

1. Compact version (33 steps), works for dates between 1 Mar 1900 and 28 Feb 2100.
If you want the Julian day number, add 1720982 to the result.

Code:
001  LBL D 002  STO 0 003  R↓ 004  3 005  x≤y? 006  GTO 0 007  + 008  9 009  + 010  1 011  STO-0 012  LBL 0 013  R↓ 014  1 015  + 016  3 017  0 018  , 019  6 020  x 021  INT 022  + 023  RCL 0 024  3 025  6 026  5 027  , 028  2 029  5 030  x 031  INT 032  + 033  RTN

2. Standard version (44 steps) that works down to the first day of the Gregorian calendar (15 Oct 1582).
If you want the Julian day number, add 1720997 to the result.

Code:
001  LBL D 002  STO 0 003  R↓ 004  3 005  x≤y? 006  GTO 0 007  + 008  9 009  + 010  1 011  STO-0 012  LBL 0 013  R↓ 014  1 015  + 016  3 017  0 018  , 019  6 020  x 021  INT 022  + 023  RCL 0 024  3 025  6 026  5 027  , 028  2 029  5 030  x 031  INT 032  + 033  RCL 0 034  EEX 035  2 036  / 037  INT 038  - 039  LSTx 040  4 041  / 042  INT 043  + 044  RTN

3. Deluxe version (64 steps) with automatic switch between Julian and Gregorian calendar (4↔15 Oct 1582). Works down to 1 Mar 0000, returns Julian day number.

Code:
001  LBL D 002  STO 0 003  R↓ 004  3 005  x≤y? 006  GTO 0 007  + 008  9 009  + 010  1 011  STO-0 012  LBL 0 013  R↓ 014  1 015  + 016  3 017  0 018  , 019  6 020  x 021  INT 022  + 023  RCL 0 024  3 025  6 026  5 027  , 028  2 029  5 030  x 031  INT 032  + 033  5 034  7 035  8 036  1 037  6 038  5 039  - 040  x≤0? (TEST 4) 041  GTO 1 042  RCL 0 043  EEX 044  2 045  / 046  INT 047  - 048  LSTx 049  4 050  / 051  INT 052  + 053  2 054  + 055  LBL 1 056  2 057  2 058  9 059  9 060  1 061  6 062  0 063  + 064  RTN

In all versions one stack level is preserved so that date differences can be easily calculated with two successive runs of the program.

28 [ENTER] 2 [ENTER] 2100  f[D]  →  2488128 (assuming deluxe version)
1 [ENTER] 3 [ENTER] 1900  f[D]  →  2415080 (assuming deluxe version)
[–]  →  73048 days

Dieter
 « Next Oldest | Next Newest »