about summary refs log tree commit diff stats
path: root/forth/factorial.forth
blob: 359a642550d5864ac6ca2ffcf97e25dac60b7a0c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
( n -- n! )
: FACTORIAL
    \ If n is 0, the loop won't run and the initial 1 is returned.
    1 SWAP            \ Put initial result 1 on stack, ( 1 n )
    1+ 1              \ Setup loop bounds, ( 1 n+1 1 )
    DO
        I * \ Multiply accumulator by loop index
    LOOP ;

5 FACTORIAL .
10 FACTORIAL .