HP‑41C series, 18 B
The \$x\$ in \$x! = n\$ can be determined by repeatedly dividing \$n\$ by an incrementing divisor (first \$1\$, then \$2\$, then \$3\$ and so forth) until the quotient equals the (next) divisor.
The program works correctly up to and including \$x = 39\$.
01♦LBL "S" 5 Bytes global label requires 4 + (length of string) Bytes
NULL 1 Byte invisible Null byte before numbers
02 1 1 Byte place 1 on top of stack; X ≔ 1 ; Y ≔ 𝘯
03 LBL 00 2 Bytes mark head of this loop with (local) label 00 ──┐
│
04 X=Y? 1 Byte if X = Y ──┐ │
05 RTN 1 Byte └── then return │
│
06 ∕ 1 Byte lastX ≔ X ; X ≔ Y ∕ X │
↑
07 LASTX 1 Byte put lastX back on top of stack again │
NULL 1 Byte invisible Null byte before numbers │
08 1 1 Byte place 1 on top of stack │
09 + 1 Byte sum X and Y, stack drops by one item │
│
10 GTO 00 2 Bytes go to (local) label 00 ───────────────────────┘
Since this is pretty abstract, let’s consider a trace of the stack in the case \$n = 6 = 3! = x!\$.
The step “sees” data from one row above, the result of a step follows in the same row.
Empty cells are not relevant to the program.
step |
X |
Y |
Z |
T |
L astX |
|
6 |
|
|
|
|
1 |
1 |
6 |
|
|
|
X=Y? |
〃 |
〃 |
|
|
|
∕ |
6 |
|
|
|
1 |
LASTX |
1 |
6 |
|
|
〃 |
1 |
1 |
1 |
6 |
|
〃 |
+ |
2 |
6 |
|
|
1 |
GTO 00 |
〃 |
〃 |
|
|
〃 |
X=Y? |
〃 |
〃 |
|
|
〃 |
∕ |
3 |
|
|
|
2 |
LASTX |
2 |
3 |
|
|
〃 |
1 |
1 |
2 |
3 |
|
〃 |
+ |
3 |
3 |
|
|
1 |
X=Y? |
〃 |
〃 |
|
|
〃 |
RTN |
〃 |
〃 |
|
|
〃 |
Note that GTO
searches for a local label downward in program memory and wraps to the beginning of the currently executed program.
To inhibit local label search, append an END
command (3 Bytes).
The above program presumes there are no other programs (following) in program memory, so the permanent (= always there) END
is sufficient.
x
andn
are positive integers. As such646077305624121491462330357080396430806673805704796612248389053020040737981389397373513335318926846519441974218777961448245634895440330929720840926954349439434654453860427703550673839109903970520283495061590634864022312082259902655711571689179112428197039756156051147969300077437438615382409042832551650139224687809841080780412598454920634889005911333104355143592477664451230317936640000000000000000000000000000000000000000000000000000000
is a perfectly valid value for n :-) \$\endgroup\$