Pascal's triangle: Difference between revisions

m
→‎{{header|Wren}}: Minor tidy and now uses binomial method in Math module.
(added RPL)
m (→‎{{header|Wren}}: Minor tidy and now uses binomial method in Math module.)
 
(5 intermediate revisions by 2 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 6,314 ⟶ 6,406:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|wrenWren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
 
var binomial = Fn.new { |n, k|
if (n == k) return 1
var prod = 1
var i = n - k + 1
while (i <= n) {
prod = prod * i
i = i + 1
}
return prod / Int.factorial(k)
}
 
var pascalTriangle = Fn.new { |n|
Line 6,334 ⟶ 6,415:
System.write(" " * (n-i-1))
for (j in 0..i) {
Fmt.write("$3d ", binomialInt.callbinomial(i, j))
}
System.print()
9,476

edits