Created
June 18, 2013 21:25
-
-
Save jmcph4/5809572 to your computer and use it in GitHub Desktop.
A small C++ program that generates pythagorean triplets, taking two integers as inputs. Compiled with GCC G++ 4.6.2.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <cstdlib> | |
| #include <iostream> | |
| using namespace std; | |
| int triplet(int m, int n) | |
| { | |
| int a = (m * m) - (n * n); | |
| int b = 2 * m * n; | |
| int c = (m * m) + (n * n); | |
| int triplet[3] = {a,b,c}; | |
| cout<<"A = ("<<triplet[0]<<","<<triplet[1]<<","<<triplet[2]<<")."<<endl; | |
| return 0; | |
| } | |
| int main(int argc, char** argv) | |
| { | |
| if(argc == 3) | |
| { | |
| int m = atoi(argv[1]); | |
| int n = atoi(argv[2]); | |
| if(m > n) | |
| { | |
| triplet(m, n); | |
| } | |
| else | |
| { | |
| cerr<<"m <= n"<<endl; | |
| } | |
| } | |
| else | |
| { | |
| cerr<<"Usage: pythagoras [m] [n]"<<endl<<endl<<"Creates a pythagorean triplet using Euclid's forumlae, such that m must be greater than n."<<endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment