Pascal's triangle: Difference between revisions
m
→{{header|Wren}}: Minor tidy and now uses binomial method in Math module.
Ttmrichter (talk | contribs) (→{{header|Lua}}: Added a Logtalk implementation.) |
m (→{{header|Wren}}: Minor tidy and now uses binomial method in Math module.) |
||
(13 intermediate revisions by 7 users not shown) | |||
Line 741:
1 5 10 10 5 1
</pre>
=={{header|Bait}}==
<syntaxhighlight lang="bait">
// Create a Pascal's triangle with a given number of rows.
// Returns an empty array for row_nr <= 0.
fun pascals_triangle(row_nr i32) [][]i32 {
mut rows := [][]i32
// Iterate over all rows
for r := 0; r < row_nr; r += 1 {
// Store the row above the current one
mut above := rows[r - 1]
// Fill the current row. It contains r + 1 numbers
for i := 0; i <= r; i += 1 {
// First number is always 1
if i == 0 {
rows.push([1]) // Push new row
}
// Last number is always 1
else if i == r {
rows[r].push(1)
}
// Other numbers are the sum of the two numbers above them
else {
rows[r].push(above[i - 1] + above[i])
}
}
}
return rows
}
// Helper function to pretty print triangles.
// It still get's ugly once numbers have >= 2 digits.
fun print_triangle(triangle [][]i32) {
for i, row in triangle {
// Create string with leading spaces
mut s := ' '.repeat(triangle.length - i - 1)
// Add each number to the string
for n in row {
s += n.str() + ' '
}
// Print and trim the extra trailing space
println(s.trim_right(' '))
}
}
fun main() {
print_triangle(pascals_triangle(7))
}
</syntaxhighlight>
{{out}}
<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
</pre>
=={{header|BASIC}}==
Line 1,966 ⟶ 2,034:
}
makeCommand("yourFavoriteWebBrowser")("triangle.html")</syntaxhighlight>
=={{header|EasyLang}}==
<syntaxhighlight>
numfmt 0 4
proc pascal n . .
r[] = [ 1 ]
for i to n
rn[] = [ ]
l = 0
for j to n - len r[]
write " "
.
for r in r[]
write r
rn[] &= l + r
l = r
.
print ""
rn[] &= l
swap r[] rn[]
.
.
pascal 13
</syntaxhighlight>
=={{header|Eiffel}}==
Line 2,875 ⟶ 2,967:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Pascal%27s_triangle}}
'''Solution'''
[[File:Fōrmulæ - Pascal's triangle 01.png]]
'''Test case'''
[[File:Fōrmulæ - Pascal's triangle 02.png]]
[[File:Fōrmulæ - Pascal's triangle 03.png]]
=={{header|GAP}}==
Line 3,998 ⟶ 4,096:
:- object(pascals).
:- uses(
:- public(reset/0).
reset :-
retractall(triangle_(_,_,_)).
:- private(triangle_/3).
:- dynamic(triangle_/3).
:- public(triangle/2).
Line 4,033 ⟶ 4,134:
<pre>
?-
true.
?- pascals::triangle(17, Ls), logtalk::print_message(
% - [1]
% - [1,1]
% - [1,2,1]
% - [1,3,3,1]
% - [1,4,6,4,1]
% - [1,5,10,10,5,1]
% - [1,6,15,20,15,6,1]
% - [1,7,21,35,35,21,7,1]
% - [1,8,28,56,70,56,28,8,1]
% - [1,9,36,84,126,126,84,36,9,1]
% - [1,10,45,120,210,252,210,120,45,10,1]
% - [1,11,55,165,330,462,462,330,165,55,11,1]
% - [1,12,66,220,495,792,924,792,495,220,66,12,1]
% - [1,13,78,286,715,1287,1716,1716,1287,715,286,78,13,1]
% - [1,14,91,364,1001,2002,3003,3432,3003,2002,1001,364,91,14,1]
% - [1,15,105,455,1365,3003,5005,6435,6435,5005,3003,1365,455,105,15,1]
% - [1,16,120,560,1820,4368,8008,11440,12870,11440,8008,4368,1820,560,120,16,1]
Ls = [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4|...], [1, 5, 10|...], [1, 6|...], [1|...], [...|...]|...].
Line 4,185 ⟶ 4,283:
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
sjoin(v, j) := apply(sconcat, rest(join(makelist(j, length(v)), v)))$
display_pascal_triangle(n) := for i from 0 thru 6 do disp(sjoin(makelist(binomial(i, j), j, 0, i), " "));
Line 4,196 ⟶ 4,295:
"1 4 6 4 1"
"1 5 10 10 5 1"
"1 6 15 20 15 6 1" */
</syntaxhighlight>
=={{header|Metafont}}==
Line 5,547 ⟶ 5,647:
1 4 6 4 1
</pre>
=={{header|RPL}}==
« 0 SWAP '''FOR''' n
"" 0 n '''FOR''' p
n p COMB + " " +
'''NEXT'''
n 1 + DISP
'''NEXT'''
7 FREEZE
» '<span style="color:blue">PASCAL</span>' STO
8 <span style="color:blue">PASCAL</span>
{{out}}
<pre>
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 …
</pre>
RPL screens are limited to 22 characters.
=={{header|Ruby}}==
Line 6,281 ⟶ 6,406:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|
<syntaxhighlight lang="
import "./math" for Int
var pascalTriangle = Fn.new { |n|
Line 6,301 ⟶ 6,415:
System.write(" " * (n-i-1))
for (j in 0..i) {
Fmt.write("$3d ",
}
System.print()
|