4 Replies - 3372 Views - Last Post: 08 January 2015 - 06:46 PM

#1 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Recursive Power Function

Posted 01 March 2012 - 08:12 PM

Description: Finds r^p recursively
double power(int r, int p) {
	if(p == 0) {
		return 1.0;
	}
	return r * power(r, p - 1);
}


Is This A Good Question/Topic? 0
  • +

Replies To: Recursive Power Function

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12492
  • View blog
  • Posts: 45,627
  • Joined: 27-December 08

Re: Recursive Power Function

Posted 09 March 2012 - 05:21 PM

Approved, but to note, the precondition is that p >= 0.
Was This Post Helpful? 0
  • +
  • -

#3 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Recursive Power Function

Posted 10 March 2012 - 01:00 PM

Um. no it's not. I suppose p <= 0 would also work
Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Recursive Power Function

Posted 10 March 2012 - 01:02 PM

I am trying to write "I supposed p less than or equal to 0 would also work"
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12492
  • View blog
  • Posts: 45,627
  • Joined: 27-December 08

Re: Recursive Power Function

Posted 08 January 2015 - 06:46 PM

If p < 0, this will recurse infinitely. So p >= 0 must hold for this code to terminate.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1