home *** CD-ROM | disk | FTP | other *** search
- \ Forth source file
- \ Copyright Mads Meisner-Jensen, 6 Apr 1993
- \ Math examples
-
- \ Compute factorial of n. Note that n cannot be too large because RECURSEing
- \ is being used (data and/or return stack could overflow)
- : FACTORIAL1 ( +n1 -- n2 )
- DUP 1 > IF DUP 1- RECURSE * THEN
- ;
-
- \ Compute factorial of n. This implementation is safe, as opposed to FACTORIAL1.
- : FACTORIAL2 ( +n1 -- n2 )
- DUP 1 > IF
- DUP
- 1 DO
- I *
- LOOP
- THEN
- ;
-
- \ Print the first n Fibonacci numbers. Note that n cannot be too large!
- : FIBONACCI ( +n -- )
- 1 1 ROT ( 1 1 +n )
- 0 DO
- DUP .
- TUCK +
- LOOP
- 2DROP
- ;
-