alternative of nested loop ????

these nested loops take a long time for compilation so i want the alternative and fast solution of this code

main() { int a,b,c; for(a=0;a<1000;a++) { for(b=0;b<1000;b++) { for(c=0;c<1000;c++) { cout<<"( "<<a<<" , "<<b<<" , "<<c<<" )"<<endl; if((a+b+c)==1000) { if(((a*a)+(b*b))==(c*c)) { cout<<"( "<<a<<" , "<<b<<" , "<<c<<" )"<<endl; getch(); } } } } } }eā€“

Looks like you are trying to print pythagorean triplets with sum less than 1000.

Remove the third loop. Also remove the first if condition. Iterate for only a and b and make c=1000-a-b.
This would require about (10^3)*(10^3) operations, and it would work fine.

1 Like