Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Created June 18, 2013 21:25
Show Gist options
  • Select an option

  • Save jmcph4/5809572 to your computer and use it in GitHub Desktop.

Select an option

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.
#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