June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,4 +1,4 @@
[[wp:Pascal's triangle|Pascal's triangle]]   is an arithmetic and geometric figure first imagined by   [[wp:Blaise Pascal|Blaise Pascal]].
[[wp:Pascal's triangle|Pascal's triangle]] is an arithmetic and geometric figure often associated with the name of [[wp:Blaise Pascal|Blaise Pascal]], but also studied centuries earlier in India, Persia, China and elsewhere.
Its first few rows look like this:
1

View file

@ -0,0 +1,24 @@
10 INPUT "HOW MANY";N
20 IF N<1 THEN END
30 DIM C(N)
40 DIM D(N)
50 LET C(1)=1
60 LET D(1)=1
70 FOR J=1 TO N
80 FOR I=1 TO N-J+1
90 PRINT " ";
100 NEXT I
110 FOR I=1 TO J
120 PRINT C(I)" ";
130 NEXT I
140 PRINT
150 IF J=N THEN END
160 C(J+1)=1
170 D(J+1)=1
180 FOR I=1 TO J-1
190 D(I+1)=C(I)+C(I+1)
200 NEXT I
210 FOR I=1 TO J
220 C(I)=D(I)
230 NEXT I
240 NEXT J

View file

@ -0,0 +1,12 @@
RUN
HOW MANY? 8
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
READY.

View file

@ -0,0 +1,33 @@
MODULE Pascal;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
PROCEDURE PrintLine(n : INTEGER);
VAR
buf : ARRAY[0..63] OF CHAR;
m,j : INTEGER;
BEGIN
IF n<1 THEN RETURN END;
m := 1;
WriteString("1 ");
FOR j:=1 TO n-1 DO
m := m * (n - j) DIV j;
FormatString("%i ", buf, m);
WriteString(buf)
END;
WriteLn
END PrintLine;
PROCEDURE Print(n : INTEGER);
VAR i : INTEGER;
BEGIN
FOR i:=1 TO n DO
PrintLine(i)
END
END Print;
BEGIN
Print(10);
ReadChar
END Pascal.

View file

@ -0,0 +1,34 @@
#!/usr/bin/perl
use strict;
use warnings;
{
my @tartaglia ;
sub tartaglia {
my ($x,$y) = @_;
if ($x == 0 or $y == 0) { $tartaglia[$x][$y]=1 ; return 1};
my $ret ;
foreach my $yps (0..$y){
$ret += ( $tartaglia[$x-1][$yps] || tartaglia($x-1,$yps) );
}
$tartaglia[$x][$y] = $ret;
return $ret;
}
}
sub tartaglia_row {
my $y = shift;
my $x = 0;
my @row;
$row[0] = &tartaglia($x,$y+1);
foreach my $pos (0..$y-1) {push @row, tartaglia(++$x,--$y)}
return @row;
}
for (0..5) {print join ' ', tartaglia_row($_),"\n"}
print "\n\n";
print tartaglia(3,3),"\n";
my @third = tartaglia_row(5);
print "@third\n";

View file

@ -3,7 +3,7 @@ def pascal(n)
yield ar = [1]
(n-1).times do
ar.unshift(0).push(0) # tack a zero on both ends
yield ar = ar.each_cons(2).map{|a, b| a + b }
yield ar = ar.each_cons(2).map(&:sum)
end
end

View file

@ -0,0 +1,15 @@
fn pascal_triangle(n: u64)
{
for i in 0..n {
let mut c = 1;
for _j in 1..2*(n-1-i)+1 {
print!(" ");
}
for k in 0..i+1 {
print!("{:2} ", c);
c = c * (i-k)/(k+1);
}
println!();
}
}

View file

@ -1,5 +1,5 @@
def tri(row:Int):List[Int] = { row match {
case 1 => List(1)
case n:Int => List(1) ::: ((tri(n-1) zip tri(n-1).tail) map {case (a,b) => a+b}) ::: List(1)
}
}
def tri(row: Int): List[Int] =
row match {
case 1 => List(1)
case n: Int => 1 +: ((tri(n - 1) zip tri(n - 1).tail) map { case (a, b) => a + b }) :+ 1
}

View file

@ -1,2 +1,3 @@
def prettytri(n:Int) = (1 to n) foreach {i=>print(" "*(n-i)); tri(i) map (c=>print(c+" ")); println}
prettytri(5)
def prettyTri(n:Int) = (1 to n) foreach {i => print(" "*(n-i)); tri(i) map (c => print(c + " ")); println}
prettyTri(5)

View file

@ -0,0 +1,10 @@
object Blaise extends App {
def pascalTriangle(): Stream[Vector[Int]] =
Vector(1) #:: Stream.iterate(Vector(1, 1))(1 +: _.sliding(2).map(_.sum).toVector :+ 1)
val output = pascalTriangle().take(15).map(_.mkString(" "))
val longest = output.last.length
println("Pascal's Triangle")
output.foreach(line => println(s"${" " * ((longest - line.length) / 2)}$line"))
}

View file

@ -0,0 +1,26 @@
function pascal1(n) {
return(comb(J(1,n,0::n-1),J(n,1,0..n-1)))
}
function pascal2(n) {
a = I(n)
a[.,1] = J(n,1,1)
for (i=3; i<=n; i++) {
a[i,2..i-1] = a[i-1,2..i-1]+a[i-1,1..i-2]
}
return(a)
}
function pascal3(n) {
a = J(n,n,0)
for (i=1; i<n; i++) {
a[i+1,i] = i
}
s = p = I(n)
k = 1
for (i=0; i<n; i++) {
p = p*a/k++
s = s+p
}
return(s)
}

View file

@ -0,0 +1,16 @@
function print_pascal_triangle(n) {
a = pascal1(n)
for (i=1; i<=n; i++) {
for (j=1; j<=i; j++) {
printf("%10.0f",a[i,j])
}
printf("\n")
}
}
print_pascal_triangle(5)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

View file

@ -0,0 +1,24 @@
10 INPUT "How many rows? ";n
15 IF n<1 THEN GO TO 210
20 DIM c(n)
25 DIM d(n)
30 LET c(1)=1
35 LET d(1)=1
40 FOR r=1 TO n
50 FOR i=1 TO (n-r)
60 PRINT " ";
70 NEXT i
80 FOR i=1 TO r
90 PRINT c(i);" ";
100 NEXT i
110 PRINT
120 IF r>=n THEN GO TO 140
130 LET d(r+1)=1
140 FOR i=2 TO r
150 LET d(i)=c(i-1)+c(i)
160 NEXT i
165 IF r>=n THEN GO TO 200
170 FOR i=1 TO r+1
180 LET c(i)=d(i)
190 NEXT i
200 NEXT r

View file

@ -0,0 +1 @@
RUN