• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursion

 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all !
This program calculates the power of a number using recursion.
if i set the initial value of ans(the local variable in the method power)
other than 1 , things get messed up why???
One other thing.
I'm using the following stmt
ans=base1*power(base1,power1-1);
But even if i use
ans*=base1*power(base1,power1-1);
I get same answer.

But i guess the best approach is to simply return the expresion
return base1*power(base1,power1-1);

Thanks in advance
--Danish

[ November 03, 2002: Message edited by: Danish Shaukat ]
[ November 03, 2002: Message edited by: Danish Shaukat ]
[ November 03, 2002: Message edited by: Danish Shaukat ]
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recursion can be a powerful tool if left in the hands of someone who understands Machiavellistic principles: divide and conquer. If we apply these nice little principles to your nice little problem, we might notice the following:
the problem is: find pow(x, y) where both x and y are integral numbers. The following simple statements are considered to be true:
- if y < 0 we consider the answer to be 0
- else if y == 0 we consider the answer to be 1
- else if y == 1 the anser will be x
- else we (ab)use some Poor Old Wanderer (pow) to do some calculations for us: z= pow(x, y/2). If y is even, pow(x, y) equals z*z, else (if y is odd), pow(x, y) equals x*z*z.
The notions above allow us to implement the pow(x, y) function:

kind regards
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and you may want to consider that 0 raised to a negative power is undefined (and so perhaps throw an ArithmeticException in that case).
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ditto for 0 to the power 0.
As x approaches 0, x^0 approaches 1 but 0^x approaches 0. So 0^0 has to be considered undefined.
[ November 03, 2002: Message edited by: Ron Newman ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic