Re: Recursive Functions

From: James Hu (jxh_at_despammed.com)
Date: 10/27/03


Date: Mon, 27 Oct 2003 12:34:19 -0600

On 2003-10-27, dmattis <dmattis@yahoo.com> 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?

This is not a C question... but the C answer is use pow() (unless you
are purposefully avoiding floating point, in which case a table lookup
is in order).

<DYOHW> Rewrite your word problem into a recurrence relationship. Use
induction to prove this recurrence correctly calculates x to the nth
power. It should be obvious at that point what your function should
look like and what your base case is. </DYOHW>

-- James



Relevant Pages

  • Re: Recursive Functions
    ... > Power, and multiplying by x again if n was odd, and to find a ... > suitable base case to stop the recursion. ...
    (comp.lang.c)
  • Re: Recursive Functions
    ... > Power, and multiplying by x again if n was odd, and to find a ... > suitable base case to stop the recursion. ...
    (comp.lang.c)
  • Re: Recursive Functions
    ... > Power, and multiplying by x again if n was odd, and to find a ... > suitable base case to stop the recursion. ...
    (comp.lang.c)
  • Re: Recursive Functions
    ... >> suitable base case to stop the recursion. ... dimensioned INT_MAX-INT_MIN+1 will take up a lot of memory. ... > induction to prove this recurrence correctly calculates x to the nth ...
    (comp.lang.c)
  • Re: Recursive Functions
    ... >> suitable base case to stop the recursion. ... which squares an expression through mutliplication. ...
    (comp.lang.c)