Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,38 @@
* Pascal's triangle 25/10/2015
PASCAL CSECT
USING PASCAL,R15 set base register
LA R7,1 n=1
LOOPN C R7,=A(M) do n=1 to m
BH ELOOPN if n>m then goto
MVC U,=F'1' u(1)=1
LA R8,PG pgi=@pg
LA R6,1 i=1
LOOPI CR R6,R7 do i=1 to n
BH ELOOPI if i>n then goto
LR R1,R6 i
SLA R1,2 i*4
L R3,T-4(R1) t(i)
L R4,T(R1) t(i+1)
AR R3,R4 t(i)+t(i+1)
ST R3,U(R1) u(i+1)=t(i)+t(i+1)
LR R1,R6 i
SLA R1,2 i*4
L R2,U-4(R1) u(i)
XDECO R2,XD edit u(i)
MVC 0(4,R8),XD+8 output u(i):4
LA R8,4(R8) pgi=pgi+4
LA R6,1(R6) i=i+1
B LOOPI end i
ELOOPI MVC T((M+1)*(L'T)),U t=u
XPRNT PG,80 print
LA R7,1(R7) n=n+1
B LOOPN end n
ELOOPN XR R15,R15 set return code
BR R14 return to caller
M EQU 11 <== input
T DC (M+1)F'0' t(m+1) init 0
U DC (M+1)F'0' u(m+1) init 0
PG DC CL80' ' pg init ' '
XD DS CL12 temp
YREGS
END PASCAL

View file

@ -0,0 +1,39 @@
@echo off
setlocal enabledelayedexpansion
::The Main Thing...
cls
echo.
set row=15
call :pascal
echo.
pause
exit /b 0
::/The Main Thing.
::The Functions...
:pascal
set /a prev=%row%-1
for /l %%I in (0,1,%prev%) do (
set c=1&set r=
for /l %%K in (0,1,%row%) do (
if not !c!==0 (
call :numstr !c!
set r=!r!!space!!c!
)
set /a c=!c!*^(%%I-%%K^)/^(%%K+1^)
)
echo !r!
)
goto :EOF
:numstr
::This function returns the number of whitespaces to be applied on each numbers.
set cnt=0&set proc=%1&set space=
:loop
set currchar=!proc:~%cnt%,1!
if not "!currchar!"=="" set /a cnt+=1&goto loop
set /a numspaces=5-!cnt!
for /l %%A in (1,1,%numspaces%) do set "space=!space! "
goto :EOF
::/The Functions.

View file

@ -0,0 +1,3 @@
0" :swor fo rebmuN">:#,_&> 55+, v
v01*p00-1:g00.:<1p011p00:\-1_v#:<
>g:1+10p/48*,:#^_$ 55+,1+\: ^>$$@

View file

@ -0,0 +1,85 @@
// Compile with -std=c++11
#include<iostream>
#include<vector>
using namespace std;
void print_vector(vector<int> dummy){
for (vector<int>::iterator i = dummy.begin(); i != dummy.end(); ++i)
cout<<*i<<" ";
cout<<endl;
}
void print_vector_of_vectors(vector<vector<int>> dummy){
for (vector<vector<int>>::iterator i = dummy.begin(); i != dummy.end(); ++i)
print_vector(*i);
cout<<endl;
}
vector<vector<int>> dynamic_triangle(int dummy){
vector<vector<int>> result;
if (dummy > 0){ // if the argument is 0 or negative exit immediately
vector<int> row;
// The first row
row.push_back(1);
result.push_back(row);
// The second row
if (dummy > 1){
row.clear();
row.push_back(1); row.push_back(1);
result.push_back(row);
}
// The other rows
if (dummy > 2){
for (int i = 2; i < dummy; i++){
row.clear();
row.push_back(1);
for (int j = 1; j < i; j++)
row.push_back(result.back().at(j - 1) + result.back().at(j));
row.push_back(1);
result.push_back(row);
}
}
}
return result;
}
vector<vector<int>> static_triangle(int dummy){
vector<vector<int>> result;
if (dummy > 0){ // if the argument is 0 or negative exit immediately
vector<int> row;
result.resize(dummy); // This should work faster than consecutive push_back()s
// The first row
row.resize(1);
row.at(0) = 1;
result.at(0) = row;
// The second row
if (result.size() > 1){
row.resize(2);
row.at(0) = 1; row.at(1) = 1;
result.at(1) = row;
}
// The other rows
if (result.size() > 2){
for (int i = 2; i < result.size(); i++){
row.resize(i + 1); // This should work faster than consecutive push_back()s
row.front() = 1;
for (int j = 1; j < row.size() - 1; j++)
row.at(j) = result.at(i - 1).at(j - 1) + result.at(i - 1).at(j);
row.back() = 1;
result.at(i) = row;
}
}
}
return result;
}
int main(){
vector<vector<int>> triangle;
int n;
cout<<endl<<"The Pascal's Triangle"<<endl<<"Enter the number of rows: ";
cin>>n;
// Call the dynamic function
triangle = dynamic_triangle(n);
cout<<endl<<"Calculated using dynamic vectors:"<<endl<<endl;
print_vector_of_vectors(triangle);
// Call the static function
triangle = static_triangle(n);
cout<<endl<<"Calculated using static vectors:"<<endl<<endl;
print_vector_of_vectors(triangle);
return 0;
}

View file

@ -0,0 +1,80 @@
// Compile with -std=c++11
#include<iostream>
#include<vector>
using namespace std;
class pascal_triangle{
vector<vector<int>> data; // This is the actual data
void print_row(vector<int> dummy){
for (vector<int>::iterator i = dummy.begin(); i != dummy.end(); ++i)
cout<<*i<<" ";
cout<<endl;
}
public:
pascal_triangle(int dummy){ // Everything is done on the construction phase
if (dummy > 0){ // if the argument is 0 or negative exit immediately
vector<int> row;
data.resize(dummy); // Theoretically this should work faster than consecutive push_back()s
// The first row
row.resize(1);
row.at(0) = 1;
data.at(0) = row;
// The second row
if (data.size() > 1){
row.resize(2);
row.at(0) = 1; row.at(1) = 1;
data.at(1) = row;
}
// The other rows
if (data.size() > 2){
for (int i = 2; i < data.size(); i++){
row.resize(i + 1); // Theoretically this should work faster than consecutive push_back()s
row.front() = 1;
for (int j = 1; j < row.size() - 1; j++)
row.at(j) = data.at(i - 1).at(j - 1) + data.at(i - 1).at(j);
row.back() = 1;
data.at(i) = row;
}
}
}
}
~pascal_triangle(){
for (vector<vector<int>>::iterator i = data.begin(); i != data.end(); ++i)
i->clear(); // I'm not sure about the necessity of this loop!
data.clear();
}
void print_row(int dummy){
if (dummy < data.size())
for (vector<int>::iterator i = data.at(dummy).begin(); i != data.at(dummy).end(); ++i)
cout<<*i<<" ";
cout<<endl;
}
void print(){
for (int i = 0; i < data.size(); i++)
print_row(i);
}
int get_coeff(int dummy1, int dummy2){
int result = 0;
if ((dummy1 < data.size()) && (dummy2 < data.at(dummy1).size()))
result = data.at(dummy1).at(dummy2);
return result;
}
vector<int> get_row(int dummy){
vector<int> result;
if (dummy < data.size())
result = data.at(dummy);
return result;
}
};
int main(){
int n;
cout<<endl<<"The Pascal's Triangle with a class!"<<endl<<endl<<"Enter the number of rows: ";
cin>>n;
pascal_triangle myptri(n);
cout<<endl<<"The whole triangle:"<<endl;
myptri.print();
cout<<endl<<"Just one row:"<<endl;
myptri.print_row(n/2);
cout<<endl<<"Just one coefficient:"<<endl;
cout<<myptri.get_coeff(n/2, n/4)<<endl<<endl;
return 0;
}

View file

@ -0,0 +1,21 @@
program PascalsTriangle;
procedure Pascal(r:Integer);
var
i, c, k:Integer;
begin
for i := 0 to r - 1 do
begin
c := 1;
for k := 0 to i do
begin
Write(c:3);
c := c * (i - k) div (k + 1);
end;
Writeln;
end;
end;
begin
Pascal(9);
end.

View file

@ -0,0 +1,12 @@
defmodule Pascal do
def triangle(n), do: triangle(n,[1])
def triangle(0,list), do: list
def triangle(n,list) do
IO.inspect list
new_list = Enum.zip([0]++list, list++[0]) |> Enum.map(fn {a,b} -> a+b end)
triangle(n-1,new_list)
end
end
Pascal.triangle(8)

View file

@ -0,0 +1,72 @@
(function (n) {
// A Pascal triangle of n rows
// n --> [[n]]
function pascalTriangle(n) {
// Sums of each consecutive pair of numbers
// [n] --> [n]
function pairSums(lst) {
return lst.reduce(function (acc, n, i, l) {
var iPrev = i ? i - 1 : 0;
return i ? acc.concat(l[iPrev] + l[i]) : acc
}, []);
}
// Next line in a Pascal triangle series
// [n] --> [n]
function nextPascal(lst) {
return lst.length ? [1].concat(
pairSums(lst)
).concat(1) : [1];
}
// Each row is a function of the preceding row
return n ? Array.apply(null, Array(n - 1)).reduce(
function (a, _, i) {
return a.concat([nextPascal(a[i])]);
}, [[1]]) : [];
}
// TEST
var lstTriangle = pascalTriangle(n);
// FORMAT OUTPUT AS WIKI TABLE
// [[a]] -> bool -> s -> s
function wikiTable(lstRows, blnHeaderRow, strStyle) {
return '{| class="wikitable" ' + (
strStyle ? 'style="' + strStyle + '"' : ''
) + lstRows.map(function (lstRow, iRow) {
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
return typeof v === 'undefined' ? ' ' : v;
}).join(' ' + strDelim + strDelim + ' ');
}).join('') + '\n|}';
}
var lstLastLine = lstTriangle.slice(-1)[0],
lngBase = (lstLastLine.length * 2) - 1,
nWidth = lstLastLine.reduce(function (a, x) {
var d = x.toString().length;
return d > a ? d : a;
}, 1) * lngBase;
return [
wikiTable(
lstTriangle.map(function (lst) {
return lst.join(';;').split(';');
}).map(function (line, i) {
var lstPad = Array((lngBase - line.length) / 2);
return lstPad.concat(line).concat(lstPad);
}),
false,
'text-align:center;width:' + nWidth + 'em;height:' + nWidth +
'em;table-layout:fixed;'
),
JSON.stringify(lstTriangle)
].join('\n\n');
})(7);

View file

@ -0,0 +1 @@
[[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]]

View file

@ -1,6 +1,5 @@
pascal:{(x-1){1_ +':0,x,0}\1}
pascal 6
pascal:{(x-1){+':x,0}\1}
pascal 6
(1
1 1
1 2 1

View file

@ -0,0 +1,18 @@
fun pas(rows: Int) {
for (i in 0..rows - 1) {
for (j in 0..i)
print(ncr(i, j).toString() + " ")
println()
}
}
fun ncr(n: Int, r: Int) = fact(n) / (fact(r) * fact(n - r))
fun fact(n: Int) : Long {
var ans = 1.toLong()
for (i in 2..n)
ans *= i
return ans
}
fun main(args: Array<String>) = pas(args[0].toInt())

View file

@ -1,3 +1,3 @@
sub pascal { [1], { [0, @^p Z+ @^p, 0] } ... * }
sub pascal { [1], -> $prev { [0, |$prev Z+ |$prev, 0] } ... * }
.say for pascal[^10];

View file

@ -1,3 +1,3 @@
constant Pascal = [1], { [0, @^p Z+ @^p, 0] } ... *;
constant @pascal = [1], -> $prev { [0, |$prev Z+ |$prev, 0] } ... *;
.say for Pascal[^10];
.say for @pascal[^10];

View file

@ -1,7 +1,7 @@
multi pascal (1) { [1] }
multi pascal (Int $n where 2..*) {
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 )];
|@rows, [0, |@rows[*-1] Z+ |@rows[*-1], 0 ];
}
.say for pascal 10;

View file

@ -1,7 +1,7 @@
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;
@last = 1, |map({ @last[$_] + @last[$_ + 1] }, 0 .. $row - 2), 1;
say @last;
}
}

View file

@ -1,7 +1,8 @@
sub pascal
sub pascal {
my $rows = shift;
my @next = (1);
for my $n (1 .. $rows) {
print "@next\n";
@next = (1, (map $next[$_]+$next[$_+1], 0 .. $n-2), 1);
}
}

View file

@ -1,28 +1,25 @@
/*REXX program to display Pascal's triangle, neatly centered/formatted.*/
/*AKA: Yang Hui's ▲, Khayyam-Pascal ▲, Kyayyam ▲, Tartaglia's ▲ */
numeric digits 1000 /*let's be able to handle big ▲. */
arg nn .; if nn=='' then nn=10; n=abs(nn)
a. = 1 /*if NN < 0, output is to a file.*/
mx = !(n-1) / !(n%2) / !(n-1-n%2) /*MX =biggest number in triangle.*/
w = length(mx) /* W =width of biggest number. */
line. = 1
do row=1 for n; prev=row-1
a.row.1 = 1
do j=2 to row-1; jm=j-1
a.row.j = a.prev.jm + a.prev.j
line.row = line.row right(a.row.j,w)
end /*j*/
if row\==1 then line.row=line.row right(1,w) /*append the last "1".*/
end /*row*/
width=length(line.n) /*width of last line in triangle.*/
do L=1 for n /*show lines in Pascal's triangle*/
if nn>0 then say center(line.L,width) /*either SAY or write.*/
else call lineout 'PASCALS.'n, center(line.L,width)
end /*L*/
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────────────! (factorial) subroutine─────────*/
!: procedure; arg x;!=1;do j=2 to x;!=!*j;end;return ! /*calc. factorial*/
/*REXX program displays Pascal's triangle (centered/formatted); also known as:*/
/*────────── Yang Hui's, Khayyam─Pascal, Kyayyam, and/or Tartaglia's triangle.*/
numeric digits 3000 /*be able to handle gihugeic triangles.*/
parse arg nn .; if nn=='' then nn=10 /*use default if NN wasn't specified.*/
N=abs(nn) /*N is the number of rows in triangle.*/
@.=1; $.=@. /*default value for rows and for lines.*/
w=length(!(N-1) / !(N%2) / !(N-1-N%2)) /*W is the width of the biggest number*/
/* [↓] build rows of Pascals' triangle*/
do r=1 for N; rm=r-1 /*Note: the first column is always 1.*/
do c=2 to rm; cm=c-1 /*build the rest of the columns in row.*/
@.r.c= @.rm.cm + @.rm.c /*assign value to a specific row & col.*/
$.r = $.r right(@.r.c, w) /*and construct a line for output (row)*/
end /*c*/ /* [↑] C is the column being built.*/
if r\==1 then $.r=$.r right(1, w) /*for most rows, append a trailing "1".*/
end /*r*/ /* [↑] R is the row being built.*/
/* [↑] WIDTH: for nicely looking line.*/
width=length($.N) /*width of the last (output) line (row)*/
/*if NN<0, output is written to a file.*/
do r=1 for N /*show│write lines (rows) of triangle. */
if nn>0 then say center($.r, width) /*SAY, or*/
else call lineout 'PASCALS.'n, center($.r, width) /*write. */
end /*r*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────! subroutine (factorial)─────────────────*/
!: procedure; parse arg x; !=1; do j=2 to x; !=!*j; end; return !

View file

@ -0,0 +1,18 @@
#! /bin/bash
pascal() {
local -i n=${1:-1}
if (( n <= 1 )); then
echo 1
else
local output=$( $FUNCNAME $((n - 1)) )
set -- $( tail -n 1 <<<"$output" ) # previous row
echo "$output"
printf "1 "
while [[ -n $1 ]]; do
printf "%d " $(( $1 + ${2:-0} ))
shift
done
echo
fi
}
pascal "$1"

View file

@ -0,0 +1,14 @@
Pascal_Triangle(WScript.Arguments(0))
Function Pascal_Triangle(n)
Dim values(100)
values(1) = 1
WScript.StdOut.Write values(1)
WScript.StdOut.WriteLine
For row = 2 To n
For i = row To 1 Step -1
values(i) = values(i) + values(i-1)
WScript.StdOut.Write values(i) & " "
Next
WScript.StdOut.WriteLine
Next
End Function

View file

@ -0,0 +1,18 @@
Sub pascaltriangle()
'Pascal's triangle
Const m = 11
Dim t(40) As Integer, u(40) As Integer
Dim i As Integer, n As Integer, s As String, ss As String
ss = ""
For n = 1 To m
u(1) = 1
s = ""
For i = 1 To n
u(i + 1) = t(i) + t(i + 1)
s = s & u(i) & " "
t(i) = u(i)
Next i
ss = ss & s & vbCrLf
Next n
MsgBox ss, , "Pascal's triangle"
End Sub 'pascaltriangle