Path: ns-mx!uunet!usc!wupost!darwin.sura.net!Sirius.dfn.de!chx400!sicsun!disuns2!eldi.epfl.ch From: DINARICH@eldi.epfl.ch (Mirko Dinarich) Newsgroups: comp.sys.apple2 Subject: Re: TML Pascal II... Message-ID: <3335@disuns2.epfl.ch> Date: 3 Mar 92 11:41:55 GMT References: <1992Mar3.021749.11353@nuscc.nus.sg> Sender: news@disuns2.epfl.ch Lines: 73 Nntp-Posting-Host: eldi.epfl.ch X-News-Reader: VMS NEWS 1.20 In-Reply-To: ltchean@iss.nus.sg's message of 3 Mar 92 02:17:49 GMT In <1992Mar3.021749.11353@nuscc.nus.sg> ltchean@iss.nus.sg writes: > So without this operator, the new algorithm is sooooo much slower. Or somebody > else can provide me with a short algorithm that can handle Power(x, y: integer): longInt efficiently? Hello A friend of mine writes this function: FUNCTION Power (x : LONGINT; y : INTEGER) : LONGINT; VAR r : LONGINT; BEGIN (* Power *) r := 1; WHILE y>0 DO BEGIN IF odd(y) THEN r := r * x; (* or Mult (r, x) *) y := y DIV 2; x := x * x (* or Mult (x, x) *) END; Power := r END; (* Power *) If the multiplication function is slow you can utilise this one: ---------------------------------------------------------------- FUNCTION Mult (x : LONGINT; y : LONGINT) : LONGINT; VAR r : LONGINT; sign : BOOLEAN; BEGIN (* Mult *) r := 0; sign := y<0; IF sign THEN y := -y; WHILE y>0 DO BEGIN IF odd(y) THEN r := r + x; y := y DIV 2; x := x * 2 (* or x + x if faster *) END; IF sign THEN Mult := -r ELSE Mult := r END; (* Mult *) /------------------------------------------------------------------------------\ ! ! ! Mirko Dinarich e.mail dinarich@eldi.epfl.ch ! ! ! ! mail Mirko Dinarich ! ! Via San Nicolao 2 ! ! CH - 6816 Bissone ! ! Switzerland ! ! ! \------------------------------------------------------------------------------/