Skip to content

Instantly share code, notes, and snippets.

@nabijaczleweli
Forked from skorezore/nick.cpp
Last active December 2, 2015 19:21
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 nabijaczleweli/0f4c2f74ae5744e4f18d to your computer and use it in GitHub Desktop.
Save nabijaczleweli/0f4c2f74ae5744e4f18d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <ctime>
int main() {
const auto pentagonal_function = [](double n) -> double { return (n * ((3 * n) - 1)) / 2; };
const auto hexagonal_function = [](double n) -> double { return n * ((2 * n) - 1); };
auto pent_index = 0u;
auto hex_index = 0u;
auto solution_count = 1u;
std::clock_t start = std::clock();
while(true) {
const auto pent = pentagonal_function(pent_index);
const auto hex = hexagonal_function(hex_index);
if(pent < hex)
++pent_index;
else if(pent > hex)
++hex_index;
else if(pent == hex) {
++solution_count;
++pent_index;
std::cout << "solution #" << solution_count << ":\n pent_index: " << pent_index << "\n hex_index: " << hex_index << "\n y: " << pent
<< "\n elapsed: " << ((std::clock() - start) / (double)CLOCKS_PER_SEC) << "s\n\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment