CARDIAC › Tips and tricks › Digits
Fit the arithmetic to three digits
Say you want the next number in a random sequence: (21s + 17) mod 1000.
The direct way works out 21s by adding s to itself 21 times. Every STO keeps only three digits, so the mod 1000 takes care of itself, but you're looping 21 times for one number.
cell contents notes
10 040 INP 40 cells 10-19: 21s by adding s 21 times; STO keeps three digits each time
11 141 CLA 41
12 642 STO 42
13 143 CLA 43
14 240 ADD 40
15 643 STO 43
16 142 CLA 42
17 200 ADD 00
18 642 STO 42
19 313 TAC 13
20 143 CLA 43 cells 20-24: then add 17
21 244 ADD 44
22 643 STO 43
23 543 OUT 43
24 900 HRS 00
40 000
41 -021
42 000
43 000
44 017
Step through the natural way in Cardigan
With a little rearranging, nothing in the formula needs more than four digits. 21s is 20s + s, and 20s mod 1000 is 10 times (2s mod 100). SFT 22 keeps the last two digits, which is your mod 100. SFT 10 multiplies by ten. And the final STO does the mod 1000 for you.
cell contents notes
10 040 INP 40 cells 10-19: 21s + 17 as 10 (2s mod 100) + s + 17
11 140 CLA 40
12 240 ADD 40 2s
13 422 SFT 22 2s mod 100
14 410 SFT 10 10 (2s mod 100), never past 990
15 240 ADD 40
16 241 ADD 41
17 642 STO 42 STO keeps three digits: the mod 1000
18 542 OUT 42
19 900 HRS 00
40 000
41 017
42 000
Step through the CARDIAC way in Cardigan
Both turn 999 into 996. The loop takes 20 cells and runs 155 instructions; the rearranged version takes 13 cells and runs 10.
From the tips in
cardiac-code on GitHub. All the tips.