SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

CARDIAC › Tips and tricks › Digits

Divide by walking, and stop when the answer stops mattering

Say you want to know how many times 7 goes into 100, but anything over 3 just counts as 3, like a meter with three bars.

The straightforward way divides all the way, 14 rounds of subtracting, and then cuts the answer down to 3.

cell  contents  notes
 10      040    INP 40  cells 10-19: divide all the way: how many times d goes into x
 11      041    INP 41
 12      140    CLA 40
 13      741    SUB 41
 14      320    TAC 20
 15      640    STO 40
 16      142    CLA 42
 17      200    ADD 00
 18      642    STO 42
 19      812    JMP 12
 20      142    CLA 42  cells 20-26: then cut the answer down to 3
 21      743    SUB 43
 22      325    TAC 25
 23      143    CLA 43
 24      642    STO 42
 25      542    OUT 42
 26      900    HRS 00
 40      000
 41      000
 42      000    the count
 43      003

Step through the natural way in Cardigan

Since CARDIAC divides by walking, one subtraction at a time anyway, you can simply stop walking the moment the answer reaches 3. Everything after that would be work you throw away.

cell  contents  notes
 10      040    INP 40  cells 10-22: walk, and stop as soon as the count reaches 3
 11      041    INP 41
 12      140    CLA 40
 13      741    SUB 41
 14      321    TAC 21
 15      640    STO 40
 16      142    CLA 42
 17      200    ADD 00
 18      642    STO 42
 19      743    SUB 43
 20      312    TAC 12  under 3: keep walking
 21      542    OUT 42
 22      900    HRS 00
 40      000
 41      000
 42      000    the count
 43      003

Step through the CARDIAC way in Cardigan

Both print 3. Dividing all the way takes 21 cells and runs 124 instructions; stopping at 3 takes 17 cells and runs 31.

From the tips in cardiac-code on GitHub. All the tips.