-
-
Save skorezore/54354456841162c66687 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
#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