Skip to content

Instantly share code, notes, and snippets.

@skorezore
Last active December 2, 2015 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save skorezore/f89b5f2f8f4c795cff37 to your computer and use it in GitHub Desktop.
Save skorezore/f89b5f2f8f4c795cff37 to your computer and use it in GitHub Desktop.
#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
Copy link

nmai commented Dec 2, 2015

'use strict'

// Handling the comparison in a separate function gives a ~30% speed boost
function compare (first_number, second_number) {
    if (first_number < second_number) return -1
    if (first_number > second_number) return 1
    if (first_number === second_number) return 0
}

// Arrow functions are perfect for these types of expressions.
let hex = (n) => { return n * ((2 * n) - 1) }
let pent = (n) => { return (n * ((3 * n) - 1)) / 2 }

// These get incremented separately based on comparison results.
let pent_index = 0, hex_index = 0

let start_date = new Date()
let sol_count = 1

while (true) {
    // Grab and store the results of both equations
    let h = hex(hex_index), p = pent(pent_index)

    // Decide which var(s) we need to increment
    let result = compare(p, h)
    if (result === -1) pent_index++
    if (result === 1) hex_index++
    if (result === 0) {
        let time = (new Date() - start_date)/1000
        if (time > 30) break
        console.log('Solution ' + sol_count + ' found after ' + time + ' seconds.')
        console.log('-> Number: ' + h)
        console.log('--> Triangle N val: ' + (h * 2))
        console.log('--> Pentagonal N val: ' + p)
        console.log('--> Hexagonal N val: ' + h)
        pent_index++
        sol_count++
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment