In Project Euler’s problem 9, I encounter a problem: infinite loops.
Here is my code:
#include <iostream>
#include <cmath>
bool isPythagorean(int a, int b, int c);
int main(){
int a;
int aa;
int b;
int bb;
int c;
for(a = 0; a <= 1000; a++){ /*a loop*/
aa = a;
for(b = aa; b <= 1000; b++){ /*b loop*/
bb = b;
for(c = bb; c <= 1000; c++){
if(isPythagorean(a,b,c)){
if(a + b + c == 1000){
std::cout << (a * b) * c;
return 0;
}
else
continue;
}
}
}
return 1;
}
bool isPythagorean(int a, int b, int c){
int Pa = (int) pow(a, 2);
int Pb = (int) pow(b, 2);
int Pc = (int) pow(c, 2);
if(a < b && b < c){
if(Pa + Pb == Pc)
return true;
else
return false;
}
else
return false;
}
Courtesy of everyone who has helped the idiot writing this, the code has been changed, but the error still stands:
When the code is run, nothing is output to the terminal. Could anyone kindly tell me what is going wrong here?
(I am such an idiot; My thanks go to everyone that is even looking at this.)
Thank you, istrandjev for noticing a whole host of bad pieces of code.
Thank you, Blastfurnace for noticing that stupid error.
I don’t get much of the logic you are trying to apply. What is a%1==0&&b%1==0&c%1==0 checking? You can simply write it if(true), you know. Also when is the cycle supposed to end? How is a triplet supposed to be Pythagorean if one of the conditions is a > c and then you want to have a*a + b*b == c*c?