Need explanation to the function isVesuvian()

Hello guys,

I have this question:
A number n is vesuvian if it is the sum of two different pairs of squares. For example, 50 is
vesuvian because 50 == 25 + 25 and 1 + 49. The numbers 65 (1+64, 16+49) and 85 (4+81,
36+49) are also vesuvian. 789 of the first 10,000 integers are vesuvian.
Write a function named isVesuvian that returns 1 if its parameter is a vesuvian number, otherwise
it returns 0. Hint: be sure to verify that your function detects that 50 is a vesuvian number!

I also have a function that works!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int isVesuvian(int n)
{ 
	int max = sqrt(n);
	int count = 0;
	int tmp = 0;
 
	for (int i = 1; i<max; i++) 
	{
		for (int j = 2; j<=max; j++) 
		{
			if((i*i + j*j) == n && j != tmp)
			{
				tmp = i;
				
				count ++;
			}
		}
	}
	if (count == 2)
	{
		return 1;
	}
	return 0;
}


While I understand the question I really can't fully comprehend the solution herein (for instance I don't understand the role of the tmp variable, etc). Can someone help me with an explanation? (adding comments to the various statements would be nice)

Pls note that I don't want a different solution to this problem, unless something simple without any complex coding and structures. Thank you.
Last edited on
Why do you think the function works?
Run it with a main that passes it numbers from 1 to 10000.
You say there should be 789 vesuvian numbers.
But your algorithm says there are only 375.

(It could also be made more efficient since there's no need for an inner loop.)
Last edited on
Topic archived. No new replies allowed.