Skip to content

Instantly share code, notes, and snippets.

@jaredallard
Created December 18, 2015 20:25
Show Gist options
  • Save jaredallard/e983cadbdb7796b416fd to your computer and use it in GitHub Desktop.
Save jaredallard/e983cadbdb7796b416fd to your computer and use it in GitHub Desktop.
Calculate a penny triangle using node.js!

pen.js

How to use:

node pen.js width height pennies_per_stack pennies_removed_per_row
"use strict"; // perfromance optimization
var num_width = process.argv[2];
var num_height = process.argv[3];
var num_stack = process.argv[4];
var num_dec = process.argv[5];
console.log('h:', num_height, 'w:', num_width, 'dec:', num_dec, 'stack:', num_stack);
if(!num_stack || !num_width || !num_height || !num_dec) {
console.log('invalid input');
process.exit(1); // bad error code
}
// scope & instancing
var num = 0,
total = 0;
console.time('main');
for(var i = 0; num !== 1; i++) {
var nd_wi = num_dec*i; // calculate amount to subtract from difference * iteration
var nh_wd = num_height-nd_wi; // calculate the height with difference
var nw_wd = num_width-nd_wi; // calculate the width with difference
var num = nw_wd*nh_wd; // get the area based on width and height
var num_ws = num*num_stack // get the amount w/ the variable stack.
total += num_ws; // add that layer to the total.
}
console.log('total is', total);
console.timeEnd('main');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment