Skip to content

Instantly share code, notes, and snippets.

@h1g0
Created March 6, 2014 06:30
Show Gist options
  • Save h1g0/9383592 to your computer and use it in GitHub Desktop.
Save h1g0/9383592 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <boost/timer.hpp>
/*###########################################
*入力された値がピタゴラス数かどうかを判定
###########################################*/
int main(){
int z,z2;
std::cout << "X^2 + Y^2 = Z^2" <<std::endl;
std::cout << "Input Z?" << std::endl;
std::cin >> z;
if(z<1){
std::cout << "An unexpected input has been detected." << std::endl;
std::cout << "Please input an unsigned integer." << std::endl;
return -1; //異常終了
}
z2 = z * z;
int counter = 0;
boost::timer t;
for(int x = 1; x * x < z2 >> 1; x++){
int x2 = x * x;
for(int y = x; y * y <= z2 - x2; y++){
int y2 = y * y;
if(x2 + y2 == z2){
std::cout << x << "^2 + " << y << "^2 = " << z << "^2" << std::endl;
counter++;
}
}
}
std::cout << "Done. " << counter << "pattern(s) found.["
<< t.elapsed() << "sec.]" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment