Jump to content

Pascal's triangle: Difference between revisions

m
→‎{{header|Wren}}: Minor tidy and now uses binomial method in Math module.
m (→‎{{header|Wren}}: Minor tidy and now uses binomial method in Math module.)
 
(20 intermediate revisions by 9 users not shown)
Line 341:
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="Amazing Hopper">
#include <jambo.h>
#define Mulbyandmoveto(_X_) Mul by '_X_', Move to '_X_'
 
Main
filas=0, Get arg numeric '2', Move to 'filas'
i=0, r=""
Loop if( var 'i' Is less than 'filas' )
c=1, j=0
Set 'c' To str, Move to 'r'
Loop if ( var 'j' Is less than 'i' )
Set 'i' Minus 'j', Plus one 'j', Div it; Mul by and move to 'c'
Multi cat ' r, "\t", Str(c) '; Move to 'r'
++j
Back
Printnl 'r'
++i
Back
End
</syntaxhighlight>
{{out}}
<pre>
$ hopper jm/pascal.jambo 14
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
 
</pre>
 
Line 700 ⟶ 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,925 ⟶ 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,834 ⟶ 2,967:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Pascal%27s_triangle}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Pascal's triangle 01.png]]
In '''[https://formulae.org/?example=Pascal%27s_triangle this]''' page you can see the program(s) related to this task and their results.
 
'''Test case'''
 
[[File:Fōrmulæ - Pascal's triangle 02.png]]
 
[[File:Fōrmulæ - Pascal's triangle 03.png]]
 
=={{header|GAP}}==
Line 3,186 ⟶ 3,325:
1 4 6 4 1</syntaxhighlight>
 
However, multi-digit numbers take up additional space, which looks slightly odd,. butBut we can work around that by adding additional padding and shifting the lines a bit more:
 
<syntaxhighlight lang=J> require(|."_1~ 0-3*i.@-@#) ;@((<'format/printf%6d') sprintf each -.&0)"1 !~/~i.10
1
(|."_1~ 0-3*i.@-@#) ;@((<'%6d') sprintf each -.&0)"1 !~/~i.9
1 1
1 2 1
1 23 3 1
1 34 36 4 1
1 45 10 6 10 4 5 1
1 56 1015 1020 15 5 6 1
1 67 1521 2035 1535 21 6 7 1
1 78 2128 3556 3570 2156 28 7 8 1
1 89 2836 5684 126 70 126 56 84 28 36 8 9 1
</syntaxhighlight>
 
Also... when we mix positive and negative numbers it stops being a triangle:
 
<syntaxhighlight lang=J> i:5
_5 _4 _3 _2 _1 0 1 2 3 4 5
!~/~i:5
1 0 0 0 0 1 _5 15 _35 70 _126
_4 1 0 0 0 1 _4 10 _20 35 _56
6 _3 1 0 0 1 _3 6 _10 15 _21
_4 3 _2 1 0 1 _2 3 _4 5 _6
1 _1 1 _1 1 1 _1 1 _1 1 _1
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 1 2 1 0 0 0
0 0 0 0 0 1 3 3 1 0 0
0 0 0 0 0 1 4 6 4 1 0
0 0 0 0 0 1 5 10 10 5 1
!/~i:5
1 _4 6 _4 1 0 0 0 0 0 0
0 1 _3 3 _1 0 0 0 0 0 0
0 0 1 _2 1 0 0 0 0 0 0
0 0 0 1 _1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1
_5 _4 _3 _2 _1 0 1 2 3 4 5
15 10 6 3 1 0 0 1 3 6 10
_35 _20 _10 _4 _1 0 0 0 1 4 10
70 35 15 5 1 0 0 0 0 1 5
_126 _56 _21 _6 _1 0 0 0 0 0 1</syntaxhighlight>
 
 
 
See the [[Talk:Pascal's_triangle#J_Explanation|talk page]] for explanation of earlier version
Line 3,920 ⟶ 4,090:
 
for [i 1 10] [print pascal :i]</syntaxhighlight>
 
=={{header|Logtalk}}==
Our implementation will have an object <code>pascals</code> with work done in the method <code>triangle/2</code>. We will be caching results for time efficiency at the cost of space efficiency,and the <code>reset/0</code> method will flush that cache should it grow to be a problem. The resulting object looks like this:
<syntaxhighlight lang="logtalk">
:- object(pascals).
 
:- uses(integer, [plus/3, succ/2]).
 
:- public(reset/0).
 
reset :-
retractall(triangle_(_,_,_)).
:- private(triangle_/3).
:- dynamic(triangle_/3).
 
:- public(triangle/2).
 
triangle(N, Lines) :-
triangle(N, _, DiffLines),
difflist::as_list(DiffLines, Lines).
 
% Shortcut with cached value if it exists.
triangle(N, Line, DiffLines) :- triangle_(N, Line, DiffLines), !.
 
triangle(N, Line, DiffLines) :-
succ(N0, N),
triangle(N0, Line0, DiffLines0),
ZL = [0|Line0],
list::append(Line0, [0], ZR),
meta::map(plus, ZL, ZR, Line),
difflist::add(Line, DiffLines0, DiffLines),
asserta(triangle_(N, Line, DiffLines)).
 
triangle(1, [1], [[1]|X]-X).
 
:- end_object.
</syntaxhighlight>
 
{{Out}}
 
Using the SWI-Prolog back-end:
 
<pre>
?- logtalk_load([meta(loader), types(loader), pascals], [optimize(on)]).
% messages elided
true.
 
?- pascals::triangle(17, Ls), logtalk::print_message(information, user, Ls).
% - [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|...], [...|...]|...].
 
?-
</pre>
 
=={{header|Lua}}==
Line 4,043 ⟶ 4,283:
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">sjoin(v, j) := apply(sconcat, rest(join(makelist(j, length(v)), v)))$
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,054 ⟶ 4,295:
"1 4 6 4 1"
"1 5 10 10 5 1"
"1 6 15 20 15 6 1" */</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Metafont}}==
Line 5,405 ⟶ 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,139 ⟶ 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,159 ⟶ 6,415:
System.write(" " * (n-i-1))
for (j in 0..i) {
Fmt.write("$3d ", binomialInt.callbinomial(i, j))
}
System.print()
9,476

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.