The task is to use a recursive algorithm to determine the sum of two numbers n1 and n2.

I just keep on returning to using the formula n1*n1 +n2*n2

The part which I have questions is I don't get how to do this in a recursive way.

Thanks!

flyingcurry
0
Light Poster

The task is to use a recursive algorithm to determine the sum of two numbers n1 and n2.

I just keep on returning to using the formula n1*n1 +n2*n2

The part which I have questions is I don't get how to do this in a recursive way.

Thanks!

Jump to PostJust by looking at the question, why would you want to do recursion for that? It would be much easier just to do in a method using Math.pow and adding the results.

Jump to PostThere are 2 key ideas in recursive algorithms. The first is the problem must be something that can be broken down into simpler steps in an iterative manner. The second is the iteration must have a well defined stopping point. Factorial is a common example because it has a "natural" …

cale.macdonald
4
Junior Poster

kramerd
29
Posting Pro in Training

There are 2 key ideas in recursive algorithms. The first is the problem must be something that can be broken down into simpler steps in an iterative manner. The second is the iteration must have a well defined stopping point. Factorial is a common example because it has a "natural" recursive solution. I.e., 5! = 5 * 4!, 4! = 4 * 3!, 3! = 3 * 2!, 2! = 2 * 1!, 1! = 1. So breaking the problem into simpler steps becomes n! = n * (n-1)!, and the stopping point is 1! = 1.

Understanding that recursive solutions are not particularly efficient (usually a loop performs better, at least in Java), both multiplication and addition problems can have recursive solutions. You just have to think about a simpler operation.

Multiplication can be accomplished by repeating addition, and Addition can be solved by repeatedly adding 1. The latter doesn't eliminate the addition operation, but it is simpler to just add 1, and this can be done using recursion.

flyingcurry
0
Light Poster

Just by looking at the question, why would you want to do recursion for that?

You just have to think about a simpler operation.

Thanks! It turns out that the question meant add all the sums of all the squares of the numbers from n1 to n2, so n1=2 n2=4 would give 29 as 4+9+16=29

Below is my code, it works fine:)

```
public static int sumSquares (int n1, int n2)
{
if (n1==n2)
return n1*n2;
return n1* n1 + sumSquares(n1+1, n2);
}
```

Edited
by flyingcurry because:
*
n/a *

JamesCherrill
4,583
Most Valuable Poster
Moderator
Featured Poster

This code will crash and burn horribly if presented with parameters like (3,2) or, more subtly, (-2,-3).

A simple replacement of the == on line 4 with a >= will protect against these nastys, as will the even simpler replacement of lines 4,5 with

if (n1>n2) return 0;

flyingcurry
0
Light Poster

This code will crash and burn horribly if presented with parameters like (3,2) or, more subtly, (-2,-3).

A simple replacement of the == on line 4 with a >= will protect against these nastys, as will the even simpler replacement of lines 4,5 with

if (n1>n2) return 0;

Thanks, it did crash and burn.

So I changed my code

```
public static int sumSquares (int m, int n)
{
if (m==n)
return m*n;
if (m>n)
return 0;
return m* m + sumSquares(m+1, n);
}
```

JamesCherrill
4,583
Most Valuable Poster
Moderator
Featured Poster

Edited
by JamesCherrill because:
*
n/a *

flyingcurry
0
Light Poster

What does it mean by recurse one more time? Because when i take out line 4,5 and do >= for line 6, it just gives me 0 even if the two numbers are equal, and for some of the numbers like m=1, n=2, it gives me wrong results like three.

JamesCherrill
4,583
Most Valuable Poster
Moderator
Featured Poster

Oh, very sorry, small mistake in my last post. It was right with just the > on line 6. This version is short & works:

```
public static int sumSquares (int m, int n) {
if (m>n) return 0;
return m* m + sumSquares(m+1, n);
}
```

My apologies.

flyingcurry
0
Light Poster

Oh, very sorry, small mistake in my last post. It was right with just the > on line 6. This version is short & works:

`public static int sumSquares (int m, int n) { if (m>n) return 0; return m* m + sumSquares(m+1, n); }`

My apologies.

Thanks! I see, so it works even if m and n are equal, it does the method again and does m*m+ sumSquares(m+1,n), which in this case will just give 0, so we get m*m.

The code is much simpler and the time complexity is more efficient!

Edited
by flyingcurry because:
*
n/a *

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.

Broken Link

**You're trying to visit a URL that doesn't currently exist on the web.**
Most likely, a member posted a link a long time ago to a web page that has since been removed.
It's also possible that there was a typo when posting the URL.
We redirect you to this notice instead of stripping out the link to preserve the integrity of the post.