Pascal's triangle: Difference between revisions

no edit summary
(Added method using combinations)
No edit summary
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 44:
Print(General_Triangle(7));
end Pascals_Triangle;
</adalang>
=={{header|ALGOL 68}}==
<pre>
Line 90:
If the user enters value less than 1, the first row is still always displayed.
 
<lang freebasic>DIM i AS Integer
DIM row AS Integer
DIM nrows AS Integer
Line 105:
NEXT i
PRINT
NEXT row</freebasiclang>
 
===Using binary coefficients===
Line 112:
 
If the user enters value less than 1, nothing is displayed.
<lang freebasic>DIM c AS Integer
DIM i AS Integer
DIM row AS Integer
Line 126:
NEXT i
PRINT
NEXT row</freebasiclang>
 
Note: Unlike most Basic implementations, FreeBASIC requires that all variables have been declared before use
Line 132:
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <algorithm>
#include <vector>
Line 157:
last.swap(thisRow);
}
}</cpplang>
 
=={{header|Clojure}}==
Line 193:
=={{header|D}}==
There is similarity between Pascal's triangle and [[Sierpinski triangle]]. Their difference are the initial line and the operation that act on the line element to produce next line. The following is a generic pascal's triangle implementation for positive number of lines output (n).
<lang d>import std.stdio, std.string, std.format : fmx = format ;
 
string Pascal(alias dg, T, T initValue)(int n) {
Line 229:
// an order 4 sierpinski triangle is a 2^4 lines generic pascal triangle with xor operation
foreach(i ; [16]) writef(sierpinski(i)) ;
}</dlang>
 
=={{header|Forth}}==
Line 392:
===Summing from Previous Rows===
{{works with|Java|1.5+}}
<lang java>import java.util.ArrayList;
...//class definition, etc.
public static void genPyrN(int rows){
Line 412:
System.out.println(thisRow);
}
}</javalang>
===Combinations===
This method is limited to 21 rows because of the limits of <tt>long</tt>. Calling <tt>pas</tt> with an argument of 22 or above will cause intermediate math to wrap around and give false answers.
<lang java>public class Pas{
public static void main(String[] args){
//usage
Line 441:
return ans;
}
}</javalang>
 
=={{header|Nial}}==
Line 458:
=={{header|OCaml}}==
 
<lang ocaml>(* generate next row from current row *)
let next_row row =
List.map2 (+) ([0] @ row) (row @ [0])
Line 467:
if i = n then []
else row :: loop (i+1) (next_row row)
in loop 0 [1]</ocamllang>
 
=={{header|Perl}}==
<lang perl>sub pascal
# Prints out $n rows of Pascal's triangle. It returns undef for
# failure and 1 for success.
Line 482:
@last = (1, @this, 1);
print join(' ', @last), "\n";}
return 1;}</perllang>
 
=={{header|Python}}==
<lang python>def pascal(n):
"""Prints out n rows of Pascal's triangle.
 
Line 497:
last = [1] + this + [1]
print " ".join(map(str, last))
return True</pythonlang>
 
=={{header|RapidQ}}==
Line 543:
 
=={{header|Ruby}}==
<lang ruby>
def pascal(n = 1)
Line 571:
end
</rubylang>
 
=={{header|Vedit macro language}}==