In the recent challenge a formula was provided for days beteen dates, but didn't work in some instances. Actually it won't work when dates include any century years not divisible by 400.
Quote:
2.281900
3.011900
--> 2 using this algorithm (incorrect)
--> 1 on a real 12c (correct, because 1900 is not a leap year)
A single failure, ie a single time that it does not work means the program's broken.
OK, so flame me for being pedantic.
However, I offer a solution. I thought to base my solution on finding the difference between Julian Days using the Julian Day formulae:
a = INT[(14-month)/12]
y = year+4800-a
m = month + 12*a - 3
For a date in the Gregorian (current) calendar:
JD = day + INT[(153*m+2)/5] + y*365 + INT[y/4] – INT[y/100] + INT[y/400] - 32045 (this is for whole days)
Observations:
As the JD numbering starts in 4800BC, and assuming that we're only interested in 0 AD onwards, discard the 4800 in the "y" formula.
Also, as we're interested in the difference between 2 JD's and "-32045" is a common factor, discard it too.
My first implementation is on the 32S,
A01 LBL A B21 + B47 -
A02 STO A B22 5 B48 RCL C
A03 x<>y B23 ÷ B49 400
A04 STO B B24 IP B50 ÷
A05 100 B25 RCL B B51 IP
A06 STO D B26 RCL D B52 +
B01 LBL B B27 * B53 RCL B
B02 RCL B B28 FP B54 FP
B03 IP B29 RCL D B55 RCL D
B04 +/- B30 x^2 B56 *
B05 14 B31 * B57 IP
B06 + B32 RCL E B58 +
B07 12 B33 - B59 RCL A
B08 ÷ B34 STO C B60 STO B
B09 IP B35 365 B61 x<>y
B10 STO E B36 * B62 STO A
B11 12 B37 + B63 x<>y
B12 * B38 RCL C B64 FP
B13 3 B39 4 B65 x>0?
B14 - B40 ÷ B66 GTO B
B15 RCL B B41 IP B67 x<>y
B16 IP B42 + B68 LAST x
B17 + B43 RCL C B69 -
B18 153 B44 RCL D B70 RTN
B19 * B45 ÷
B20 2 B46 IP
This is 5 registers, 2 labels and 76 lines. I tried a mix of registers & code and this gives me the best combination (as numbers in lines seem to use lots of memory). 178 bytes calculated as (free mem before code and with cleared registers) subtract (free mem after coding, execution and leaving registers un-cleared).
I thought a challenge would be implementing it on a 20S. Being algebraic, it takes more steps to do things. It has a fixed 99 step memory and the register memory is separate. Unfortunately I could not find a solution without having to pre-enter data on some registers (6 to 9 see below listing).
01 LBL A 34 IP 67 RCL 3
02 STO 0 35 + 68 /
03 SWAP 36 1 69 4
04 STO 1 37 2 70 =
05 C (ON) 38 * 71 IP
06 LBL B 39 RCL 2 72 -
07 RCL 1 40 - 73 (
08 IP 41 3 74 RCL 3
09 +/- 42 = 75 /
10 + 43 STO 4 76 RCL 9
11 1 44 RCL 1 77 )
12 4 45 FP 78 IP
13 = 46 * 79 =
14 / 47 RCL 9 80 +
15 1 48 = 81 RCL 3
16 2 49 IP 82 /
17 = 50 + 83 RCL 6
18 IP 51 ( 84 =
19 STO 2 52 RCL 8 85 IP
20 RCL 1 53 * 86 RCL 0
21 * 54 RCL 4 87 STO 1
22 RCL 9 55 + 88 FP
23 = 56 2 89 x=0?
24 FP 57 ) 90 GTO D
25 * 58 / 91 LAST
26 RCL 9 59 5 92 STO 0
27 x^2 60 = 93 GTO B
28 = 61 IP 94 LBL D
29 - 62 + 95 LAST
30 RCL 2 63 RCL 7 96 -
31 = 64 * 97 RCL 0
32 STO 3 65 RCL 3 98 =
33 RCL 1 66 + 99 RTN
Pre-program the following numbers:
100 STO 9
153 STO 8
365 STO 7
400 STO 6
Executing:
D1 INPUT D2 XEQ A
Now I do not have an 11C, nor emulator, but I Imagine the code would look like this:
01 LBL A 30 5 59 RCL 3
02 STO 0 31 3 60 ÷
03 x<>y 32 * 61 INT
04 STO 1 33 2 62 -
05 1 34 + 63 RCL 2
06 0 35 5 64 4
07 0 36 ÷ 65 0
08 STO 3 37 INT 66 0
09 LBL B 38 RCL 1 67 ÷
10 RCL 1 39 RCL 3 68 INT
11 INT 40 * 69 +
12 +/- 41 FRAC 70 RCL 1
13 1 42 RCL 3 71 FRAC
14 4 43 x^2 72 RCL 3
15 + 44 * 73 *
16 1 45 RCL 4 74 INT
17 2 46 - 75 +
18 ÷ 47 STO 2 76 RCL 0
19 INT 48 3 77 STO 1
20 STO 4 49 6 78 x<>y
21 1 50 5 79 STO 0
22 2 51 * 80 x<>y
23 * 52 + 81 FRAC
24 3 53 RCL 2 82 x>0?
25 - 54 4 83 GTO B
26 RCL 1 55 ÷ 84 x<>y
27 INT 56 INT 85 LAST x
28 + 57 + 86 -
29 1 58 RCL 2 87 RTN
It would get a score of 119 (87lines + 7*4registers + 2*2labels). I could probably have saved on registers but was worried about program length as I think the 11C has max 99 lines (not sure about this as it dynamically re-allocates memory between registers?).
Closing observations:
a = INT[(14-month)/12] gives 1 if month is <=2 and 0 if >2. Perhaps using flags could save some lines.
I am not a great programmer, so the above could probably be optimised. It would be particularly useful to see solutions for the 20S not requiring pre-entering data in some registers.
Bart