-
-
Save skorezore/f89b5f2f8f4c795cff37 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 <ctime> | |
enum class comparison_result | |
{ | |
lesser, | |
greater, | |
equal | |
}; | |
comparison_result compare(unsigned long long first_number, unsigned long long second_number) | |
{ | |
if (first_number < second_number) return comparison_result::lesser; | |
if (first_number > second_number) return comparison_result::greater; | |
if (first_number == second_number) return comparison_result::equal; | |
__builtin_unreachable(); | |
} | |
int main() | |
{ | |
auto pentagonal_function = [](unsigned long long n) { return static_cast<unsigned long long>((n * ((3 * n) - 1)) / 2); }; | |
auto hexagonal_function = [](unsigned long long n) { return static_cast<unsigned long long>(n * ((2 * n) - 1)); }; | |
unsigned long long int pent_index = 0; | |
unsigned long long int hex_index = 0; | |
unsigned int solution_count = 1; | |
std::clock_t start = std::clock(); | |
while (true) | |
{ | |
if (compare(pentagonal_function(pent_index), hexagonal_function(hex_index)) == comparison_result::lesser) pent_index++; | |
if (compare(pentagonal_function(pent_index), hexagonal_function(hex_index)) == comparison_result::greater) hex_index++; | |
if (compare(pentagonal_function(pent_index), hexagonal_function(hex_index)) == comparison_result::equal) | |
{ | |
std::cout << "solution #" << solution_count << ": " << "pent_index: " << pent_index << "\n" << "hex_index: " << hex_index << "\n" << "y: " << pentagonal_function(pent_index) << "\nelapsed: " << (std::clock() - start) / (double) CLOCKS_PER_SEC << "s\n" << std::endl; | |
solution_count++; | |
pent_index++; | |
} | |
} | |
} |
nmai
commented
Dec 2, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment