Pascal's triangle: Difference between revisions

m
Fixed lang tags.
No edit summary
m (Fixed lang tags.)
Line 9:
 
=={{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;
 
Line 43 ⟶ 42:
begin
Print(General_Triangle(7));
end Pascals_Triangle;</lang>
</lang>
=={{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),
MAXUPB = ([]INT a,b)INT: (UPB a>UPB b|UPB a|UPB b);
Line 65 ⟶ 62:
# WHILE # i < stop DO
row := row[AT 1] + row[AT 2]
OD</prelang>
Output:
1
Line 99 ⟶ 96:
 
GuiClose:
ExitApp</lang>
</lang>
 
=={{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}}==
Line 248 ⟶ 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).
<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}}==
To evaluate, call (pascal n). For n < 1, it simply returns nil.
 
<prelang lisp>(defun pascal (n)
(genrow n '(1)))
 
Line 275 ⟶ 271:
(if (> 2 (length l))
'(1)
(cons (+ (car l) (cadr l)) (newrow (cdr l)))))</prelang>
 
=={{header|D}}==
Line 356 ⟶ 352:
This implementation works by summing the previous line content. Result for n < 1 is the same as for n == 1.
 
<lang factor>USING: grouping kernel math sequences ;
USING: grouping kernel math sequences ;
 
: (pascal) ( seq -- newseq )
Line 363 ⟶ 358:
 
: pascal ( n -- seq )
1 - { { 1 } } swap [ (pascal) ] times ;</lang>
</lang>
 
It works as:
 
<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}}==
<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}}==
{{works with|Fortran|90 and later}}
Prints nothing for n<=0. Output formatting breaks down for n>20
<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}}==
Line 454 ⟶ 446:
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 [] ys = 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
the ends:
 
<lang haskell>extendWith f [] = []
<pre>
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,
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
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]
<lang haskell>-- generate next row from current row
<pre>
-- generate next row from current row
nextRow row = zipWith (+) ([0] ++ row) (row ++ [0])
 
-- returns the first n rows
pascal = iterate nextRow [1]</lang>
</pre>
 
=={{header|J}}==
=== solution ===
 
<lang j>!/~@i. N</lang>
<pre>
!/~@i. N
</pre>
 
=== explanation ===
Line 507 ⟶ 487:
 
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, 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 1 2 3 4 5 6 7 8 9
Line 526 ⟶ 503:
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81</lang>
</pre>
 
and here's the addition table for 0 to 4
<lang j>+/~@i. 4
<pre>
+/~@i. 4
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6</lang>
</pre>
 
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
0 1 2 3 4
0 0 1 3 6
0 0 0 1 4
0 0 0 0 1</lang>
</pre>
 
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 2 1
1 3 3 1
1 4 6 4 1</lang>
</pre>
 
(I won't bother explaining the formatting.)
Line 577 ⟶ 547:
print, r
 
End</lang>
</lang>
 
=={{header|Java}}==
Line 635 ⟶ 604:
 
=={{header|Logo}}==
<lang logo>to pascal :n
to pascal :n
if :n = 1 [output [1]]
localmake "a pascal :n-1
Line 642 ⟶ 610:
end
 
for [i 1 10] [print pascal :i]</lang>
</lang>
 
=={{header|Metafont}}==
Line 672 ⟶ 639:
 
(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
<lang nial>|loaddefs 'pascal.nial'
|pascal 5</lang>
 
=={{header|OCaml}}==
Line 761 ⟶ 728:
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
Line 773 ⟶ 739:
NEXT i
PRINT
NEXT row</lang>
</pre>
 
===Using binary coefficients===
{{trans|BASIC}}
<lang rapidq>INPUT "Number of rows: "; nrows
<pre>
INPUT "Number of rows: "; nrows
FOR row = 0 TO nrows-1
c = 1
Line 788 ⟶ 752:
NEXT i
PRINT
NEXT row</lang>
</pre>
 
=={{header|Ruby}}==
<lang ruby>def pascal(n = 1)
def pascal(n = 1)
return if n < 1
Line 819 ⟶ 781:
p
end</lang>
</lang>
 
=={{header|Seed7}}==
Line 844 ⟶ 805:
writeln;
end for;
end func;</lang>
</lang>
 
=={{header|Tcl}}==
Line 949 ⟶ 909:
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.
<lang Ursala>#import std
#import std
#import nat
 
pascal "n" = (next"n" sum*NiCixp) <1></lang>
</lang>
test program:
<lang Ursala>#cast %nLL
Line 980 ⟶ 938:
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
Ins_Char(' ', COUNT, #100*3-2) Num_Ins(1)
Line 993 ⟶ 950:
}
Ins_Newline
}</lang>
}
</pre>
 
===Using binary coefficients===
{{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++) {
#3 = 1
Line 1,008 ⟶ 963:
}
Ins_Newline
}</lang>
}
</pre>
Anonymous user