🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.pascal.borland
Article #171

Re: Fibonacci Numbers program problem

#171
From: Osmo Ronkanen
Date: Thu, 10 Aug 2000 00:00
45 lines
2656 bytes
In article <xAWw8HAhApk5EwqQ@merlyn.demon.co.uk>,
Dr John Stockton  <jrs@merlyn.demon.co.uk> wrote:
>
>Where a method is defined in a recursive manner, it is better to program
>it recursively, and then to seek improvement if necessary; if the
>iterative result does not agree with the recursive, one can be sure that
>something is wrong.

I disagree. It is better to use brains before one starts coding. Such
methods might leave very poorly performing code that maybe survives
the testing but in actual use cause problems.

There are few considerations on whether to use recursion: 1) does the
recursion cut the problem in half and 2) does the recursive steps store
meaningful info that would have to be stored otherwise. If the answer
is no to9 both then it is best to avoid recursion.

>Fibonacci is a bad example, in fact, since to the mildly experienced the
>iterative and recursive approaches are both obvious; probably it should
>only be used in teaching if both solutions are involved.
>
>Neither of the two functions, as written, is good for producing the
>results of your "for" loops, or for filling an array, since in each case
>(although to a different extent) the same thing is calculated
>repeatedly.

But that meaningless on any data that might be calculated with
longints. 

If one wants to fill an array, then recursion might be a good choice:

var fibarr:array[1..46] of longint;

Function Fibd(n:integer):longint;
Begin
  if fibarr[n]=0 then
    if n<3 then fibarr[n]:=1
           else Fibarr[n]:=fibd(n-1)+fibd(n-2);
  fibd:=fibarr[n];
End;

(assumes the array has been initialized to zeros and no value greater
than 46 is used as parameter.

Osmo
Technical Details
Message-ID: <8mut47$fb3$1@kruuna.Helsinki.FI>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <EZKj5.95$S96.740@news-server.bigpond.net.au> <MPG.13fa0aec45dda57698970f@news.primenet.com> <8ms9f2$jfg$1@kruuna.Helsinki.FI> <xAWw8HAhApk5EwqQ@merlyn.demon.co.uk>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 108ebd,99aae379fca00543
X-Google-Attributes: gid108ebd,public
X-Google-Thread: 111659,99aae379fca00543
X-Google-Attributes: gid111659,public
X-Google-Thread: 10cc59,99aae379fca00543
X-Google-Attributes: gid10cc59,public
From: ronkanen@cc.helsinki.fi (Osmo Ronkanen)
Subject: Re: Fibonacci Numbers program problem
Date: 2000/08/10
Message-ID: <8mut47$fb3$1@kruuna.Helsinki.FI>
X-Deja-AN: 656863050
References: <EZKj5.95$S96.740@news-server.bigpond.net.au> <MPG.13fa0aec45dda57698970f@news.primenet.com> <8ms9f2$jfg$1@kruuna.Helsinki.FI> <xAWw8HAhApk5EwqQ@merlyn.demon.co.uk>
X-Complaints-To: usenet@news.helsinki.fi
X-Trace: oravannahka.helsinki.fi 965933001 1476 128.214.205.14 (10 Aug 2000 18:43:21 GMT)
Organization: University of Helsinki
NNTP-Posting-Date: 10 Aug 2000 18:43:21 GMT
Newsgroups: comp.lang.pascal.borland,comp.lang.pascal,borland.public.turbopascal