Skip to content

Instantly share code, notes, and snippets.

@alejolp
Created April 22, 2014 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejolp/11171006 to your computer and use it in GitHub Desktop.
Save alejolp/11171006 to your computer and use it in GitHub Desktop.
/* $ g++ -Wall -std=c++0x pyt.cpp */
#include <iostream>
struct triple {
int x, y, z;
};
class pyt {
public:
pyt() : state_{1,1,1} {
next();
}
const triple& get() const { return state_; }
void next() {
do { inc(); }
while (!condition());
}
private:
void inc() {
if (++state_.y > state_.z) {
if (++state_.x > state_.z) {
++state_.z;
state_.x = 1;
}
state_.y = state_.x;
}
}
bool condition() const {
return state_.x * state_.x + state_.y * state_.y ==
state_.z * state_.z;
}
triple state_;
};
int main(int argc, char** argv) {
pyt p;
for (;;) {
const triple& t = p.get();
std::cout << t.x << ", " << t.y << ", " << t.z << std::endl;
p.next();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment