Skip to content

Instantly share code, notes, and snippets.

@jaredallard
Last active December 18, 2015 23:07
Show Gist options
  • Save jaredallard/38ab727275e62ee85387 to your computer and use it in GitHub Desktop.
Save jaredallard/38ab727275e62ee85387 to your computer and use it in GitHub Desktop.
Find the inverse of a penny pyramid from pen.js (total => stack)

unpen.js

Usage:

node index.js total

Notes

Assumes 1+h, 1+w per new row. Assumes 13 on Z Axis

// calculate possible pyramids from an option.
"use strict";
var num_total = process.argv[2];
var num_stack = 13;
var num_dec = 1;
var log = console.log;
console.log('total:', num_total, 'dec:', num_dec, 'stack:', num_stack);
console.log('');
var num = 0,
num_height = 1,
num_width = 1,
success = false;
console.time('main')
for(var i = 0; num !== num_total; i++) {
if(num > num_total) {
// console.error('Number cannot be stacked.');
break;
}
var nh_wd = num_dec*i;
var nw_wd = nh_wd; // must be the same in this scenario
var num_tmp = nw_wd*nh_wd; // get the area based on width and height
var num_ws = num_tmp*num_stack; // get the amount w/ the variable stack.
var temp_total = num + num_ws; // combine them all together with previous stacks totals
// console.log('height:', nh_wd, 'width:', nw_wd, 'total:', temp_total);
if(temp_total == num_total) {
// console.log('total found');
num_height = nh_wd;
num_width = nw_wd; // outer scope
success = true;
num = temp_total;
break;
} else {
// console.log('tt:', temp_total, 'nt:', num_total)
}
num = temp_total // add that layer to the total.
}
if(success) {
log('succedded in finding a pyramid from the amount of pennies given');
log('height:', num_height);
log('width:', num_width);
log('Z Axis:', num_stack);
log('calculated total:', num);
log('your total:', num_total);
console.timeEnd('main')
process.exit(0);
}
log('failed to find a pyramid type. It\'s most likely impossible.');
console.timeEnd('main')
process.exit(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment