home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / a293_1 / !AForth_Examples_Math < prev    next >
Encoding:
Text File  |  1993-04-13  |  613 b   |  30 lines

  1. \ Forth source file
  2. \ Copyright Mads Meisner-Jensen, 6 Apr 1993
  3. \ Math examples
  4.  
  5. \ Compute factorial of n. Note that n cannot be too large because RECURSEing
  6. \ is being used (data and/or return stack could overflow)
  7. : FACTORIAL1  ( +n1 -- n2 )
  8.   DUP 1 > IF DUP 1- RECURSE  * THEN
  9. ;
  10.  
  11. \ Compute factorial of n. This implementation is safe, as opposed to FACTORIAL1.
  12. : FACTORIAL2  ( +n1 -- n2 )
  13.   DUP 1 > IF
  14.     DUP
  15.     1 DO
  16.       I *
  17.     LOOP
  18.   THEN
  19. ;
  20.  
  21. \ Print the first n Fibonacci numbers. Note that n cannot be too large!
  22. : FIBONACCI  ( +n -- )
  23.   1 1 ROT  ( 1 1 +n )
  24.   0 DO
  25.     DUP .
  26.     TUCK +
  27.   LOOP
  28.   2DROP
  29. ;
  30.