about summary refs log tree commit diff stats
path: root/forth/factorial.forth
diff options
context:
space:
mode:
Diffstat (limited to 'forth/factorial.forth')
-rw-r--r--forth/factorial.forth11
1 files changed, 11 insertions, 0 deletions
diff --git a/forth/factorial.forth b/forth/factorial.forth
new file mode 100644
index 0000000..359a642
--- /dev/null
+++ b/forth/factorial.forth
@@ -0,0 +1,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 .