Jump to content

Pascal's triangle: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Frink)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 305:
1 3 3 1
</pre>
 
 
=={{header|AppleScript}}==
Line 764 ⟶ 763:
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
</lang>
 
 
 
=={{header|C}}==
Line 832 ⟶ 829:
free(prevRow);
}</lang>
 
=={{header|C sharp|C#}}==
{{trans|Fortran}}
Produces no output when n is less than or equal to zero.
 
<lang csharp>using System;
 
namespace RosettaCode {
 
class PascalsTriangle {
 
public static void CreateTriangle(int n) {
if (n > 0) {
for (int i = 0; i < n; i++) {
int c = 1;
Console.Write(" ".PadLeft(2 * (n - 1 - i)));
for (int k = 0; k <= i; k++) {
Console.Write("{0}", c.ToString().PadLeft(3));
c = c * (i - k) / (k + 1);
}
Console.WriteLine();
}
}
}
 
public static void Main() {
CreateTriangle(8);
}
}
}</lang>
 
===Arbitrarily large numbers (BigInteger), arbitrary row selection===
<lang csharp>using System;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
 
namespace RosettaCode
{
public static class PascalsTriangle
{
public static IEnumerable<BigInteger[]> GetTriangle(int quantityOfRows)
{
IEnumerable<BigInteger> range = Enumerable.Range(0, quantityOfRows).Select(num => new BigInteger(num));
return range.Select(num => GetRow(num).ToArray());
}
 
public static IEnumerable<BigInteger> GetRow(BigInteger rowNumber)
{
BigInteger denominator = 1;
BigInteger numerator = rowNumber;
 
BigInteger currentValue = 1;
for (BigInteger counter = 0; counter <= rowNumber; counter++)
{
yield return currentValue;
currentValue = BigInteger.Multiply(currentValue, numerator--);
currentValue = BigInteger.Divide(currentValue, denominator++);
}
yield break;
}
 
public static string FormatTriangleString(IEnumerable<BigInteger[]> triangle)
{
int maxDigitWidth = triangle.Last().Max().ToString().Length;
IEnumerable<string> rows = triangle.Select(arr =>
string.Join(" ", arr.Select(array => CenterString(array.ToString(), maxDigitWidth)) )
);
int maxRowWidth = rows.Last().Length;
return string.Join(Environment.NewLine, rows.Select(row => CenterString(row, maxRowWidth)));
}
 
private static string CenterString(string text, int width)
{
int spaces = width - text.Length;
int padLeft = (spaces / 2) + text.Length;
return text.PadLeft(padLeft).PadRight(width);
}
}
}</lang>
 
Example:
<lang csharp>static void Main()
{
IEnumerable<BigInteger[]> triangle = PascalsTriangle.GetTriangle(20);
string output = PascalsTriangle.FormatTriangleString(triangle)
Console.WriteLine(output);
}</lang>
 
{{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 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
1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1
1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1
1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1
</pre>
 
=={{header|C++}}==
Line 1,087 ⟶ 1,196:
 
</lang>
=={{header|C sharp|C#}}==
{{trans|Fortran}}
Produces no output when n is less than or equal to zero.
 
<lang csharp>using System;
 
namespace RosettaCode {
 
class PascalsTriangle {
 
public static void CreateTriangle(int n) {
if (n > 0) {
for (int i = 0; i < n; i++) {
int c = 1;
Console.Write(" ".PadLeft(2 * (n - 1 - i)));
for (int k = 0; k <= i; k++) {
Console.Write("{0}", c.ToString().PadLeft(3));
c = c * (i - k) / (k + 1);
}
Console.WriteLine();
}
}
}
 
public static void Main() {
CreateTriangle(8);
}
}
}</lang>
 
===Arbitrarily large numbers (BigInteger), arbitrary row selection===
<lang csharp>using System;
using System.Linq;
using System.Numerics;
using System.Collections.Generic;
 
namespace RosettaCode
{
public static class PascalsTriangle
{
public static IEnumerable<BigInteger[]> GetTriangle(int quantityOfRows)
{
IEnumerable<BigInteger> range = Enumerable.Range(0, quantityOfRows).Select(num => new BigInteger(num));
return range.Select(num => GetRow(num).ToArray());
}
 
public static IEnumerable<BigInteger> GetRow(BigInteger rowNumber)
{
BigInteger denominator = 1;
BigInteger numerator = rowNumber;
 
BigInteger currentValue = 1;
for (BigInteger counter = 0; counter <= rowNumber; counter++)
{
yield return currentValue;
currentValue = BigInteger.Multiply(currentValue, numerator--);
currentValue = BigInteger.Divide(currentValue, denominator++);
}
yield break;
}
 
public static string FormatTriangleString(IEnumerable<BigInteger[]> triangle)
{
int maxDigitWidth = triangle.Last().Max().ToString().Length;
IEnumerable<string> rows = triangle.Select(arr =>
string.Join(" ", arr.Select(array => CenterString(array.ToString(), maxDigitWidth)) )
);
int maxRowWidth = rows.Last().Length;
return string.Join(Environment.NewLine, rows.Select(row => CenterString(row, maxRowWidth)));
}
 
private static string CenterString(string text, int width)
{
int spaces = width - text.Length;
int padLeft = (spaces / 2) + text.Length;
return text.PadLeft(padLeft).PadRight(width);
}
}
}</lang>
 
Example:
<lang csharp>static void Main()
{
IEnumerable<BigInteger[]> triangle = PascalsTriangle.GetTriangle(20);
string output = PascalsTriangle.FormatTriangleString(triangle)
Console.WriteLine(output);
}</lang>
 
{{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 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
1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1
1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1
1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1
</pre>
 
=={{header|Clojure}}==
Line 1,509 ⟶ 1,507:
1 12 66 220 495 792 924 792 495 220 66 12 1
</pre>
 
=={{header|D}}==
===Less functional Version===
Line 2,037 ⟶ 2,036:
}
</lang>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Pascal%27s_triangle this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|Forth}}==
Line 2,231 ⟶ 2,222:
1 9 36 84 126 126 84 36 9 1
</pre>
 
=={{header|Fōrmulæ}}==
 
In [http://wiki.formulae.org/Pascal%27s_triangle this] page you can see the solution of this task.
 
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text ([http://wiki.formulae.org/Editing_F%C5%8Drmul%C3%A6_expressions more info]). 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 transportation effects more than visualization and edition.
 
The option to show Fōrmulæ programs and their results is showing images. Unfortunately images cannot be uploaded in Rosetta Code.
 
=={{header|GAP}}==
Line 2,448 ⟶ 2,447:
ENDDO
END</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
The code below is slightly modified from the library version of pascal which prints 0's to the full width of the carpet.
Line 3,031:
end)
end;</lang>
 
=={{header|Julia}}==
 
Line 3,204 ⟶ 3,205:
 
for [i 1 10] [print pascal :i]</lang>
 
=={{header|Lua}}==
<lang lua>
Line 3,233 ⟶ 3,235:
<lang Mathematica>Column[StringReplace[ToString /@ Replace[MatrixExp[SparseArray[
{Band[{2,1}] -> Range[n-1]},{n,n}]],{x__,0..}->{x},2] ,{"{"|"}"|","->" "}], Center]</lang>
[[File:MmaPascal.png]]
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 3,315 ⟶ 3,317:
"1 5 10 10 5 1"
"1 6 15 20 15 6 1" */</lang>
 
 
=={{header|Metafont}}==
Line 3,567 ⟶ 3,568:
 
For n = 0, prints nothing. For negative n, throws an exception.
 
=={{header|PARI/GP}}==
<lang parigp>pascals_triangle(N)= {
Line 3,697 ⟶ 3,699:
1 5 10 10 5 1
</pre>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-10-03}}
=== using a lazy sequence generator ===
 
The following routine returns a lazy list of lines using the sequence operator (<tt>...</tt>). With a lazy result you need not tell the routine how many you want; you can just use a slice subscript to get the first N lines:
<lang perl6>sub pascal {
[1], { [0, |$_ Z+ |$_, 0] } ... *
}
.say for pascal[^10];</lang>
 
One problem with the routine above is that it might recalculate the sequence each time you call it. Slightly more idiomatic would be to define the sequence as a lazy constant. Here we use the <tt>@</tt> sigil to indicate that the sequence should cache its values for reuse, and use an explicit parameter <tt>$prev</tt> for variety:
 
<lang perl6>constant @pascal = [1], -> $prev { [0, |$prev Z+ |$prev, 0] } ... *;
.say for @pascal[^10];</lang>
 
Since we use ordinary subscripting, non-positive inputs throw an index-out-of-bounds error.
 
=== recursive ===
 
{{trans|Haskell}}
 
<lang perl6>multi sub pascal (1) { $[1] }
multi sub pascal (Int $n where 2..*) {
my @rows = pascal $n - 1;
|@rows, [0, |@rows[*-1] Z+ |@rows[*-1], 0 ];
}
.say for pascal 10;</lang>
 
Non-positive inputs throw a multiple-dispatch error.
 
=== iterative ===
 
{{trans|Perl}}
<lang perl6>sub pascal ($n where $n >= 1) {
say my @last = 1;
for 1 .. $n - 1 -> $row {
@last = 1, |map({ @last[$_] + @last[$_ + 1] }, 0 .. $row - 2), 1;
say @last;
}
}
pascal 10;</lang>
 
Non-positive inputs throw a type check error.
 
{{Output}}
<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 1]
[1 9 36 84 126 126 84 36 9 1]</pre>
 
=={{header|Phix}}==
Line 3,789 ⟶ 3,731:
 
"Reflected" Pascal's triangle, it uses symmetry property to "mirror" second part. It determines even and odd strings. automatically.
 
=={{header|PHP}}==
<lang php>
Line 3,836 ⟶ 3,779:
?>
</lang> =={{header|PHP}}==
 
=={{header|PHP}}==
<lang php>function pascalsTriangle($num){
$c = 1;
Line 3,871 ⟶ 3,812:
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
 
=={{header|PicoLisp}}==
{{trans|C}}
<lang PicoLisp>(de pascalTriangle (N)
(for I N
(space (* 2 (- N I)))
(let C 1
(for K I
(prin (align 3 C) " ")
(setq C (*/ C (- I K) K)) ) )
(prinl) ) )</lang>
 
=={{header|PL/I}}==
Line 3,904 ⟶ 3,856:
1 10 45 120 210 252 210 120 45 10 1
</lang>
 
=={{header|PicoLisp}}==
{{trans|C}}
<lang PicoLisp>(de pascalTriangle (N)
(for I N
(space (* 2 (- N I)))
(let C 1
(for K I
(prin (align 3 C) " ")
(setq C (*/ C (- I K) K)) ) )
(prinl) ) )</lang>
 
=={{header|Potion}}==
Line 4,390 ⟶ 4,331:
 
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-10-03}}
=== using a lazy sequence generator ===
 
The following routine returns a lazy list of lines using the sequence operator (<tt>...</tt>). With a lazy result you need not tell the routine how many you want; you can just use a slice subscript to get the first N lines:
<lang perl6>sub pascal {
[1], { [0, |$_ Z+ |$_, 0] } ... *
}
.say for pascal[^10];</lang>
 
One problem with the routine above is that it might recalculate the sequence each time you call it. Slightly more idiomatic would be to define the sequence as a lazy constant. Here we use the <tt>@</tt> sigil to indicate that the sequence should cache its values for reuse, and use an explicit parameter <tt>$prev</tt> for variety:
 
<lang perl6>constant @pascal = [1], -> $prev { [0, |$prev Z+ |$prev, 0] } ... *;
.say for @pascal[^10];</lang>
 
Since we use ordinary subscripting, non-positive inputs throw an index-out-of-bounds error.
 
=== recursive ===
 
{{trans|Haskell}}
 
<lang perl6>multi sub pascal (1) { $[1] }
multi sub pascal (Int $n where 2..*) {
my @rows = pascal $n - 1;
|@rows, [0, |@rows[*-1] Z+ |@rows[*-1], 0 ];
}
.say for pascal 10;</lang>
 
Non-positive inputs throw a multiple-dispatch error.
 
=== iterative ===
 
{{trans|Perl}}
<lang perl6>sub pascal ($n where $n >= 1) {
say my @last = 1;
for 1 .. $n - 1 -> $row {
@last = 1, |map({ @last[$_] + @last[$_ + 1] }, 0 .. $row - 2), 1;
say @last;
}
}
pascal 10;</lang>
 
Non-positive inputs throw a type check error.
 
{{Output}}
<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 1]
[1 9 36 84 126 126 84 36 9 1]</pre>
 
=={{header|RapidQ}}==
Line 4,464 ⟶ 4,466:
;
13 pascalTriangle</lang>
 
 
=={{header|REXX}}==
Line 4,933 ⟶ 4,934:
:End
:[A]</lang>
 
=={{header|Turing}}==
 
Line 4,980 ⟶ 4,982:
0 OK, 0:380
</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
Line 5,077 ⟶ 5,080:
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</pre>
 
=={{header|VBScript}}==
Derived from the BASIC version.
Line 5,139 ⟶ 5,143:
Ins_Newline
}</lang>
 
 
=={{header|Visual Basic}}==
10,333

edits

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