0
How can i write a porgram to find all Pythagorean triples (a^2+b^2=c^2) recursively? And c has to be less than a given int N.
#include <iostream> using namespace std; void Pit_rek(int N,int k) { int a,b,c; if (k) //ako k>010 { a=2k+1; b=2k*(k+1); c=b+1; if(c<N) {cout<<"Za k = "<<k<<"; a = "<<a<<"; b = "<<b<<"; c = "<<c<<endl;} Pit_rek(N,k-1); } } int main() { int N,k; cout<<"Vavedi chisloto k: "<<endl; cin>>k; cout<<"Vavedi gorna granica za c (N): "<<endl; cin>>N; Pit_rek(N,k); system ("pause"); return 0; }
14 Answers
+ 3
Try it now
+ 3
I updated mine to remove the for loop you used in main.
+ 2
I really change your code. I assumed k is maximum a and b values with one being their minimum.
https://code.sololearn.com/cpw9QS5qSGm6
+ 2
Line 16 second half of if. a^2 is a*a C++ does not support ^ operator. Other choice is pow(a, 2) but it is much slower.
+ 1
Get rid of k and use N instead.
0
This is not the code that i ask for, but thanks a lot for your help Mr. Wells. I need a program that can find all Pythagorean triples integer numbers recursively and c<N.
0
Your code as written makes no sense. If I understood what you mean, maybe I can fix it.
0
I need a code that generates all Pythagorean triples, recursively (a,b, c-hypotenuse ,c<N).That's all. I know that my code isn't true, but i don't know how to generate them recursively. Please,help me.
0
Okay my code just needed to check for c being an integer. Try it again.
0
It works! But i need a program where we using Euclid's formula for Phytagorean triplet without inputing k. I should change m and n values depending on the value of N.
0
Now it didn't generate all of them đ
0
I can't see the Euclid's formula there. I must use it. đ
0
Finally i did it:
#include <iostream>
#include <cmath>
using namespace std;
void Pit_rek(int N,int m,int n)
{ int a,b,c;
if (m)
{ a=m*m-n*n;
b=2*m*n;
c=m*m+n*n;
if(c<N && pow(a,2)+pow(b,2)==pow(c,2))
{cout<<" a = "<<a<<"; b = "<<b<<"; c = "<<c<<endl;}
if(m-1>n)
{Pit_rek(N,m-1, n);}}
}
int main()
{ int N;
cout<<"Vavedi gorna granica za c (N): "<<endl;
cin>>N;
for(int n=1;n<sqrt(N);n++)
{Pit_rek(N,sqrt(N),n);}
system ("pause");
return 0;
}
0
Thanks!