Subject: Re: iterative-version for computing a Fibonacci number
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 28 Jul 2006 21:34:37 -0500
Newsgroups: comp.lang.lisp
Message-ID: <ZdidndFHVtygVFfZnZ2dnUVZ_r6dnZ2d@speakeasy.net>
thebjorn <BjornSteinarFjeldPettersen@gmail.com> wrote:
+---------------
| >     > (defun sum-even-fibs (how-many)
| > 	    (let ((x (loop ... )))
| > 	      (ceiling (log x 10))))
...
| >     > (time (sum-even-fibs 100000))
| 
| I got floating point overflow here running on CLisp (it was the easiest
| accessible on xp at the time I downloaded it...) I'll look into CMUCL
| over the weekend.
+---------------

It will certainly run much faster on CMUCL. But try this variation
on CLISP in the meantime: Replace the (CEILING (LOG X 10)) in
SUM-EVEN-FIBS with (CEILING (/ (INTEGER-LENGTH X) (LOG 10d0 2d0))).
That should run without overflow on CLISP.

But note that the latter formula doesn't always give *quite* the
same results, e.g., (sum-even-fibs 100000) ==> 20899 as before, but
(sum-even-fibs 200000) ==> 41798, one more than before. At the cost
of a little more run-time (~0.1 s more, on CMUCL), you can get a
better count of "digits" with (LENGTH (FORMAT NIL "~d" X)), which
once again gives 20899 & 41797, respectively.


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607