Pascal's triangle: Difference between revisions
Content added Content deleted
No edit summary |
m (Fixed lang tags.) |
||
Line 9: | Line 9: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
<lang ada>with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; |
|||
<lang ada> |
|||
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; |
|||
with Ada.Text_Io; use Ada.Text_Io; |
with Ada.Text_Io; use Ada.Text_Io; |
||
Line 43: | Line 42: | ||
begin |
begin |
||
Print(General_Triangle(7)); |
Print(General_Triangle(7)); |
||
end Pascals_Triangle; |
end Pascals_Triangle;</lang> |
||
</lang> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
<lang algol68>PRIO MINLWB = 8, MAXUPB = 8; |
|||
<pre> |
|||
PRIO MINLWB = 8, MAXUPB = 8; |
|||
OP MINLWB = ([]INT a,b)INT: (LWB a<LWB b|LWB a|LWB b), |
OP MINLWB = ([]INT a,b)INT: (LWB a<LWB b|LWB a|LWB b), |
||
MAXUPB = ([]INT a,b)INT: (UPB a>UPB b|UPB a|UPB b); |
MAXUPB = ([]INT a,b)INT: (UPB a>UPB b|UPB a|UPB b); |
||
Line 65: | Line 62: | ||
# WHILE # i < stop DO |
# WHILE # i < stop DO |
||
row := row[AT 1] + row[AT 2] |
row := row[AT 1] + row[AT 2] |
||
OD</ |
OD</lang> |
||
Output: |
Output: |
||
1 |
1 |
||
Line 99: | Line 96: | ||
GuiClose: |
GuiClose: |
||
ExitApp |
ExitApp</lang> |
||
</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang awk>$ awk 'BEGIN{for(i=0;i<6;i++){c=1;r=c;for(j=0;j<i;j++){c*=(i-j)/(j+1);r=r" "c};print r}}' |
|||
1 |
|||
1 1 |
|||
1 2 1 |
|||
1 3 3 1 |
|||
1 4 6 4 1 |
|||
1 5 10 10 5 1</lang> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
Line 248: | Line 244: | ||
For n < 1, prints nothing, always returns nil. Copied from the Common Lisp implementation below, but with local functions and explicit tail-call-optimized recursion (recur). |
For n < 1, prints nothing, always returns nil. Copied from the Common Lisp implementation below, but with local functions and explicit tail-call-optimized recursion (recur). |
||
<lang lisp>(defn pascal [n] |
|||
(let [newrow (fn newrow [lst ret] |
|||
(if lst |
|||
(recur (rest lst) |
|||
(conj ret (+ (first lst) (or (second lst) 0)))) |
|||
ret)) |
|||
genrow (fn genrow [n lst] |
|||
(when (< 0 n) |
|||
(do (println lst) |
|||
(recur (dec n) (conj (newrow lst []) 1)))))] |
|||
(genrow n [1]))) |
|||
(pascal 4)</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
To evaluate, call (pascal n). For n < 1, it simply returns nil. |
To evaluate, call (pascal n). For n < 1, it simply returns nil. |
||
< |
<lang lisp>(defun pascal (n) |
||
(genrow n '(1))) |
(genrow n '(1))) |
||
Line 275: | Line 271: | ||
(if (> 2 (length l)) |
(if (> 2 (length l)) |
||
'(1) |
'(1) |
||
(cons (+ (car l) (cadr l)) (newrow (cdr l)))))</ |
(cons (+ (car l) (cadr l)) (newrow (cdr l)))))</lang> |
||
=={{header|D}}== |
=={{header|D}}== |
||
Line 356: | Line 352: | ||
This implementation works by summing the previous line content. Result for n < 1 is the same as for n == 1. |
This implementation works by summing the previous line content. Result for n < 1 is the same as for n == 1. |
||
<lang factor> |
<lang factor>USING: grouping kernel math sequences ; |
||
USING: grouping kernel math sequences ; |
|||
: (pascal) ( seq -- newseq ) |
: (pascal) ( seq -- newseq ) |
||
Line 363: | Line 358: | ||
: pascal ( n -- seq ) |
: pascal ( n -- seq ) |
||
1 - { { 1 } } swap [ (pascal) ] times ; |
1 - { { 1 } } swap [ (pascal) ] times ;</lang> |
||
</lang> |
|||
It works as: |
It works as: |
||
<lang factor> |
<lang factor>5 pascal . |
||
{ { 1 } { 1 1 } { 1 2 1 } { 1 3 3 1 } { 1 4 6 4 1 } }</lang> |
|||
5 pascal . |
|||
{ { 1 } { 1 1 } { 1 2 1 } { 1 3 3 1 } { 1 4 6 4 1 } } |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
<lang forth>: init ( n -- ) |
|||
here swap cells erase 1 here ! ; |
|||
: .line ( n -- ) |
|||
cr here swap 0 do dup @ . cell+ loop drop ; |
|||
: next ( n -- ) |
|||
here swap 1- cells here + do |
|||
i @ i cell+ +! |
|||
-1 cells +loop ; |
|||
: pascal ( n -- ) |
|||
dup init 1 .line |
|||
1 ?do i next i 1+ .line loop ;</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
Prints nothing for n<=0. Output formatting breaks down for n>20 |
Prints nothing for n<=0. Output formatting breaks down for n>20 |
||
<lang fortran> |
<lang fortran>PROGRAM Pascals_Triangle |
||
CALL Print_Triangle(8) |
|||
END PROGRAM Pascals_Triangle |
|||
SUBROUTINE Print_Triangle(n) |
|||
IMPLICIT NONE |
|||
INTEGER, INTENT(IN) :: n |
|||
INTEGER :: c, i, j, k, spaces |
|||
DO i = 0, n-1 |
|||
c = 1 |
|||
spaces = 3 * (n - 1 - i) |
|||
DO j = 1, spaces |
|||
WRITE(*,"(A)", ADVANCE="NO") " " |
|||
END DO |
|||
DO k = 0, i |
|||
WRITE(*,"(I6)", ADVANCE="NO") c |
|||
c = c * (i - k) / (k + 1) |
|||
END DO |
|||
WRITE(*,*) |
|||
END DO |
|||
END SUBROUTINE Print_Triangle</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 454: | Line 446: | ||
similar function |
similar function |
||
<lang haskell>zapWith :: (a -> a -> a) -> [a] -> [a] -> [a] |
|||
<pre> |
|||
zapWith :: (a -> a -> a) -> [a] -> [a] -> [a] |
|||
zapWith f xs [] = xs |
zapWith f xs [] = xs |
||
zapWith f [] ys = ys |
zapWith f [] ys = ys |
||
zapWith f (x:xs) (y:ys) = f x y : zapWith f xs ys |
zapWith f (x:xs) (y:ys) = f x y : zapWith f xs ys</lang> |
||
</pre> |
|||
Now we can shift a list and add it to itself, extending it by keeping |
Now we can shift a list and add it to itself, extending it by keeping |
||
the ends: |
the ends: |
||
<lang haskell>extendWith f [] = [] |
|||
<pre> |
|||
extendWith f |
extendWith f xs@(x:ys) = x : zapWith f xs ys</lang> |
||
extendWith f xs@(x:ys) = x : zapWith f xs ys |
|||
</pre> |
|||
And for the whole (infinite) triangle, we just iterate this operation, |
And for the whole (infinite) triangle, we just iterate this operation, |
||
starting with the first row: |
starting with the first row: |
||
<lang haskell>pascal = iterate (extendWith (+)) [1]</lang> |
|||
<pre> |
|||
pascal = iterate (extendWith (+)) [1] |
|||
</pre> |
|||
For the first ''n'' rows, we just take the first ''n'' elements from this |
For the first ''n'' rows, we just take the first ''n'' elements from this |
||
list, as in |
list, as in |
||
<lang haskell>*Main> take 6 pascal |
|||
<pre> |
|||
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]]</lang> |
|||
*Main> take 6 pascal |
|||
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]] |
|||
</pre> |
|||
A shorter approach, plagiarized from [http://www.haskell.org/haskellwiki/Blow_your_mind] |
A shorter approach, plagiarized from [http://www.haskell.org/haskellwiki/Blow_your_mind] |
||
<lang haskell>-- generate next row from current row |
|||
<pre> |
|||
-- generate next row from current row |
|||
nextRow row = zipWith (+) ([0] ++ row) (row ++ [0]) |
nextRow row = zipWith (+) ([0] ++ row) (row ++ [0]) |
||
-- returns the first n rows |
-- returns the first n rows |
||
pascal = iterate nextRow [1] |
pascal = iterate nextRow [1]</lang> |
||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
=== solution === |
=== solution === |
||
<lang j>!/~@i. N</lang> |
|||
<pre> |
|||
!/~@i. N |
|||
</pre> |
|||
=== explanation === |
=== explanation === |
||
Line 507: | Line 487: | ||
So, for example, the number of ways to choose a poker hand (5 cards from the deck of 52): |
So, for example, the number of ways to choose a poker hand (5 cards from the deck of 52): |
||
<lang j>5!52 |
|||
<pre> |
|||
2598960</lang> |
|||
5!52 |
|||
2598960 |
|||
</pre> |
|||
So <tt>!</tt> is the mathematical choose function. What of <tt>/~@i.</tt>? Well, you can think of <tt>/~</tt> as "table of" and <tt>@i.</tt> "the first N non-negative integers (i.e. 0 .. N-1)". |
So <tt>!</tt> is the mathematical choose function. What of <tt>/~@i.</tt>? Well, you can think of <tt>/~</tt> as "table of" and <tt>@i.</tt> "the first N non-negative integers (i.e. 0 .. N-1)". |
||
So, for example, here's the multiplication table you had to memorize in first grade: |
So, for example, here's the multiplication table you had to memorize in first grade: |
||
<lang j>*/~@i. 10 |
|||
<pre> |
|||
*/~@i. 10 |
|||
0 0 0 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
||
0 1 2 3 4 5 6 7 8 9 |
0 1 2 3 4 5 6 7 8 9 |
||
Line 526: | Line 503: | ||
0 7 14 21 28 35 42 49 56 63 |
0 7 14 21 28 35 42 49 56 63 |
||
0 8 16 24 32 40 48 56 64 72 |
0 8 16 24 32 40 48 56 64 72 |
||
0 9 18 27 36 45 54 63 72 81 |
0 9 18 27 36 45 54 63 72 81</lang> |
||
</pre> |
|||
and here's the addition table for 0 to 4 |
and here's the addition table for 0 to 4 |
||
<lang j>+/~@i. 4 |
|||
<pre> |
|||
+/~@i. 4 |
|||
0 1 2 3 |
0 1 2 3 |
||
1 2 3 4 |
1 2 3 4 |
||
2 3 4 5 |
2 3 4 5 |
||
3 4 5 6 |
3 4 5 6</lang> |
||
</pre> |
|||
Similarly, <tt>!/~@i.</tt> is the number-of-combinations table, or the "choose" table: |
Similarly, <tt>!/~@i.</tt> is the number-of-combinations table, or the "choose" table: |
||
<lang j>!/~@i. 5 |
|||
<pre> |
|||
!/~@i. 5 |
|||
1 1 1 1 1 |
1 1 1 1 1 |
||
0 1 2 3 4 |
0 1 2 3 4 |
||
0 0 1 3 6 |
0 0 1 3 6 |
||
0 0 0 1 4 |
0 0 0 1 4 |
||
0 0 0 0 1 |
0 0 0 0 1</lang> |
||
</pre> |
|||
Of course, to format it nicely, you need to do a little more work: |
Of course, to format it nicely, you need to do a little more work: |
||
<lang j>([:'0'&=`(,:&' ')} -@|. |."_1 [: ":@|: !/~)@i. 5 |
|||
<pre> |
|||
([:'0'&=`(,:&' ')} -@|. |."_1 [: ":@|: !/~)@i. 5 |
|||
1 |
1 |
||
1 1 |
1 1 |
||
1 2 1 |
1 2 1 |
||
1 3 3 1 |
1 3 3 1 |
||
1 4 6 4 1 |
1 4 6 4 1</lang> |
||
</pre> |
|||
(I won't bother explaining the formatting.) |
(I won't bother explaining the formatting.) |
||
Line 577: | Line 547: | ||
print, r |
print, r |
||
End |
End</lang> |
||
</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 635: | Line 604: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
<lang logo> |
<lang logo>to pascal :n |
||
to pascal :n |
|||
if :n = 1 [output [1]] |
if :n = 1 [output [1]] |
||
localmake "a pascal :n-1 |
localmake "a pascal :n-1 |
||
Line 642: | Line 610: | ||
end |
end |
||
for [i 1 10] [print pascal :i] |
for [i 1 10] [print pascal :i]</lang> |
||
</lang> |
|||
=={{header|Metafont}}== |
=={{header|Metafont}}== |
||
Line 672: | Line 639: | ||
(pascal.nial) |
(pascal.nial) |
||
<lang nial>factorial is recur [ 0 =, 1 first, pass, product, -1 +] |
|||
combination is fork [ > [first, second], 0 first, |
|||
/ [factorial second, * [factorial - [second, first], factorial first] ] |
|||
] |
|||
pascal is transpose each combination cart [pass, pass] tell</lang> |
|||
Using it |
Using it |
||
<lang nial>|loaddefs 'pascal.nial' |
|||
|pascal 5</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 761: | Line 728: | ||
RapidQ does not require simple variables to be declared before use. |
RapidQ does not require simple variables to be declared before use. |
||
<lang rapidq>DEFINT values(100) = {0,1} |
|||
<pre> |
|||
DEFINT values(100) = {0,1} |
|||
INPUT "Number of rows: "; nrows |
INPUT "Number of rows: "; nrows |
||
Line 773: | Line 739: | ||
NEXT i |
NEXT i |
||
PRINT |
PRINT |
||
NEXT row |
NEXT row</lang> |
||
</pre> |
|||
===Using binary coefficients=== |
===Using binary coefficients=== |
||
{{trans|BASIC}} |
{{trans|BASIC}} |
||
<lang rapidq>INPUT "Number of rows: "; nrows |
|||
<pre> |
|||
INPUT "Number of rows: "; nrows |
|||
FOR row = 0 TO nrows-1 |
FOR row = 0 TO nrows-1 |
||
c = 1 |
c = 1 |
||
Line 788: | Line 752: | ||
NEXT i |
NEXT i |
||
PRINT |
PRINT |
||
NEXT row |
NEXT row</lang> |
||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<lang ruby> |
<lang ruby>def pascal(n = 1) |
||
def pascal(n = 1) |
|||
return if n < 1 |
return if n < 1 |
||
Line 819: | Line 781: | ||
p |
p |
||
end |
end</lang> |
||
</lang> |
|||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 844: | Line 805: | ||
writeln; |
writeln; |
||
end for; |
end for; |
||
end func; |
end func;</lang> |
||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 949: | Line 909: | ||
insert zero at the head of a list (initially the unit list <1>), zip it with its reversal, |
insert zero at the head of a list (initially the unit list <1>), zip it with its reversal, |
||
map the sum over the list of pairs, iterate n times, and return the trace. |
map the sum over the list of pairs, iterate n times, and return the trace. |
||
<lang Ursala> |
<lang Ursala>#import std |
||
#import std |
|||
#import nat |
#import nat |
||
pascal "n" = (next"n" sum*NiCixp) <1> |
pascal "n" = (next"n" sum*NiCixp) <1></lang> |
||
</lang> |
|||
test program: |
test program: |
||
<lang Ursala>#cast %nLL |
<lang Ursala>#cast %nLL |
||
Line 980: | Line 938: | ||
For example, if #99 contains value 2, then #@99 accesses contents of numeric register #2. |
For example, if #99 contains value 2, then #@99 accesses contents of numeric register #2. |
||
<lang vedit>#100 = Get_Num("Number of rows: ", STATLINE) |
|||
<pre> |
|||
#100 = Get_Num("Number of rows: ", STATLINE) |
|||
#0=0; #1=1 |
#0=0; #1=1 |
||
Ins_Char(' ', COUNT, #100*3-2) Num_Ins(1) |
Ins_Char(' ', COUNT, #100*3-2) Num_Ins(1) |
||
Line 993: | Line 950: | ||
} |
} |
||
Ins_Newline |
Ins_Newline |
||
}</lang> |
|||
} |
|||
</pre> |
|||
===Using binary coefficients=== |
===Using binary coefficients=== |
||
{{trans|BASIC}} |
{{trans|BASIC}} |
||
<lang vedit>#1 = Get_Num("Number of rows: ", STATLINE) |
|||
<pre> |
|||
#1 = Get_Num("Number of rows: ", STATLINE) |
|||
for (#2 = 0; #2 < #1; #2++) { |
for (#2 = 0; #2 < #1; #2++) { |
||
#3 = 1 |
#3 = 1 |
||
Line 1,008: | Line 963: | ||
} |
} |
||
Ins_Newline |
Ins_Newline |
||
}</lang> |
|||
} |
|||
</pre> |