Usage:
node index.js totalAssumes 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); |