Anonymous user
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;
with Ada.Text_Io; use Ada.Text_Io;
Line 43 ⟶ 42:
begin
Print(General_Triangle(7));
end Pascals_Triangle;</lang>
=={{header|ALGOL 68}}==
<lang algol68>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</
Output:
1
Line 99 ⟶ 96:
GuiClose:
ExitApp</lang>
=={{header|AWK}}==
=={{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).
=={{header|Common Lisp}}==
To evaluate, call (pascal n). For n < 1, it simply returns nil.
<
(genrow n '(1)))
Line 275 ⟶ 271:
(if (> 2 (length l))
'(1)
(cons (+ (car l) (cadr l)) (newrow (cdr l)))))</
=={{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 ;
: (pascal) ( seq -- newseq )
Line 363 ⟶ 358:
: pascal ( n -- seq )
1 - { { 1 } } swap [ (pascal) ] times ;</lang>
It works as:
<lang factor>5 pascal .
{ { 1 } { 1 1 } { 1 2 1 } { 1 3 3 1 } { 1 4 6 4 1 } }</lang>
=={{header|Forth}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
Prints nothing for n<=0. Output formatting breaks down for n>20
<lang fortran>
=={{header|Groovy}}==
Line 454 ⟶ 446:
similar function
<lang haskell>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>
Now we can shift a list and add it to itself, extending it by keeping
the ends:
<lang haskell>extendWith f [] = []
extendWith f
And for the whole (infinite) triangle, we just iterate this operation,
starting with the first row:
<lang haskell>pascal = iterate (extendWith (+)) [1]</lang>
For the first ''n'' rows, we just take the first ''n'' elements from this
list, as in
<lang haskell>*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]]</lang>
A shorter approach, plagiarized from [http://www.haskell.org/haskellwiki/Blow_your_mind]
<lang haskell>-- generate next row from current row
nextRow row = zipWith (+) ([0] ++ row) (row ++ [0])
-- returns the first n rows
pascal = iterate nextRow [1]</lang>
=={{header|J}}==
=== solution ===
<lang j>!/~@i. N</lang>
=== 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
2598960</lang>
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
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>
and here's the addition table for 0 to 4
<lang j>+/~@i. 4
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6</lang>
Similarly, <tt>!/~@i.</tt> is the number-of-combinations table, or the "choose" table:
<lang j>!/~@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>
Of course, to format it nicely, you need to do a little more work:
<lang j>([:'0'&=`(,:&' ')} -@|. |."_1 [: ":@|: !/~)@i. 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1</lang>
(I won't bother explaining the formatting.)
Line 577 ⟶ 547:
print, r
End</lang>
=={{header|Java}}==
Line 635 ⟶ 604:
=={{header|Logo}}==
<lang logo>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>
=={{header|Metafont}}==
Line 672 ⟶ 639:
(pascal.nial)
Using it
=={{header|OCaml}}==
Line 761 ⟶ 728:
RapidQ does not require simple variables to be declared before use.
<lang rapidq>DEFINT values(100) = {0,1}
INPUT "Number of rows: "; nrows
Line 773 ⟶ 739:
NEXT i
PRINT
NEXT row</lang>
===Using binary coefficients===
{{trans|BASIC}}
<lang rapidq>INPUT "Number of rows: "; nrows
FOR row = 0 TO nrows-1
c = 1
Line 788 ⟶ 752:
NEXT i
PRINT
NEXT row</lang>
=={{header|Ruby}}==
<lang ruby>def pascal(n = 1)
return if n < 1
Line 819 ⟶ 781:
p
end</lang>
=={{header|Seed7}}==
Line 844 ⟶ 805:
writeln;
end for;
end func;</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 nat
pascal "n" = (next"n" sum*NiCixp) <1></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)
#0=0; #1=1
Ins_Char(' ', COUNT, #100*3-2) Num_Ins(1)
Line 993 ⟶ 950:
}
Ins_Newline
}</lang>
===Using binary coefficients===
{{trans|BASIC}}
<lang vedit>#1 = Get_Num("Number of rows: ", STATLINE)
for (#2 = 0; #2 < #1; #2++) {
#3 = 1
Line 1,008 ⟶ 963:
}
Ins_Newline
}</lang>
|