hi

pls help me with the pythogorean triplets code for numbers between 1 and 1000

Should work. I'm leaving to go home and won't both going through it.

#include <iostream>
#include <fstream>
using namespace std;
int main(){
int i=0,m=2,a,b,c,f=2;
while (i<1000){
a=2*m;
b=m*m-1;
c=m*m+1;
while (f<=a){
if (a%f!=0 and b%f!=0 and c%f!=0){
f++;}}
if (f=a+1){cout<<a<<", "<<b<<", "<<c;
i++;}}
system("pause");}

I just realized that that may not be what you wanted. Do you mean c^2<=100, c<1000, or the first 1000 triplets? :?:

That code wouldn't work at all anyway; the values of m, a, b, and c never change.

And it seems completely oblivious to triplets like 20,21,29.

You're right. I forgot about that. This should take everything into effect and list the first 1000 triplets. For some reason, it isn't working.

#include <iostream>
#include <fstream>
using namespace std;
int main(){
int a,b,c,d,i,m=2,n,nmax=1,count=0;
while (count<1000){
n=1;
while (n<nmax){
nmax=m-1;
i=2;
a=2*m*n;
b=m*m-n;
c=m*m+n;
while (i<a){
if (a%i==0 and b&i==0 and c&i==0){
cout<<"NO MATCH";}
else {i++;}}
if (i==a){
if (b%i==0 and c%i==0){cout<<"NO MATCH";}
else {cout<<a<<b<<c;count++;}}
n++;}
m++;}
system("pause");}
Be a part of the DaniWeb community

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