Re: Recursive Functions
From: Richard Heathfield (dontmail_at_address.co.uk.invalid)
Date: 10/27/03
- Next message: Default User: "Re: Asking if elements in struct arre zero"
- Previous message: Kevin Goodsell: "Re: C or C++ first?"
- In reply to: dmattis: "Recursive Functions"
- Next in thread: Julian V. Noble: "Re: Recursive Functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 27 Oct 2003 18:51:28 +0000 (UTC)
dmattis wrote:
> I am trying to write a recursive version of Power(x,n) that works by
> breaking n down into halves(where half of n=n/2), squaring
> Power(x,n/2), and multiplying by x again if n was odd, and to find a
> suitable base case to stop the recursion. Can someone give me an
> example of this?
/*
untested,
unreadable,
unambitious,
unenthusiastic
*/
#define k \
unsigned char
k Power(k x,k
n){k r=1;if(n
){if(1==n){r=
x;}else{k t =
Power(x,n>>1)
;if(1&n){r=x;
}r *= t*t; }}
return r ;}
-- Richard Heathfield : binary@eton.powernet.co.uk "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999. C FAQ: http://www.eskimo.com/~scs/C-faq/top.html K&R answers, C books, etc: http://users.powernet.co.uk/eton
- Next message: Default User: "Re: Asking if elements in struct arre zero"
- Previous message: Kevin Goodsell: "Re: C or C++ first?"
- In reply to: dmattis: "Recursive Functions"
- Next in thread: Julian V. Noble: "Re: Recursive Functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|