Simplex v.0.5, 12 bytes
(The Docs page may be outdated; mainly, the *
also increments the pointer and the J
being the max of two elements.)
h*M{*LTRpM}]
This defines a macro that performs the factorial function on the current byte. It maintains the structure of the strip, but inserts an extra 0
at the next byte. You can delete this by adding another command p
before the function ends.
This one works for inputs on a strip whose sole member is the input. I.e., a strip which looks like [N,/,/,...]
(/
is the empty or null bit.) It clocks in at…
11 Bytes!!
This beats the GolfScript entry, FYI.
h{*M}pwT1J]
This is what it does:
h{*M}pwT1J]
h ] ~~ define new macro
{ } ~~ repeat inside until zero met
* ~~ copy the current byte and increment pointer
M ~~ decrement byte
p ~~ remove trailing zero
wT ~~ spreads T (multiplication) across strip backwards; sets pointer to after the result
1J ~~ Takes the maximum of 1 and the current byte
Here the non-destructive version being used in an example code:
h*M{*LTRpM}p]ih0o
This defines the macro, asks for numeric input (i
), calls the first macro (h0
) and outputs the byte as a number (o
).
Here is the pseudo-code I used:
Function factorial(N)
A = N - 1
While A > 1
N = A * N
A = A - 1
End While
Return N
End Function
This is the expanded explanation.
h ~~ open macro, implicit [
* ~~ A=N [N,A]
M ~~ A=N-1 [N,A-1]
{ ~~ Loop until current byte is zero
* ~~ [N,A-1,A-1]
LT ~~ [N*(A-1),0,A-1]
Rp ~~ [N*(A-1),A-1]
M ~~ [N*(A-1),A-2]
}
p ~~ [N!]
] ~~ close macro