Werner Holt

Ranch Hand

Posts: 37

posted 2 years ago

Strangely, the person who wrote this method was me, and it is working(it's purpose is to return the product of n odd numbers). My problem is, I do not understand how exactly works. What goes after what, how exactly the values are caluclated and multiplied and what happens in the return after n==1.

Sorry if I am asking for too much, but can someone explain this to me in detail?

I will be very grateful.

Werner

Strangely, the person who wrote this method was me, and it is working(it's purpose is to return the product of n odd numbers). My problem is, I do not understand how exactly works. What goes after what, how exactly the values are caluclated and multiplied and what happens in the return after n==1.

Sorry if I am asking for too much, but can someone explain this to me in detail?

I will be very grateful.

Werner

Piet Souris

Master Rancher

Posts: 2041

75

posted 2 years ago

- 1

hi Werner,

well, there is not so much to explain. Just write out in ful what happens

when you issue 'multiplyOdds(3)'.

When n = 3 we get

(since 3 != 1):

(2 * 3 - 1) * multiplyOdds(2) = 5 * multiplyOdds(2) =

(since 2 != 1):

5 * (2 * 2 - 1) * multiplyOdds(1) = 5 * 3 * multiplyOdds(1) =

(since n == 1):

5 * 3 * 1

= 15

Greetz,

Piet

well, there is not so much to explain. Just write out in ful what happens

when you issue 'multiplyOdds(3)'.

When n = 3 we get

(since 3 != 1):

(2 * 3 - 1) * multiplyOdds(2) = 5 * multiplyOdds(2) =

(since 2 != 1):

5 * (2 * 2 - 1) * multiplyOdds(1) = 5 * 3 * multiplyOdds(1) =

(since n == 1):

5 * 3 * 1

= 15

Greetz,

Piet

posted 2 years ago

- 2

When confronted with a sticky recursion problem I like to put print statements that give me an indented output. Something like this. This may or may not help you but it's a handy trick.

Output

Output

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Tushar Goel

Ranch Hand

Posts: 934

4