Skip to content

Instantly share code, notes, and snippets.

@kgates-github
Last active May 6, 2020 07:52
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save kgates-github/4695492 to your computer and use it in GitHub Desktop.
Save kgates-github/4695492 to your computer and use it in GitHub Desktop.
Two ways to create a Pascal's Triangle in JavaScript.
var numTiers = 100,
triangle,
start,
stop;
/**
*
* First version uses recursion
*
*/
function pascalRecursive(n, a) {
if (n < 2) return a; // We already have the top row
var prevTier = a[a.length-1];
var curTier = [1];
for (var i = 1; i < prevTier.length; i++) {
curTier[i] = prevTier[i] + prevTier[i-1];
}
curTier.push(1);
a.push(curTier);
return pascalRecursive(n-1, a);
}
start = new Date().getMilliseconds();
var triangle = pascalRecursive(numTiers, [[1]]);
stop = new Date().getMilliseconds();
executionTime = stop - start;
console.log('Execution time: ' + executionTime + '\n');
/**
*
* Second version is simpler, but slower
*
*/
function pascalSimple(numTiers) {
var triangle = [
[1]
],
tier;
for (var j = 0; j < numTiers-1; j++) {
tier = [1];
for (var k = 1; k < triangle[j].length; k++) {
tier[k] = triangle[j][k] + triangle[j][k-1];
}
tier.push(1);
triangle.push(tier);
}
return triangle;
}
start = new Date().getMilliseconds();
var triangle = pascalSimple(numTiers);
stop = new Date().getMilliseconds();
executionTime = stop - start;
console.log('Execution time: ' + executionTime + '\n');
@itsgratien
Copy link

cool, but it can be more understandable if you try to explain lines of code. thanks

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