Jump to content

Pascal's triangle: Difference between revisions

no edit summary
No edit summary
Line 806:
}
}</lang>
 
=={{header|JavaScript}}==
{{works with|SpiderMonkey}}
{{works with|V8}}
<lang javascript>// Pascal's triangle object
function pascalTriangle (rows) {
 
// Number of rows the triangle contains
this.rows = rows;
 
// The 2D array holding the rows of the triangle
this.triangle = new Array();
for (var r = 0; r < rows; r++) {
this.triangle[r] = new Array();
for (var i = 0; i <= r; i++) {
if (i == 0 || i == r)
this.triangle[r][i] = 1;
else
this.triangle[r][i] = this.triangle[r-1][i-1]+this.triangle[r-1][i];
}
}
 
// Method to print the triangle formatted into a triangle
this.print = function(base) {
if (!base)
base = 10;
 
// Private method to calculate digits in number
var digits = function(n,b) {
var d = 0;
while (n >= 1) {
d++;
n /= b;
}
return d;
}
 
// Calculate max spaces needed
var spacing = digits(this.triangle[this.rows-1][Math.round(this.rows/2)],base);
 
// Private method to add spacing between numbers
var insertSpaces = function(s) {
var buf = "";
while (s > 0) {
s--;
buf += " ";
}
return buf;
}
 
for (var r = 0; r < this.triangle.length; r++) {
var l = "";
for (var s = 0; s < Math.round(this.rows-1-r); s++) {
l += insertSpaces(spacing);
}
for (var i = 0; i < this.triangle[r].length; i++) {
if (i != 0)
l += insertSpaces(spacing-Math.ceil(digits(this.triangle[r][i],base)/2));
l += this.triangle[r][i].toString(base);
if (i < this.triangle[r].length-1)
l += insertSpaces(spacing-Math.floor(digits(this.triangle[r][i],base)/2));
}
print(l);
}
}
 
}
 
// Display 4 row triangle in base 10
var tri = new pascalTriangle(4);
tri.print();
// Display 8 row triangle in base 16
tri = new pascalTriangle(8);
tri.print(16);</lang>
Output:
<pre>$ d8 pascal.js
1
1 1
1 2 1
1 3 3 1
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 a a 5 1
1 6 f 14 f 6 1
1 7 15 23 23 15 7 1
</pre>
 
=={{header|Liberty BASIC}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.