• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursion, a problem with a repeated final output  RSS feed

 
Wisam Siya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, So i created a method that basically similar to Math.pow, but using recursion.
However, it works fine except it prints the final result twice, how do i get rid of the last repeated number ?

here is my method :-



-------------------------------------------------------------------------

for example if the base = 3, and the degree = 4
the outputs would be :



how do i get rid of that last repeated 81 ?
 
Wisam Siya
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah now it looks easier to read thanks, and I am sorry for not using the code tags. it's my first time posting on this website.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Wisam.

I don't think the problem is in the recursion. For instance, this worked for me:

 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't even know whether there's a problem at all. I don't see any code which uses the result of the recursion() method, not at the top level anyway. Something which says "if (recursion(3, 4) == 81)" would help, or anyway something which tests if the code returns the correct value.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And your recursion method constructed a bit in a non standard way.
A recursive function normally has two parts:
1st - stopping condition - processing the case where the recursive function does not call itself.
2nd - recursive step - processing the case where the recursive function calls itself.

Your way I found it a bit difficult to read (i might wrong).
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Wisam,

did you, by chance, issue:
(see Knute's listing). Only in that case do I get this double 81.

Greetz,
Piet
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And again, I don't see why outputting 81 twice is a problem. As far as I can see it just tells you that the last step involves multiplying by 1, which is exactly what you would expect from that algorithm.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again

You know that recursive power method runs in linear time? Did you know there is a version of a power function taking the exponent as a non‑negative whole number which runs in logarithmic time?
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that log-method also suffer from a repeated last number?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not that I know of.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!