syiannop
syiannop

Reputation: 87

Java Beginner - Mistake on a simple code

is my first time here so i dont know exactly how it works, so sorry for the mistakes.

What's the result from this function, when we give as "aktueller Parameter" the number 3?

(The original text:

Welches Ergebnis liefert diese Methode, wenn bei einem Aufruf als aktueller Parameter der Wert 3 übergeben wird?

Im studying in German, so i dont really know the english terms :/ )

public int m(int p)
{
   int result;
   if (p == 0)
   {
    result = 0;
   }
   else
   {
    result = 3*p + m(p-1);
   }
    return result;
   }

I have already tried it and the answer is 18, but when im trying to do it without any program the answer i find is 15:

result = 3 * 3 + 3(3-1);

Can someone please explain me why is 18 and not 15? Im assuming that i am making something wrong.

Thank you in advance.

Upvotes: 2

Views: 127

Answers (4)

B.K.
B.K.

Reputation: 10152

                           // Original argument is 3
3*3 + m(3-1)               // Step #1  New argument is 3 - 1
3*2 + m(2-1)               // Step #2  New argument is 2 - 1
3*1 + m(1-1)               // Step #3  New argument is 1 - 1

                           // p == 0   Return 0

3*1 + 0 = 3                // Step #4  Return 3
3*2 + 3 = 6 + 3 = 9        // Step #5  Return 9
3*3 + 9 = 9 + 9 = 18       // Step #6  Return 18, which is the final value

Here is how I like to work on recursive functions, and it a personal preference. Start at the top, and put together the known values, then write the function on the side, in this case we simply add it. Then, on the next line, write the known values again (this time, one of them is one less (2, rather than 3), and write the function with the new arguments to the right of it (we're once again adding), and so on. I work from top to bottom.

I then show the results on the right side and work back up. So, I don't write the results right away.

Upvotes: 0

Kick
Kick

Reputation: 4923

Due to recursive code.Please c the strack trace

When you pass the p value as 3,the function interact as below :

 result = 3*p + m(p-1);        // 3*3 + m(2)  = 9
                               // 3*2 + m(1)  = 6
                               // 3*1 + m(0)  = 3 
                                             -------
                                               18

Upvotes: 0

Embattled Swag
Embattled Swag

Reputation: 1469

When you see m(p-1), that means that you're calling the function m again from inside itself, which is called recursion.

Essentially, the arithmetic it's doing is 3*3+3*2+3*1 = 18.

Upvotes: 2

rgettman
rgettman

Reputation: 178263

Let's break down this recursive call:

With m(3), p isn't 0, so we return 3*3 + m(2);.

3*3 + (m(2))

With m(2), p isn't 0, so we return 3*2 + m(1);.

3*3 + (3*2 + m(1))

With m(1), p isn't 0, so we return 3*1 + m(0);.

3*3 + (3*2 + (3*1 + m(0))

With m(0), p is 0, so we return 0. Then the recursive call stack unwinds.

3*3 + (3*2 + (3*1 + (0)) =
9 + (6 + (3 + 0)) = 
9 + (6 + 3) = 
9 + 9 = 
18

Upvotes: 9

Related Questions