Skip to content

Instantly share code, notes, and snippets.

@skorezore
Last active December 2, 2015 21:59
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 skorezore/54354456841162c66687 to your computer and use it in GitHub Desktop.
Save skorezore/54354456841162c66687 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#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); };
double pent_index = 0;
double hex_index = 0;
unsigned int solution_count = 0;
std::clock_t start = std::clock();
std::cout << std::fixed << std::setprecision(0);
while(true)
{
const auto pent_number = pentagonal_function(pent_index);
const auto hex_number = hexagonal_function(hex_index);
if (pent_number < hex_number) ++pent_index;
else if (pent_number > hex_number) ++hex_index;
else if (pent_number == hex_number)
{
++solution_count;
std::cout << "Solution #" << solution_count << " found in " << (std::clock() - start) / (double) CLOCKS_PER_SEC << "s" << ":\n--> Triangular N: " << hex_index * 2 << "\n--> Pentagonal N: " << pent_index << "\n--> Hexagonal N: " << hex_index << "\n";
++pent_index;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment