Path: utzoo!attcan!lsuc!maccs!rory From: rory@maccs.dcss.mcmaster.ca (Rory Jacobs) Newsgroups: comp.lang.c Subject: Re: # to the nth power Message-ID: <2730D2C2.13524@maccs.dcss.mcmaster.ca> Date: 2 Nov 90 01:58:25 GMT References: <15984@mentor.cc.purdue.edu-> <9750@helios.TAMU.EDU> <1990Nov1.232830.17131@NCoast.ORG> Organization: McMaster University, Hamilton, Ontario, Canada Lines: 38 In article <1990Nov1.232830.17131@NCoast.ORG> you write: >In article <9750@helios.TAMU.EDU> randy@cs.tamu.edu (Randy Hutson) writes: >>In article <15984@mentor.cc.purdue.edu-> edgincd2@mentor.cc.purdue.edu (Chris Edgington *Computer Science Major*) writes: >>->In article <90305.005050CJH101@psuvm.psu.edu->, CJH101@psuvm.psu.edu (Carl J. Hixon) writes: [ Lots of methods for computing a raised to the power n deleted ] Oh well, I *mailed* everything said in the above articles to the original poster. But the last one (iterative computation of a^n) makes me post the following version: int power2 (x,n) /* note that x, p, sqp if made type double this would allow * x^n for real numbers x and integers n. This just does integers * x raised to the intger powers */ int x; int n; { int p = 1.0; int sqp = x; int i=n; while (i>0) { if ((i % 2) == 1) p *= sqp; sqp *= sqp; i = i / 2; } return(p); } This calculates x^n and run in O(log2 n) time... Rory "Bug Slayer" Jacobs rory@maccs.dcss.mcmaster.ca