| Deutsch English Français Italiano |
|
<103i9cl$37hl9$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: iterative-version for computing a Fibonacci number
Date: Thu, 26 Jun 2025 01:56:40 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <103i9cl$37hl9$1@dont-email.me>
References: <vbc9l4$bfnc$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Thu, 26 Jun 2025 03:56:41 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="e164c7e8c4615b560d0a58ee26f330d2";
logging-data="3393193"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19dvb5LktERbP+nQnmZiLVJ"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:lzibptl2HW8xI3yE+WG3CrVUAyM=
B. Pym wrote:
> Raffael Cavallaro wrote:
>
> > > Hi, perhaps someone can help me translate my Python version?
> >
> > (defun fib (x)
> > (do ((a 0 b) ;; do loop variable a is initially 0, then b
> > (b 1 (+ a b)) ;;loop variable b is initially 1 then a + b
> > (i 1 (1+ i))) ;;loop index incremented by 1 each go round
> > ((> i x) a))) ;;termination condition - when index passes x stop
> > ;; and return a
> >
> > (defun fib-evens (limit)
> > "find the sum of all the even fibonacci numbers less than 1 million"
> > (loop for i from 1 ;;loop index starts at 1 implicitly incremented by 1
> > as current = (fib i) ;;compute fib of the current index
> > while (< current limit) ;;stop if index exceeds limit
> > when (evenp current) sum current)) ;;sum all the even fibs and
> > return this sum
> >
> > CL-USER> (time (fib-evens 1000000)) ;; time the sum of all the even
> > fibs less than a million
> > Evaluation took:
> > 0.0 seconds of real time
> > 8.e-6 seconds of user run time ;;; took about one
> > onehundredthousandth of a second
> > 1.e-6 seconds of system run time
> > 0 page faults and
> > 0 bytes consed.
> > 1089154 ;; here's your answer
It seems that in CL (COBOL-Like), one has to use a macro whose
source measures more than 60 kilobytes.
In a Lispy language, one can program a much shorter solution by
simply using recursion.
Gauche Scheme
(define (sum-even-fibs limit)
(let go ((a 0) (b 1))
(if (>= b limit)
0
(+ (if (even? b) b 0) (go b (+ a b))))))
(sum-even-fibs 999999)
===>
1089154