Created
April 22, 2014 09:02
-
-
Save alejolp/11171006 to your computer and use it in GitHub Desktop.
This file contains 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
/* $ 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