Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,4 +1,5 @@
|
|||
[[wp:Pascal's triangle|Pascal's triangle]] is an arithmetic and geometric figure first imagined by [[wp:Blaise Pascal|Blaise Pascal]]. Its first few rows look like this:
|
||||
[[wp:Pascal's triangle|Pascal's triangle]] is an arithmetic and geometric figure first imagined by [[wp:Blaise Pascal|Blaise Pascal]].
|
||||
Its first few rows look like this:
|
||||
1
|
||||
1 1
|
||||
1 2 1
|
||||
|
|
|
|||
|
|
@ -1,7 +1 @@
|
|||
$ 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
|
||||
|
|
|
|||
11
Task/Pascals-triangle/Ada/pascals-triangle-1.ada
Normal file
11
Task/Pascals-triangle/Ada/pascals-triangle-1.ada
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package Pascal is
|
||||
|
||||
type Row is array (Natural range <>) of Natural;
|
||||
|
||||
function Length(R: Row) return Positive;
|
||||
|
||||
function First_Row(Max_Length: Positive) return Row;
|
||||
|
||||
function Next_Row(R: Row) return Row;
|
||||
|
||||
end Pascal;
|
||||
26
Task/Pascals-triangle/Ada/pascals-triangle-2.ada
Normal file
26
Task/Pascals-triangle/Ada/pascals-triangle-2.ada
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package body Pascal is
|
||||
|
||||
function First_Row(Max_Length: Positive) return Row is
|
||||
R: Row(0 .. Max_Length) := (0 | 1 => 1, others => 0);
|
||||
begin
|
||||
return R;
|
||||
end First_Row;
|
||||
|
||||
function Next_Row(R: Row) return Row is
|
||||
S: Row(R'Range);
|
||||
begin
|
||||
S(0) := Length(R)+1;
|
||||
S(Length(S)) := 1;
|
||||
for J in reverse 2 .. Length(R) loop
|
||||
S(J) := R(J)+R(J-1);
|
||||
end loop;
|
||||
S(1) := 1;
|
||||
return S;
|
||||
end Next_Row;
|
||||
|
||||
function Length(R: Row) return Positive is
|
||||
begin
|
||||
return R(0);
|
||||
end Length;
|
||||
|
||||
end Pascal;
|
||||
18
Task/Pascals-triangle/Ada/pascals-triangle-3.ada
Normal file
18
Task/Pascals-triangle/Ada/pascals-triangle-3.ada
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Command_Line, Pascal; use Pascal;
|
||||
|
||||
procedure Triangle is
|
||||
|
||||
Number_Of_Rows: Positive := Integer'Value(Ada.Command_Line.Argument(1));
|
||||
Row: Pascal.Row := First_Row(Number_Of_Rows);
|
||||
|
||||
begin
|
||||
loop
|
||||
-- print one row
|
||||
for J in 1 .. Length(Row) loop
|
||||
Ada.Integer_Text_IO.Put(Row(J), 5);
|
||||
end loop;
|
||||
Ada.Text_IO.New_Line;
|
||||
exit when Length(Row) >= Number_Of_Rows;
|
||||
Row := Next_Row(Row);
|
||||
end loop;
|
||||
end Triangle;
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Pascals_Triangle is
|
||||
type Row is array(Positive range <>) of Integer;
|
||||
type Row_Access is access Row;
|
||||
type Triangle is array(Positive range <>) of Row_Access;
|
||||
function General_Triangle(Depth : Positive) return Triangle is
|
||||
Result : Triangle(1..Depth);
|
||||
begin
|
||||
for I in Result'range loop
|
||||
Result(I) := new Row(1..I);
|
||||
for J in 1..I loop
|
||||
if J = Result(I)'First or else J = Result(I)'Last then
|
||||
Result(I)(J) := 1;
|
||||
else
|
||||
Result(I)(J) := Result(I - 1)(J - 1) + Result(I - 1)(J);
|
||||
end if;
|
||||
end loop;
|
||||
end loop;
|
||||
return Result;
|
||||
end General_Triangle;
|
||||
procedure Print(Item : Triangle) is
|
||||
begin
|
||||
for I in Item'range loop
|
||||
for J in 1..I loop
|
||||
Put(Item => Item(I)(J), Width => 3);
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Print;
|
||||
begin
|
||||
Print(General_Triangle(7));
|
||||
end Pascals_Triangle;
|
||||
|
|
@ -1,26 +1,80 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include<cstdio>
|
||||
using namespace std;
|
||||
void Pascal_Triangle(int size) {
|
||||
|
||||
void genPyrN(int rows) {
|
||||
if (rows < 0) return;
|
||||
// save the last row here
|
||||
std::vector<int> last(1, 1);
|
||||
std::cout << last[0] << std::endl;
|
||||
int a[100][100];
|
||||
int i, j;
|
||||
|
||||
for (int i = 1; i <= rows; i++) {
|
||||
// work on the next row
|
||||
std::vector<int> thisRow;
|
||||
thisRow.reserve(i+1);
|
||||
thisRow.push_back(last.front()); // beginning of row
|
||||
std::transform(last.begin(), last.end()-1, last.begin()+1, std::back_inserter(thisRow), std::plus<int>()); // middle of row
|
||||
thisRow.push_back(last.back()); // end of row
|
||||
//first row and first coloumn has the same value=1
|
||||
for (i = 1; i <= size; i++) {
|
||||
a[i][1] = a[1][i] = 1;
|
||||
}
|
||||
|
||||
//Generate the full Triangle
|
||||
for (i = 2; i <= size; i++) {
|
||||
for (j = 2; j <= size - i; j++) {
|
||||
if (a[i - 1][j] == 0 || a[i][j - 1] == 0) {
|
||||
break;
|
||||
}
|
||||
a[i][j] = a[i - 1][j] + a[i][j - 1];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
1 1 1 1
|
||||
1 2 3
|
||||
1 3
|
||||
1
|
||||
|
||||
first print as above format-->
|
||||
|
||||
for (i = 1; i < size; i++) {
|
||||
for (j = 1; j < size; j++) {
|
||||
if (a[i][j] == 0) {
|
||||
break;
|
||||
}
|
||||
printf("%8d",a[i][j]);
|
||||
}
|
||||
cout<<"\n\n";
|
||||
}*/
|
||||
|
||||
// standard Pascal Triangle Format
|
||||
|
||||
int row,space;
|
||||
for (i = 1; i < size; i++) {
|
||||
space=row=i;
|
||||
j=1;
|
||||
|
||||
while(space<=size+(size-i)+1){
|
||||
cout<<" ";
|
||||
space++;
|
||||
}
|
||||
|
||||
while(j<=i){
|
||||
if (a[row][j] == 0){
|
||||
break;
|
||||
}
|
||||
|
||||
if(j==1){
|
||||
printf("%d",a[row--][j++]);
|
||||
}
|
||||
else
|
||||
printf("%6d",a[row--][j++]);
|
||||
}
|
||||
cout<<"\n\n";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
//freopen("out.txt","w",stdout);
|
||||
|
||||
int size;
|
||||
cin>>size;
|
||||
Pascal_Triangle(size);
|
||||
}
|
||||
|
||||
for (int j = 0; j <= i; j++)
|
||||
std::cout << thisRow[j] << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
last.swap(thisRow);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
5
Task/Pascals-triangle/Clojure/pascals-triangle-4.clj
Normal file
5
Task/Pascals-triangle/Clojure/pascals-triangle-4.clj
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(def pascal
|
||||
(iterate #(concat [1]
|
||||
(map + % (rest %))
|
||||
[1])
|
||||
[1]))
|
||||
1
Task/Pascals-triangle/Clojure/pascals-triangle-5.clj
Normal file
1
Task/Pascals-triangle/Clojure/pascals-triangle-5.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
(take 10 pascal) ; returns a list of the first 10 pascal rows
|
||||
1
Task/Pascals-triangle/Clojure/pascals-triangle-6.clj
Normal file
1
Task/Pascals-triangle/Clojure/pascals-triangle-6.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
(nth pascal 10) ;returns the nth row
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
pascal:{(x-1){+':0,x,0}\1}
|
||||
pascal:{(x-1){1_ +':0,x,0}\1}
|
||||
|
||||
pascal 6
|
||||
(1
|
||||
|
|
|
|||
|
|
@ -1,7 +1,3 @@
|
|||
multi pascal (1) { [1] }
|
||||
multi pascal (Int $n where 2..*) {
|
||||
my @rows = pascal $n - 1;
|
||||
@rows, [0, @rows[*-1][] Z+ @rows[*-1][], 0 )];
|
||||
}
|
||||
sub pascal { [1], { [0, @^p Z+ @^p, 0] } ... * }
|
||||
|
||||
say .perl for pascal 10;
|
||||
.say for pascal[^10];
|
||||
|
|
|
|||
|
|
@ -1,8 +1,3 @@
|
|||
sub pascal ($n where $n >= 1) {
|
||||
# Prints out $n rows of Pascal's triangle.
|
||||
say my @last = 1;
|
||||
for 1 .. $n - 1 -> $row {
|
||||
my @this = map { @last[$_] + @last[$_ + 1] }, 0 .. $row - 2;
|
||||
say @last = 1, @this, 1;
|
||||
}
|
||||
}
|
||||
constant Pascal = [1], { [0, @^p Z+ @^p, 0] } ... *;
|
||||
|
||||
.say for Pascal[^10];
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
sub pascal { [1], -> @p { [0, @p Z+ @p, 0] } ... * }
|
||||
multi pascal (1) { [1] }
|
||||
multi pascal (Int $n where 2..*) {
|
||||
my @rows = pascal $n - 1;
|
||||
@rows, [0, @rows[*-1][] Z+ @rows[*-1][], 0 )];
|
||||
}
|
||||
|
||||
.say for pascal[^10];
|
||||
.say for pascal 10;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,9 @@
|
|||
constant Pascal = [1], -> @p { [0, @p Z+ @p, 0] } ... *;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
.say for Pascal[^10];
|
||||
pascal 10;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,7 @@
|
|||
sub pascal
|
||||
# Prints out $n rows of Pascal's triangle. It returns undef for
|
||||
# failure and 1 for success.
|
||||
{my $n = shift;
|
||||
$n < 1 and return undef;
|
||||
print "1\n";
|
||||
$n == 1 and return 1;
|
||||
my @last = (1);
|
||||
foreach my $row (1 .. $n - 1)
|
||||
{my @this = map {$last[$_] + $last[$_ + 1]} 0 .. $row - 2;
|
||||
@last = (1, @this, 1);
|
||||
print join(' ', @last), "\n";}
|
||||
return 1;}
|
||||
my $rows = shift;
|
||||
my @next = (1);
|
||||
for my $n (1 .. $rows) {
|
||||
print "@next\n";
|
||||
@next = (1, (map $next[$_]+$next[$_+1], 0 .. $n-2), 1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
use bignum;
|
||||
sub pascal { $_[0] ? unpack "A(A6)*", 1000001**$_[0] : 1 }
|
||||
print "@{[map -+-$_, pascal $_]}\n" for 0..22;
|
||||
use ntheory qw/binomial/;
|
||||
sub pascal {
|
||||
my $rows = shift;
|
||||
for my $n (0 .. $rows-1) {
|
||||
print join(" ", map { binomial($n,$_) } 0 .. $n), "\n";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
3
Task/Pascals-triangle/Perl/pascals-triangle-3.pl
Normal file
3
Task/Pascals-triangle/Perl/pascals-triangle-3.pl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
use bignum;
|
||||
sub pascal_line { $_[0] ? unpack "A(A6)*", 1000001**$_[0] : 1 }
|
||||
sub pascal { print "@{[map -+-$_, pascal_line $_]}\n" for 0..$_[0]-1 }
|
||||
|
|
@ -1,27 +1,10 @@
|
|||
def pascal(n = 1)
|
||||
|
||||
return if n < 1
|
||||
|
||||
# set up a new array of arrays with the first value
|
||||
p = [[1]]
|
||||
|
||||
# for n - 1 number of times,
|
||||
(n - 1).times do |i|
|
||||
|
||||
# inject a new array starting with [1]
|
||||
p << p[i].inject([1]) do |result, elem|
|
||||
|
||||
# if we've reached the end, tack on a 1.
|
||||
# else, tack on current elem + previous elem
|
||||
if p[i].length == result.length
|
||||
result << 1
|
||||
else
|
||||
result << elem + p[i][result.length]
|
||||
end
|
||||
end
|
||||
def pascal(n)
|
||||
raise ArgumentError, "must be positive." if n < 1
|
||||
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 }
|
||||
end
|
||||
|
||||
# and return the triangle.
|
||||
p
|
||||
|
||||
end
|
||||
|
||||
pascal(8){|row| puts row.join(" ").center(20)}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
def next_row row ; ([0] + row).zip(row + [0]).collect {|l,r| l + r }; end
|
||||
def next_row(row) ([0] + row).zip(row + [0]).collect {|l,r| l + r } end
|
||||
|
||||
def pascal n ; ([nil] * n).inject([1]) {|x,y| y = next_row x } ; end
|
||||
def pascal(n) n.times.inject([1]) {|x,_| next_row x } end
|
||||
|
||||
8.times{|i| p pascal(i)}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue