From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d74d61525ce986e9 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-22 12:29:30 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.mathworks.com!wn13feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi_feed4!attbi.com!attbi_s52.POSTED!not-for-mail Message-ID: <3F96DA49.5020101@comcast.net> From: "Robert I. Eachus" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Implementing Memorize References: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.34.139.183 X-Complaints-To: abuse@comcast.net X-Trace: attbi_s52 1066850969 24.34.139.183 (Wed, 22 Oct 2003 19:29:29 GMT) NNTP-Posting-Date: Wed, 22 Oct 2003 19:29:29 GMT Organization: Comcast Online Date: Wed, 22 Oct 2003 19:29:30 GMT Xref: archiver1.google.com comp.lang.ada:1460 Date: 2003-10-22T19:29:30+00:00 List-Id: Lutz Donnerhacke wrote: > * Marius Amado Alves wrote: > >>>What's the correct way to implement it? >> >>Maybe with an access-to-subprogram type. > > > Of course this works. But I'm looking for a generic version. > > >>What problem are your trying to solve? > > > Calculating Fibbonacci numbers recursivly is easy but braindead. Languages > with access to the symbol table at execution time are able to change the the > reccuring call with a memorize wrapper which returns already computed values > immediatly instead of recalculating them. In the referenced article I wrote > an C-Implementation of such a wrapper. Now I'm looking for a more elegant > version in Ada. Calculating Fibonacci numbers recursively is definitely braindead. Try this approach instead. (Since I use Long_Float to represent values > Integer'Last, on GNAT you get an infinity above 1474. ;-) Since GNAT apparently does the powers of two approach for computing X**N, you can try timing this but it is really a waste of (your) time. ============================================================================== with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; procedure Fib is -- a program to compute Fibonacci numbers. Temp: Long_Float; Value: Integer; function Fibonacci(N: Natural) return Long_Float is Phi: constant := 1.61803_39887_49894_84820_45868_34365_63811_77203_09179_80576; Sqrt_5: constant := 2*Phi-1.0; -- 2.23606_79774_99789_6964... begin return Long_Float'Rounding(Phi**N/Sqrt_5); end Fibonacci; begin Put_Line("Enter a number, negative to exit. Above 46 returns Long_Float, above 1474, infinity."); loop Get(Value); if Value < 0 then return; end if; Temp := Fibonacci(Value); Put(" Fibonacci ("); Put(Value, 1); if Temp > Long_Float(Integer'Last) then Put(") is "); if Value > 73 then Put (" approximately"); end if; Put(Temp,2,14,2); else Put(") is "); Put(Integer(Temp), 1); end if; Put_Line("."); end loop; end Fib; -- Robert I. Eachus "Quality is the Buddha. Quality is scientific reality. Quality is the goal of Art. It remains to work these concepts into a practical, down-to-earth context, and for this there is nothing more practical or down-to-earth than what I have been talking about all along...the repair of an old motorcycle." -- from Zen and the Art of Motorcycle Maintenance by Robert Pirsig