home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / fibonacci < prev    next >
Lisp/Scheme  |  2020-01-01  |  1KB  |  39 lines

  1. ; From: Dat Thuc Nguyen
  2. ; Subject: Recursive and Iterative Fibonacci Functions
  3. ; Date: Wed, 26 Nov 2003 13:13:58 -0500
  4. ; URL: http://www.smalltickle.com
  5. ; Adapted and corrected from C-Kermit 8.0 Update Notes, Section 9.
  6. ; Reference: Harold Abelson, Structure and Interpretation of Computer Programs
  7. ;
  8. echo Recursive...
  9.  
  10. define FIBONACCI {                                          ; (1)
  11.    (if (== \%1 0) 0
  12.        (if (== \%1 1) 1 (+ (fibonacci (- \%1 2)) (fibonacci (- \%1 1)))))
  13. }
  14.  
  15. ; Time it on fibonacci(17):
  16.  
  17. .t1 := \v(ftime)
  18. (setq t1 \v(ftime))
  19. (setq result (fibonacci 17))
  20. (setq t2 (- \v(ftime) t1))
  21. echo FIBONACCI(17) = \m(result): TIME = \ffpround(t2,3)
  22.  
  23. echo Iterative...
  24.  
  25. define FIBITER {
  26.     (if (== \%3 0) (\%2) (FibIter (+ \%1 \%2) \%1 (- \%3 1)))
  27. }
  28. define FIBONACCI {                                          ; (2)
  29.     (fibiter 1 0 \%1)
  30. }
  31.  
  32. ; Time this one too.
  33.  
  34. .t1 := \v(ftime)
  35. (setq t1 \v(ftime))
  36. (setq result (fibonacci 17))
  37. (setq t2 (- \v(ftime) t1))
  38. echo FIBONACCI(17) = \m(result): TIME = \ffpround(t2,3)
  39.