So i've been attempting this problem for the last few days and have no luck. I am tasked to find square pairs from 1 to x.
Num1 + Num2 = a perfect square (i.e. 2 + 2 = 4. 16 + 20 = 36)
Num2 - Num1 = a perfect square. (i.e. 2 - 2 = 0. 20 - 16 = 4)
I've been getting closer to a result, but for the life of me can not figure out whats going wrong in my loops. for example: this is my latest approach:
Function to test if a number is a perfect square:
bool isSquare(int num){
if(num < 0)
return false;
int root = round(sqrt(num));
return num == root * root;
}
main:
int num1 = 1; num2 = 2;
int tempP, tempM;
for(int i = 1; i <= number; i++){
for(int j = 1; j <= num1; j++){
tempP = num1 + num2;
tempM = num2 - num1;
if(isSquare(tempP) && isSquare(tempM)){
cout << num1 << "\t" << num2 << "\t" << tempP << "\t" << tempM << endl;
}
num2++;
}
num1++;
}
for some reason my output (regardless of how big 'int number' is) is limited to one row. My other tests(such as having the second loop go until j <= number) end with my num1s repeating themselves, num2s going past number, and printing every number until it stops.
I have no idea where to go next, any pointers would be helpful.
Thank you all
EDIT: expected output of 12:
N P N + P P – N
2 2 4 0
4 5 9 1
6 10 16 4
8 8 16 0
8 17 25 9
10 26 36 16
12 13 25 1
12 37 49 25
Actual output of 12:
N P N + P P – N
2 2 4 0