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. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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; }

12th Mar 2020, 10:07 AM
Hakan Nihat
Hakan Nihat - avatar
14 Answers
+ 3
Try it now
15th Mar 2020, 12:05 PM
John Wells
John Wells - avatar
+ 3
I updated mine to remove the for loop you used in main.
15th Mar 2020, 10:19 PM
John Wells
John Wells - avatar
+ 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
14th Mar 2020, 5:07 PM
John Wells
John Wells - avatar
+ 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.
15th Mar 2020, 1:13 PM
John Wells
John Wells - avatar
+ 1
Get rid of k and use N instead.
15th Mar 2020, 11:43 AM
John Wells
John Wells - avatar
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.
14th Mar 2020, 5:50 PM
Hakan Nihat
Hakan Nihat - avatar
0
Your code as written makes no sense. If I understood what you mean, maybe I can fix it.
14th Mar 2020, 7:00 PM
John Wells
John Wells - avatar
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.
14th Mar 2020, 9:32 PM
Hakan Nihat
Hakan Nihat - avatar
0
Okay my code just needed to check for c being an integer. Try it again.
14th Mar 2020, 10:50 PM
John Wells
John Wells - avatar
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.
15th Mar 2020, 11:40 AM
Hakan Nihat
Hakan Nihat - avatar
0
Now it didn't generate all of them 😔
15th Mar 2020, 12:01 PM
Hakan Nihat
Hakan Nihat - avatar
0
I can't see the Euclid's formula there. I must use it. 😔
15th Mar 2020, 12:53 PM
Hakan Nihat
Hakan Nihat - avatar
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; }
15th Mar 2020, 8:18 PM
Hakan Nihat
Hakan Nihat - avatar
0
Thanks!
15th Mar 2020, 10:21 PM
Hakan Nihat
Hakan Nihat - avatar