Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Pascals-triangle/00-META.yaml
Normal file
3
Task/Pascals-triangle/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Pascal's_triangle
|
||||
note: Arithmetic operations
|
||||
38
Task/Pascals-triangle/00-TASK.txt
Normal file
38
Task/Pascals-triangle/00-TASK.txt
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
[[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: <b>
|
||||
1
|
||||
1 1
|
||||
1 2 1
|
||||
1 3 3 1 </b>
|
||||
where each element of each row is either 1 or the sum of the two elements right above it.
|
||||
|
||||
For example, the next row of the triangle would be:
|
||||
::: '''1''' (since the first element of each row doesn't have two elements above it)
|
||||
::: '''4''' (1 + 3)
|
||||
::: '''6''' (3 + 3)
|
||||
::: '''4''' (3 + 1)
|
||||
::: '''1''' (since the last element of each row doesn't have two elements above it)
|
||||
|
||||
So the triangle now looks like this: <b>
|
||||
1
|
||||
1 1
|
||||
1 2 1
|
||||
1 3 3 1
|
||||
1 4 6 4 1 </b>
|
||||
|
||||
Each row <tt> n </tt> (starting with row 0 at the top) shows the coefficients of the binomial expansion of <big><big> (x + y)<sup>n</sup>. </big></big>
|
||||
|
||||
|
||||
;Task:
|
||||
Write a function that prints out the first <tt> n </tt> rows of the triangle (with <tt> f(1) </tt> yielding the row consisting of only the element '''1''').
|
||||
|
||||
This can be done either by summing elements from the previous rows or using a binary coefficient or combination function.
|
||||
|
||||
Behavior for <big><tt> n ≤ 0 </tt></big> does not need to be uniform, but should be noted.
|
||||
|
||||
|
||||
;See also:
|
||||
* [[Evaluate binomial coefficients]]
|
||||
<br><br>
|
||||
|
||||
8
Task/Pascals-triangle/11l/pascals-triangle.11l
Normal file
8
Task/Pascals-triangle/11l/pascals-triangle.11l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
F pascal(n)
|
||||
V row = [1]
|
||||
V k = [0]
|
||||
L 0 .< max(n, 0)
|
||||
print(row.join(‘ ’).center(16))
|
||||
row = zip(row [+] k, k [+] row).map((l, r) -> l + r)
|
||||
|
||||
pascal(7)
|
||||
38
Task/Pascals-triangle/360-Assembly/pascals-triangle.360
Normal file
38
Task/Pascals-triangle/360-Assembly/pascals-triangle.360
Normal 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
|
||||
15
Task/Pascals-triangle/8th/pascals-triangle-1.8th
Normal file
15
Task/Pascals-triangle/8th/pascals-triangle-1.8th
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
\ print the array
|
||||
: .arr \ a -- a
|
||||
( . space ) a:each ;
|
||||
|
||||
: pasc \ a --
|
||||
\ print the row
|
||||
.arr cr
|
||||
dup
|
||||
\ create two rows from the first, one with a leading the other with a trailing 0
|
||||
[0] 0 a:insert swap 0 a:push
|
||||
\ add the arrays together to make the new one
|
||||
' n:+ a:op ;
|
||||
|
||||
\ print the first 16 rows:
|
||||
[1] ' pasc 16 times
|
||||
21
Task/Pascals-triangle/8th/pascals-triangle-2.8th
Normal file
21
Task/Pascals-triangle/8th/pascals-triangle-2.8th
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
: ratio \ m n -- num denom
|
||||
tuck n:- n:1+ swap ;
|
||||
|
||||
\ one item in the row: n m
|
||||
: pascitem \ n m -- n
|
||||
r@ swap
|
||||
ratio
|
||||
n:*/ n:round int
|
||||
dup . space ;
|
||||
|
||||
\ One row of Pascal's triangle
|
||||
: pascline \ n --
|
||||
>r 1 int dup . space
|
||||
' pascitem
|
||||
1 r@ loop rdrop drop cr ;
|
||||
|
||||
\ Calculate the first 'n' rows of Pascal's triangle:
|
||||
: pasc \ n
|
||||
' pascline 0 rot loop cr ;
|
||||
|
||||
15 pasc
|
||||
19
Task/Pascals-triangle/ALGOL-68/pascals-triangle.alg
Normal file
19
Task/Pascals-triangle/ALGOL-68/pascals-triangle.alg
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
PRIO MINLWB = 8, MAXUPB = 8;
|
||||
OP MINLWB = ([]INT a,b)INT: (LWB a<LWB b|LWB a|LWB b),
|
||||
MAXUPB = ([]INT a,b)INT: (UPB a>UPB b|UPB a|UPB b);
|
||||
|
||||
OP + = ([]INT a,b)[]INT:(
|
||||
[a MINLWB b:a MAXUPB b]INT out; FOR i FROM LWB out TO UPB out DO out[i]:= 0 OD;
|
||||
out[LWB a:UPB a] := a; FOR i FROM LWB b TO UPB b DO out[i]+:= b[i] OD;
|
||||
out
|
||||
);
|
||||
|
||||
INT width = 4, stop = 9;
|
||||
FORMAT centre = $n((stop-UPB row+1)*width OVER 2)(q)$;
|
||||
|
||||
FLEX[1]INT row := 1; # example of rowing #
|
||||
FOR i WHILE
|
||||
printf((centre, $g(-width)$, row, $l$));
|
||||
# WHILE # i < stop DO
|
||||
row := row[AT 1] + row[AT 2]
|
||||
OD
|
||||
19
Task/Pascals-triangle/ALGOL-W/pascals-triangle.alg
Normal file
19
Task/Pascals-triangle/ALGOL-W/pascals-triangle.alg
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
begin
|
||||
% prints the first n lines of Pascal's triangle lines %
|
||||
% if n is <= 0, no output is produced %
|
||||
procedure printPascalTriangle( integer value n ) ;
|
||||
if n > 0 then begin
|
||||
integer array pascalLine ( 1 :: n );
|
||||
pascalLine( 1 ) := 1;
|
||||
for line := 1 until n do begin
|
||||
for i := line - 1 step - 1 until 2 do pascalLine( i ) := pascalLine( i - 1 ) + pascalLine( i );
|
||||
pascalLine( line ) := 1;
|
||||
write( s_w := 0, " " );
|
||||
for i := line until n do writeon( s_w := 0, " " );
|
||||
for i := 1 until line do writeon( i_w := 6, s_w := 0, pascalLine( i ) )
|
||||
end for_line ;
|
||||
end printPascalTriangle ;
|
||||
|
||||
printPascalTriangle( 8 )
|
||||
|
||||
end.
|
||||
1
Task/Pascals-triangle/APL/pascals-triangle-1.apl
Normal file
1
Task/Pascals-triangle/APL/pascals-triangle-1.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
{⍕0~¨⍨(-⌽A)⌽↑,/0,¨⍉A∘.!A←0,⍳⍵}
|
||||
1
Task/Pascals-triangle/APL/pascals-triangle-2.apl
Normal file
1
Task/Pascals-triangle/APL/pascals-triangle-2.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
{⍕0~¨⍨(-⌽A)⌽↑,/0,¨⍉A∘.!A←0,⍳⍵}5
|
||||
1
Task/Pascals-triangle/APL/pascals-triangle-3.apl
Normal file
1
Task/Pascals-triangle/APL/pascals-triangle-3.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
{{⍉⍵∘.!⍵} 0,⍳⍵}
|
||||
1
Task/Pascals-triangle/APL/pascals-triangle-4.apl
Normal file
1
Task/Pascals-triangle/APL/pascals-triangle-4.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
{{⍉⍵∘.!⍵} 0,⍳⍵} 3
|
||||
1
Task/Pascals-triangle/AWK/pascals-triangle.awk
Normal file
1
Task/Pascals-triangle/AWK/pascals-triangle.awk
Normal file
|
|
@ -0,0 +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}}'
|
||||
18
Task/Pascals-triangle/Action-/pascals-triangle.action
Normal file
18
Task/Pascals-triangle/Action-/pascals-triangle.action
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
PROC Main()
|
||||
BYTE count=[10],row,item
|
||||
CHAR ARRAY s(5)
|
||||
INT v
|
||||
|
||||
FOR row=0 TO count-1
|
||||
DO
|
||||
v=1
|
||||
FOR item=0 TO row
|
||||
DO
|
||||
StrI(v,s)
|
||||
Position(2*(count-row)+4*item-s(0),row+1)
|
||||
Print(s)
|
||||
v=v*(row-item)/(item+1)
|
||||
OD
|
||||
PutE()
|
||||
OD
|
||||
RETURN
|
||||
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;
|
||||
18
Task/Pascals-triangle/Amazing-Hopper/pascals-triangle.hopper
Normal file
18
Task/Pascals-triangle/Amazing-Hopper/pascals-triangle.hopper
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <jambo.h>
|
||||
#define Mulbyandmoveto(_X_) Mul by '_X_', Move to '_X_'
|
||||
|
||||
Main
|
||||
filas=0, Get arg numeric '2', Move to 'filas'
|
||||
i=0, r=""
|
||||
Loop if( var 'i' Is less than 'filas' )
|
||||
c=1, j=0
|
||||
Set 'c' To str, Move to 'r'
|
||||
Loop if ( var 'j' Is less than 'i' )
|
||||
Set 'i' Minus 'j', Plus one 'j', Div it; Mul by and move to 'c'
|
||||
Multi cat ' r, "\t", Str(c) '; Move to 'r'
|
||||
++j
|
||||
Back
|
||||
Printnl 'r'
|
||||
++i
|
||||
Back
|
||||
End
|
||||
213
Task/Pascals-triangle/AppleScript/pascals-triangle.applescript
Normal file
213
Task/Pascals-triangle/AppleScript/pascals-triangle.applescript
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
-------------------- PASCAL'S TRIANGLE -------------------
|
||||
|
||||
-- pascal :: Generator [[Int]]
|
||||
on pascal()
|
||||
script nextRow
|
||||
on |λ|(row)
|
||||
zipWith(my plus, {0} & row, row & {0})
|
||||
end |λ|
|
||||
end script
|
||||
iterate(nextRow, {1})
|
||||
end pascal
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
on run
|
||||
showPascal(take(7, pascal()))
|
||||
end run
|
||||
|
||||
|
||||
------------------------ FORMATTING ----------------------
|
||||
|
||||
-- showPascal :: [[Int]] -> String
|
||||
on showPascal(xs)
|
||||
set w to length of intercalate(" ", item -1 of xs)
|
||||
script align
|
||||
on |λ|(x)
|
||||
|center|(w, space, intercalate(" ", x))
|
||||
end |λ|
|
||||
end script
|
||||
unlines(map(align, xs))
|
||||
end showPascal
|
||||
|
||||
|
||||
------------------------- GENERIC ------------------------
|
||||
|
||||
-- center :: Int -> Char -> String -> String
|
||||
on |center|(n, cFiller, strText)
|
||||
set lngFill to n - (length of strText)
|
||||
if lngFill > 0 then
|
||||
set strPad to replicate(lngFill div 2, cFiller) as text
|
||||
set strCenter to strPad & strText & strPad
|
||||
if lngFill mod 2 > 0 then
|
||||
cFiller & strCenter
|
||||
else
|
||||
strCenter
|
||||
end if
|
||||
else
|
||||
strText
|
||||
end if
|
||||
end |center|
|
||||
|
||||
|
||||
-- intercalate :: String -> [String] -> String
|
||||
on intercalate(sep, xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, sep}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
return s
|
||||
end intercalate
|
||||
|
||||
|
||||
-- iterate :: (a -> a) -> a -> Generator [a]
|
||||
on iterate(f, x)
|
||||
script
|
||||
property v : missing value
|
||||
property g : mReturn(f)'s |λ|
|
||||
on |λ|()
|
||||
if missing value is v then
|
||||
set v to x
|
||||
else
|
||||
set v to g(v)
|
||||
end if
|
||||
return v
|
||||
end |λ|
|
||||
end script
|
||||
end iterate
|
||||
|
||||
|
||||
-- length :: [a] -> Int
|
||||
on |length|(xs)
|
||||
set c to class of xs
|
||||
if list is c or string is c then
|
||||
length of xs
|
||||
else
|
||||
2 ^ 30 -- (simple proxy for non-finite)
|
||||
end if
|
||||
end |length|
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- min :: Ord a => a -> a -> a
|
||||
on min(x, y)
|
||||
if y < x then
|
||||
y
|
||||
else
|
||||
x
|
||||
end if
|
||||
end min
|
||||
|
||||
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
-- 2nd class handler function lifted into 1st class script wrapper.
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
|
||||
-- plus :: Num -> Num -> Num
|
||||
on plus(a, b)
|
||||
a + b
|
||||
end plus
|
||||
|
||||
|
||||
-- Egyptian multiplication - progressively doubling a list, appending
|
||||
-- stages of doubling to an accumulator where needed for binary
|
||||
-- assembly of a target length
|
||||
-- replicate :: Int -> a -> [a]
|
||||
on replicate(n, a)
|
||||
set out to {}
|
||||
if n < 1 then return out
|
||||
set dbl to {a}
|
||||
|
||||
repeat while (n > 1)
|
||||
if (n mod 2) > 0 then set out to out & dbl
|
||||
set n to (n div 2)
|
||||
set dbl to (dbl & dbl)
|
||||
end repeat
|
||||
return out & dbl
|
||||
end replicate
|
||||
|
||||
|
||||
-- take :: Int -> [a] -> [a]
|
||||
-- take :: Int -> String -> String
|
||||
on take(n, xs)
|
||||
set c to class of xs
|
||||
if list is c then
|
||||
if 0 < n then
|
||||
items 1 thru min(n, length of xs) of xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
else if string is c then
|
||||
if 0 < n then
|
||||
text 1 thru min(n, length of xs) of xs
|
||||
else
|
||||
""
|
||||
end if
|
||||
else if script is c then
|
||||
set ys to {}
|
||||
repeat with i from 1 to n
|
||||
set end of ys to xs's |λ|()
|
||||
end repeat
|
||||
return ys
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end take
|
||||
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set str to xs as text
|
||||
set my text item delimiters to dlm
|
||||
str
|
||||
end unlines
|
||||
|
||||
|
||||
-- unwords :: [String] -> String
|
||||
on unwords(xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, space}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
return s
|
||||
end unwords
|
||||
|
||||
|
||||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
on zipWith(f, xs, ys)
|
||||
set lng to min(|length|(xs), |length|(ys))
|
||||
if 1 > lng then return {}
|
||||
set xs_ to take(lng, xs) -- Allow for non-finite
|
||||
set ys_ to take(lng, ys) -- generators like cycle etc
|
||||
set lst to {}
|
||||
tell mReturn(f)
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs_, item i of ys_)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end zipWith
|
||||
13
Task/Pascals-triangle/Arturo/pascals-triangle.arturo
Normal file
13
Task/Pascals-triangle/Arturo/pascals-triangle.arturo
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
pascalTriangle: function [n][
|
||||
triangle: new [[1]]
|
||||
|
||||
loop 1..dec n 'x [
|
||||
'triangle ++ @[map couple (last triangle)++[0] [0]++(last triangle) 'x -> x\[0] + x\[1]]
|
||||
]
|
||||
|
||||
return triangle
|
||||
]
|
||||
|
||||
loop pascalTriangle 10 'row [
|
||||
print pad.center join.with: " " map to [:string] row 'x -> pad.center x 5 60
|
||||
]
|
||||
22
Task/Pascals-triangle/AutoHotkey/pascals-triangle-1.ahk
Normal file
22
Task/Pascals-triangle/AutoHotkey/pascals-triangle-1.ahk
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
n := 8, p0 := "1" ; 1+n rows of Pascal's triangle
|
||||
Loop %n% {
|
||||
p := "p" A_Index, %p% := v := 1, q := "p" A_Index-1
|
||||
Loop Parse, %q%, %A_Space%
|
||||
If (A_Index > 1)
|
||||
%p% .= " " v+A_LoopField, v := A_LoopField
|
||||
%p% .= " 1"
|
||||
}
|
||||
; Triangular Formatted output
|
||||
VarSetCapacity(tabs,n,Asc("`t"))
|
||||
t .= tabs "`t1"
|
||||
Loop %n% {
|
||||
t .= "`n" SubStr(tabs,A_Index)
|
||||
Loop Parse, p%A_Index%, %A_Space%
|
||||
t .= A_LoopField "`t`t"
|
||||
}
|
||||
Gui Add, Text,, %t% ; Show result in a GUI
|
||||
Gui Show
|
||||
Return
|
||||
|
||||
GuiClose:
|
||||
ExitApp
|
||||
25
Task/Pascals-triangle/AutoHotkey/pascals-triangle-2.ahk
Normal file
25
Task/Pascals-triangle/AutoHotkey/pascals-triangle-2.ahk
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Msgbox % format(pascalstriangle())
|
||||
Return
|
||||
|
||||
format(o) ; converts object to string
|
||||
{
|
||||
For k, v in o
|
||||
s .= IsObject(v) ? format(v) "`n" : v " "
|
||||
Return s
|
||||
}
|
||||
pascalstriangle(n=7) ; n rows of Pascal's triangle
|
||||
{
|
||||
p := Object(), z:=Object()
|
||||
Loop, % n
|
||||
Loop, % row := A_Index
|
||||
col := A_Index
|
||||
, p[row, col] := row = 1 and col = 1
|
||||
? 1
|
||||
: (p[row-1, col-1] = "" ; math operations on blanks return blanks; I want to assume zero
|
||||
? 0
|
||||
: p[row-1, col-1])
|
||||
+ (p[row-1, col] = ""
|
||||
? 0
|
||||
: p[row-1, col])
|
||||
Return p
|
||||
}
|
||||
16
Task/Pascals-triangle/BASIC/pascals-triangle.basic
Normal file
16
Task/Pascals-triangle/BASIC/pascals-triangle.basic
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
DIM i AS Integer
|
||||
DIM row AS Integer
|
||||
DIM nrows AS Integer
|
||||
DIM values(100) AS Integer
|
||||
|
||||
INPUT "Number of rows: "; nrows
|
||||
values(1) = 1
|
||||
PRINT TAB((nrows)*3);" 1"
|
||||
FOR row = 2 TO nrows
|
||||
PRINT TAB((nrows-row)*3+1);
|
||||
FOR i = row TO 1 STEP -1
|
||||
values(i) = values(i) + values(i-1)
|
||||
PRINT USING "##### "; values(i);
|
||||
NEXT i
|
||||
PRINT
|
||||
NEXT row
|
||||
13
Task/Pascals-triangle/BBC-BASIC/pascals-triangle.basic
Normal file
13
Task/Pascals-triangle/BBC-BASIC/pascals-triangle.basic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
nrows% = 10
|
||||
|
||||
colwidth% = 4
|
||||
@% = colwidth% : REM Set column width
|
||||
FOR row% = 1 TO nrows%
|
||||
PRINT SPC(colwidth%*(nrows% - row%)/2);
|
||||
acc% = 1
|
||||
FOR element% = 1 TO row%
|
||||
PRINT acc%;
|
||||
acc% = acc% * (row% - element%) / element% + 0.5
|
||||
NEXT
|
||||
PRINT
|
||||
NEXT row%
|
||||
14
Task/Pascals-triangle/BCPL/pascals-triangle.bcpl
Normal file
14
Task/Pascals-triangle/BCPL/pascals-triangle.bcpl
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
get "libhdr"
|
||||
|
||||
let pascal(n) be
|
||||
for i=0 to n-1
|
||||
$( let c = 1
|
||||
for j=1 to 2*(n-1-i) do wrch(' ')
|
||||
for k=0 to i
|
||||
$( writef("%I3 ",c)
|
||||
c := c*(i-k)/(k+1)
|
||||
$)
|
||||
wrch('*N')
|
||||
$)
|
||||
|
||||
let start() be pascal(8)
|
||||
3
Task/Pascals-triangle/BQN/pascals-triangle-1.bqn
Normal file
3
Task/Pascals-triangle/BQN/pascals-triangle-1.bqn
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Pascal ← {(0⊸∾+∾⟜0)⍟(↕𝕩)⋈1}
|
||||
|
||||
•Show¨Pascal 6
|
||||
6
Task/Pascals-triangle/BQN/pascals-triangle-2.bqn
Normal file
6
Task/Pascals-triangle/BQN/pascals-triangle-2.bqn
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
⟨ 1 ⟩
|
||||
⟨ 1 1 ⟩
|
||||
⟨ 1 2 1 ⟩
|
||||
⟨ 1 3 3 1 ⟩
|
||||
⟨ 1 4 6 4 1 ⟩
|
||||
⟨ 1 5 10 10 5 1 ⟩
|
||||
39
Task/Pascals-triangle/Batch-File/pascals-triangle.bat
Normal file
39
Task/Pascals-triangle/Batch-File/pascals-triangle.bat
Normal 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.
|
||||
3
Task/Pascals-triangle/Befunge/pascals-triangle.bf
Normal file
3
Task/Pascals-triangle/Befunge/pascals-triangle.bf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
0" :swor fo rebmuN">:#,_&> 55+, v
|
||||
v01*p00-1:g00.:<1p011p00:\-1_v#:<
|
||||
>g:1+10p/48*,:#^_$ 55+,1+\: ^>$$@
|
||||
18
Task/Pascals-triangle/Bracmat/pascals-triangle.bracmat
Normal file
18
Task/Pascals-triangle/Bracmat/pascals-triangle.bracmat
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
( out$"Number of rows? "
|
||||
& get':?R
|
||||
& -1:?I
|
||||
& whl
|
||||
' ( 1+!I:<!R:?I
|
||||
& 1:?C
|
||||
& -1:?K
|
||||
& !R+-1*!I:?tabs
|
||||
& whl'(!tabs+-1:>0:?tabs&put$\t)
|
||||
& whl
|
||||
' ( 1+!K:~>!I:?K
|
||||
& put$(!C \t\t)
|
||||
& !C*(!I+-1*!K)*(!K+1)^-1:?C
|
||||
)
|
||||
& put$\n
|
||||
)
|
||||
&
|
||||
)
|
||||
18
Task/Pascals-triangle/Burlesque/pascals-triangle.blq
Normal file
18
Task/Pascals-triangle/Burlesque/pascals-triangle.blq
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
blsq ) {1}{1 1}{^^2CO{p^?+}m[1+]1[+}15E!#s<-spbx#S
|
||||
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
|
||||
80
Task/Pascals-triangle/C++/pascals-triangle-1.cpp
Normal file
80
Task/Pascals-triangle/C++/pascals-triangle-1.cpp
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include<cstdio>
|
||||
using namespace std;
|
||||
void Pascal_Triangle(int size) {
|
||||
|
||||
int a[100][100];
|
||||
int i, j;
|
||||
|
||||
//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);
|
||||
}
|
||||
|
||||
}
|
||||
85
Task/Pascals-triangle/C++/pascals-triangle-2.cpp
Normal file
85
Task/Pascals-triangle/C++/pascals-triangle-2.cpp
Normal 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;
|
||||
}
|
||||
80
Task/Pascals-triangle/C++/pascals-triangle-3.cpp
Normal file
80
Task/Pascals-triangle/C++/pascals-triangle-3.cpp
Normal 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;
|
||||
}
|
||||
25
Task/Pascals-triangle/C-sharp/pascals-triangle-1.cs
Normal file
25
Task/Pascals-triangle/C-sharp/pascals-triangle-1.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Task/Pascals-triangle/C-sharp/pascals-triangle-2.cs
Normal file
48
Task/Pascals-triangle/C-sharp/pascals-triangle-2.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
6
Task/Pascals-triangle/C-sharp/pascals-triangle-3.cs
Normal file
6
Task/Pascals-triangle/C-sharp/pascals-triangle-3.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
static void Main()
|
||||
{
|
||||
IEnumerable<BigInteger[]> triangle = PascalsTriangle.GetTriangle(20);
|
||||
string output = PascalsTriangle.FormatTriangleString(triangle)
|
||||
Console.WriteLine(output);
|
||||
}
|
||||
22
Task/Pascals-triangle/C/pascals-triangle-1.c
Normal file
22
Task/Pascals-triangle/C/pascals-triangle-1.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void pascaltriangle(unsigned int n)
|
||||
{
|
||||
unsigned int c, i, j, k;
|
||||
|
||||
for(i=0; i < n; i++) {
|
||||
c = 1;
|
||||
for(j=1; j <= 2*(n-1-i); j++) printf(" ");
|
||||
for(k=0; k <= i; k++) {
|
||||
printf("%3d ", c);
|
||||
c = c * (i-k)/(k+1);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
pascaltriangle(8);
|
||||
return 0;
|
||||
}
|
||||
18
Task/Pascals-triangle/C/pascals-triangle-2.c
Normal file
18
Task/Pascals-triangle/C/pascals-triangle-2.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define D 32
|
||||
int pascals(int *x, int *y, int d)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < d; i++)
|
||||
printf("%d%c", y[i] = x[i - 1] + x[i],
|
||||
i < d - 1 ? ' ' : '\n');
|
||||
|
||||
return D > d ? pascals(y, x, d + 1) : 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int x[D] = {0, 1, 0}, y[D] = {0};
|
||||
return pascals(x, y, 0);
|
||||
}
|
||||
16
Task/Pascals-triangle/C/pascals-triangle-3.c
Normal file
16
Task/Pascals-triangle/C/pascals-triangle-3.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
void triangleC(int nRows) {
|
||||
if (nRows <= 0) return;
|
||||
int *prevRow = NULL;
|
||||
for (int r = 1; r <= nRows; r++) {
|
||||
int *currRow = malloc(r * sizeof(int));
|
||||
for (int i = 0; i < r; i++) {
|
||||
int val = i==0 || i==r-1 ? 1 : prevRow[i-1] + prevRow[i];
|
||||
currRow[i] = val;
|
||||
printf(" %4d", val);
|
||||
}
|
||||
printf("\n");
|
||||
free(prevRow);
|
||||
prevRow = currRow;
|
||||
}
|
||||
free(prevRow);
|
||||
}
|
||||
12
Task/Pascals-triangle/Clojure/pascals-triangle-1.clj
Normal file
12
Task/Pascals-triangle/Clojure/pascals-triangle-1.clj
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(defn pascal [n]
|
||||
(let [newrow (fn newrow [lst ret]
|
||||
(if lst
|
||||
(recur (rest lst)
|
||||
(conj ret (+ (first lst) (or (second lst) 0))))
|
||||
ret))
|
||||
genrow (fn genrow [n lst]
|
||||
(when (< 0 n)
|
||||
(do (println lst)
|
||||
(recur (dec n) (conj (newrow lst []) 1)))))]
|
||||
(genrow n [1])))
|
||||
(pascal 4)
|
||||
8
Task/Pascals-triangle/Clojure/pascals-triangle-2.clj
Normal file
8
Task/Pascals-triangle/Clojure/pascals-triangle-2.clj
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(defn nextrow [row]
|
||||
(vec (concat [1] (map #(apply + %) (partition 2 1 row)) [1] )))
|
||||
|
||||
(defn pascal [n]
|
||||
(assert (and (integer? n) (pos? n)))
|
||||
(let [triangle (take n (iterate nextrow [1]))]
|
||||
(doseq [row triangle]
|
||||
(println row))))
|
||||
7
Task/Pascals-triangle/Clojure/pascals-triangle-3.clj
Normal file
7
Task/Pascals-triangle/Clojure/pascals-triangle-3.clj
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(def pascal
|
||||
(iterate
|
||||
(fn [prev-row]
|
||||
(->>
|
||||
(concat [[(first prev-row)]] (partition 2 1 prev-row) [[(last prev-row)]])
|
||||
(map (partial apply +) ,,,)))
|
||||
[1]))
|
||||
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
|
||||
28
Task/Pascals-triangle/CoffeeScript/pascals-triangle.coffee
Normal file
28
Task/Pascals-triangle/CoffeeScript/pascals-triangle.coffee
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
pascal = (n) ->
|
||||
width = 6
|
||||
for r in [1..n]
|
||||
s = ws (width/2) * (n-r) # center row
|
||||
output = (n) -> s += pad width, n
|
||||
cell = 1
|
||||
output cell
|
||||
# Compute binomial coefficients as you go
|
||||
# across the row.
|
||||
for c in [1...r]
|
||||
cell *= (r-c) / c
|
||||
output cell
|
||||
console.log s
|
||||
|
||||
ws = (n) ->
|
||||
s = ''
|
||||
s += ' ' for i in [0...n]
|
||||
s
|
||||
|
||||
pad = (cnt, n) ->
|
||||
s = n.toString()
|
||||
# There is probably a better way to do this.
|
||||
cnt -= s.length
|
||||
right = Math.floor(cnt / 2)
|
||||
left = cnt - right
|
||||
ws(left) + s + ws(right)
|
||||
|
||||
pascal(7)
|
||||
|
|
@ -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
|
||||
|
|
@ -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.
|
||||
13
Task/Pascals-triangle/Common-Lisp/pascals-triangle-1.lisp
Normal file
13
Task/Pascals-triangle/Common-Lisp/pascals-triangle-1.lisp
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(defun pascal (n)
|
||||
(genrow n '(1)))
|
||||
|
||||
(defun genrow (n l)
|
||||
(when (plusp n)
|
||||
(print l)
|
||||
(genrow (1- n) (cons 1 (newrow l)))))
|
||||
|
||||
(defun newrow (l)
|
||||
(if (null (rest l))
|
||||
'(1)
|
||||
(cons (+ (first l) (second l))
|
||||
(newrow (rest l)))))
|
||||
12
Task/Pascals-triangle/Common-Lisp/pascals-triangle-2.lisp
Normal file
12
Task/Pascals-triangle/Common-Lisp/pascals-triangle-2.lisp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(defun pascal-next-row (a)
|
||||
(loop :for q :in a
|
||||
:and p = 0 :then q
|
||||
:as s = (list (+ p q))
|
||||
:nconc s :into a
|
||||
:finally (rplacd s (list 1))
|
||||
(return a)))
|
||||
|
||||
(defun pascal (n)
|
||||
(loop :for a = (list 1) :then (pascal-next-row a)
|
||||
:repeat n
|
||||
:collect a))
|
||||
16
Task/Pascals-triangle/Common-Lisp/pascals-triangle-3.lisp
Normal file
16
Task/Pascals-triangle/Common-Lisp/pascals-triangle-3.lisp
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
(defun next-pascal-triangle-row (list)
|
||||
`(1
|
||||
,.(mapcar #'+ list (rest list))
|
||||
1))
|
||||
|
||||
(defun pascal-triangle (number-of-rows)
|
||||
(loop repeat number-of-rows
|
||||
for row = '(1) then (next-pascal-triangle-row row)
|
||||
collect row))
|
||||
|
||||
(defun print-pascal-triangle (number-of-rows)
|
||||
(let* ((triangle (pascal-triangle number-of-rows))
|
||||
(max-row-length (length (write-to-string (first (last triangle))))))
|
||||
(format t
|
||||
(format nil "~~{~~~D:@<~~{~~A ~~}~~>~~%~~}" max-row-length)
|
||||
triangle)))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(print-pascal-triangle 4)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
1
|
||||
1 1
|
||||
1 2 1
|
||||
1 3 3 1
|
||||
|
|
@ -0,0 +1 @@
|
|||
(print-pascal-triangle 8)
|
||||
|
|
@ -0,0 +1,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
|
||||
71
Task/Pascals-triangle/Component-Pascal/pascals-triangle.pas
Normal file
71
Task/Pascals-triangle/Component-Pascal/pascals-triangle.pas
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
MODULE PascalTriangle;
|
||||
IMPORT StdLog, DevCommanders, TextMappers;
|
||||
|
||||
TYPE
|
||||
Expansion* = POINTER TO ARRAY OF LONGINT;
|
||||
|
||||
PROCEDURE Show*(e: Expansion);
|
||||
VAR
|
||||
i: INTEGER;
|
||||
BEGIN
|
||||
i := 0;
|
||||
WHILE (i < LEN(e)) & (e[i] # 0) DO
|
||||
StdLog.Int(e[i]);
|
||||
INC(i)
|
||||
END;
|
||||
StdLog.Ln
|
||||
END Show;
|
||||
|
||||
PROCEDURE GenFor*(p: LONGINT): Expansion;
|
||||
VAR
|
||||
expA,expB: Expansion;
|
||||
i,j: LONGINT;
|
||||
|
||||
PROCEDURE Swap(VAR x,y: Expansion);
|
||||
VAR
|
||||
swap: Expansion;
|
||||
BEGIN
|
||||
swap := x; x := y; y := swap
|
||||
END Swap;
|
||||
|
||||
BEGIN
|
||||
ASSERT(p >= 0);
|
||||
NEW(expA,p + 2);NEW(expB,p + 2);
|
||||
FOR i := 0 TO p DO
|
||||
IF i = 0 THEN expA[0] := 1
|
||||
ELSE
|
||||
FOR j := 0 TO i DO
|
||||
IF j = 0 THEN
|
||||
expB[j] := expA[j]
|
||||
ELSE
|
||||
expB[j] := expA[j - 1] + expA[j]
|
||||
END
|
||||
END;
|
||||
Swap(expA,expB)
|
||||
END;
|
||||
END;
|
||||
expB := NIL; (* for the GC *)
|
||||
RETURN expA
|
||||
END GenFor;
|
||||
|
||||
|
||||
PROCEDURE Do*;
|
||||
VAR
|
||||
s: TextMappers.Scanner;
|
||||
exp: Expansion;
|
||||
BEGIN
|
||||
s.ConnectTo(DevCommanders.par.text);
|
||||
s.SetPos(DevCommanders.par.beg);
|
||||
s.Scan;
|
||||
WHILE (~s.rider.eot) DO
|
||||
IF (s.type = TextMappers.char) & (s.char = '~') THEN
|
||||
RETURN
|
||||
ELSIF (s.type = TextMappers.int) THEN
|
||||
exp := GenFor(s.int);
|
||||
Show(exp)
|
||||
END;
|
||||
s.Scan
|
||||
END
|
||||
END Do;
|
||||
|
||||
END PascalTriangle.
|
||||
25
Task/Pascals-triangle/D/pascals-triangle-1.d
Normal file
25
Task/Pascals-triangle/D/pascals-triangle-1.d
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
int[][] pascalsTriangle(in int rows) pure nothrow {
|
||||
auto tri = new int[][rows];
|
||||
foreach (r; 0 .. rows) {
|
||||
int v = 1;
|
||||
foreach (c; 0 .. r+1) {
|
||||
tri[r] ~= v;
|
||||
v = (v * (r - c)) / (c + 1);
|
||||
}
|
||||
}
|
||||
return tri;
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable t = pascalsTriangle(10);
|
||||
assert(t == [[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]]);
|
||||
}
|
||||
11
Task/Pascals-triangle/D/pascals-triangle-2.d
Normal file
11
Task/Pascals-triangle/D/pascals-triangle-2.d
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
auto pascal() pure nothrow {
|
||||
return [1].recurrence!q{ zip(a[n - 1] ~ 0, 0 ~ a[n - 1])
|
||||
.map!q{ a[0] + a[1] }
|
||||
.array };
|
||||
}
|
||||
|
||||
void main() {
|
||||
pascal.take(5).writeln;
|
||||
}
|
||||
42
Task/Pascals-triangle/D/pascals-triangle-3.d
Normal file
42
Task/Pascals-triangle/D/pascals-triangle-3.d
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import std.stdio, std.string, std.array, std.format;
|
||||
|
||||
string Pascal(alias dg, T, T initValue)(int n) {
|
||||
string output;
|
||||
|
||||
void append(in T[] l) {
|
||||
output ~= " ".replicate((n - l.length + 1) * 2);
|
||||
foreach (e; l)
|
||||
output ~= format("%4s", format("%4s", e));
|
||||
output ~= "\n";
|
||||
}
|
||||
|
||||
if (n > 0) {
|
||||
T[][] lines = [[initValue]];
|
||||
append(lines[0]);
|
||||
foreach (i; 1 .. n) {
|
||||
lines ~= lines[i - 1] ~ initValue; // length + 1
|
||||
foreach (int j; 1 .. lines[i-1].length)
|
||||
lines[i][j] = dg(lines[i-1][j], lines[i-1][j-1]);
|
||||
append(lines[i]);
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
string delegate(int n) genericPascal(alias dg, T, T initValue)() {
|
||||
mixin Pascal!(dg, T, initValue);
|
||||
return &Pascal;
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto pascal = genericPascal!((int a, int b) => a + b, int, 1)();
|
||||
static char xor(char a, char b) { return a == b ? '_' : '*'; }
|
||||
auto sierpinski = genericPascal!(xor, char, '*')();
|
||||
|
||||
foreach (i; [1, 5, 9])
|
||||
writef(pascal(i));
|
||||
// an order 4 sierpinski triangle is a 2^4 lines generic
|
||||
// Pascal triangle with xor operation
|
||||
foreach (i; [16])
|
||||
writef(sierpinski(i));
|
||||
}
|
||||
15
Task/Pascals-triangle/DWScript/pascals-triangle.dw
Normal file
15
Task/Pascals-triangle/DWScript/pascals-triangle.dw
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
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
|
||||
Print(Format('%4d', [c]));
|
||||
c:=(c*(i-k)) div (k+1);
|
||||
end;
|
||||
PrintLn('');
|
||||
end;
|
||||
end;
|
||||
|
||||
Pascal(9);
|
||||
39
Task/Pascals-triangle/Dart/pascals-triangle.dart
Normal file
39
Task/Pascals-triangle/Dart/pascals-triangle.dart
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import 'dart:io';
|
||||
|
||||
pascal(n) {
|
||||
if(n<=0) print("Not defined");
|
||||
|
||||
else if(n==1) print(1);
|
||||
|
||||
else {
|
||||
List<List<int>> matrix = new List<List<int>>();
|
||||
matrix.add(new List<int>());
|
||||
matrix.add(new List<int>());
|
||||
matrix[0].add(1);
|
||||
matrix[1].add(1);
|
||||
matrix[1].add(1);
|
||||
for (var i = 2; i < n; i++) {
|
||||
List<int> list = new List<int>();
|
||||
list.add(1);
|
||||
for (var j = 1; j<i; j++) {
|
||||
list.add(matrix[i-1][j-1]+matrix[i-1][j]);
|
||||
}
|
||||
list.add(1);
|
||||
matrix.add(list);
|
||||
}
|
||||
for(var i=0; i<n; i++) {
|
||||
for(var j=0; j<=i; j++) {
|
||||
stdout.write(matrix[i][j]);
|
||||
stdout.write(' ');
|
||||
}
|
||||
stdout.write('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
pascal(0);
|
||||
pascal(1);
|
||||
pascal(3);
|
||||
pascal(6);
|
||||
}
|
||||
21
Task/Pascals-triangle/Delphi/pascals-triangle.delphi
Normal file
21
Task/Pascals-triangle/Delphi/pascals-triangle.delphi
Normal 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.
|
||||
20
Task/Pascals-triangle/E/pascals-triangle-1.e
Normal file
20
Task/Pascals-triangle/E/pascals-triangle-1.e
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
def pascalsTriangle(n, out) {
|
||||
def row := [].diverge(int)
|
||||
out.print("<table style='text-align: center; border: 0; border-collapse: collapse;'>")
|
||||
for y in 1..n {
|
||||
out.print("<tr>")
|
||||
row.push(1)
|
||||
def skip := n - y
|
||||
if (skip > 0) {
|
||||
out.print(`<td colspan="$skip"></td>`)
|
||||
}
|
||||
for x => v in row {
|
||||
out.print(`<td>$v</td><td></td>`)
|
||||
}
|
||||
for i in (1..!y).descending() {
|
||||
row[i] += row[i - 1]
|
||||
}
|
||||
out.println("</tr>")
|
||||
}
|
||||
out.print("</table>")
|
||||
}
|
||||
7
Task/Pascals-triangle/E/pascals-triangle-2.e
Normal file
7
Task/Pascals-triangle/E/pascals-triangle-2.e
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
def out := <file:triangle.html>.textWriter()
|
||||
try {
|
||||
pascalsTriangle(15, out)
|
||||
} finally {
|
||||
out.close()
|
||||
}
|
||||
makeCommand("yourFavoriteWebBrowser")("triangle.html")
|
||||
17
Task/Pascals-triangle/ERRE/pascals-triangle.erre
Normal file
17
Task/Pascals-triangle/ERRE/pascals-triangle.erre
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
PROGRAM PASCAL_TRIANGLE
|
||||
|
||||
PROCEDURE PASCAL(R%)
|
||||
LOCAL I%,C%,K%
|
||||
FOR I%=0 TO R%-1 DO
|
||||
C%=1
|
||||
FOR K%=0 TO I% DO
|
||||
WRITE("###";C%;)
|
||||
C%=(C%*(I%-K%)) DIV (K%+1)
|
||||
END FOR
|
||||
PRINT
|
||||
END FOR
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
PASCAL(9)
|
||||
END PROGRAM
|
||||
101
Task/Pascals-triangle/Eiffel/pascals-triangle.e
Normal file
101
Task/Pascals-triangle/Eiffel/pascals-triangle.e
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
note
|
||||
description : "Prints pascal's triangle"
|
||||
output : "[
|
||||
Per requirements of the RosettaCode example, execution will print the first n rows of pascal's triangle
|
||||
]"
|
||||
date : "19 December 2013"
|
||||
authors : "Sandro Meier", "Roman Brunner"
|
||||
revision : "1.0"
|
||||
libraries : "Relies on HASH_TABLE from EIFFEL_BASE library"
|
||||
implementation : "[
|
||||
Recursive implementation to calculate the n'th row.
|
||||
]"
|
||||
warning : "[
|
||||
Will not work for large n's (INTEGER_32)
|
||||
]"
|
||||
|
||||
class
|
||||
APPLICATION
|
||||
|
||||
inherit
|
||||
ARGUMENTS
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
local
|
||||
n:INTEGER
|
||||
do
|
||||
create {HASH_TABLE[ARRAY[INTEGER],INTEGER]}pascal_lines.make (n) --create the hash_table object
|
||||
io.new_line
|
||||
n:=25
|
||||
draw(n)
|
||||
end
|
||||
feature
|
||||
line(n:INTEGER):ARRAY[INTEGER]
|
||||
--Calculates the n'th line
|
||||
local
|
||||
upper_line:ARRAY[INTEGER]
|
||||
i:INTEGER
|
||||
do
|
||||
if n=1 then --trivial case first line
|
||||
create Result.make_filled (0, 1, n+2)
|
||||
Result.put (0, 1)
|
||||
Result.put (1, 2)
|
||||
Result.put (0, 3)
|
||||
elseif pascal_lines.has (n) then --checks if the result was already calculated
|
||||
Result := pascal_lines.at (n)
|
||||
else --calculates the n'th line recursively
|
||||
create Result.make_filled(0,1,n+2) --for caluclation purposes add a 0 at the beginning of each line
|
||||
Result.put (0, 1)
|
||||
upper_line:=line(n-1)
|
||||
from
|
||||
i:=1
|
||||
until
|
||||
i>upper_line.count-1
|
||||
loop
|
||||
Result.put(upper_line[i]+upper_line[i+1],i+1)
|
||||
i:=i+1
|
||||
end
|
||||
Result.put (0, n+2) --for caluclation purposes add a 0 at the end of each line
|
||||
pascal_lines.put (Result, n)
|
||||
end
|
||||
end
|
||||
|
||||
draw(n:INTEGER)
|
||||
--draw n lines of pascal's triangle
|
||||
local
|
||||
space_string:STRING
|
||||
width, i:INTEGER
|
||||
|
||||
do
|
||||
space_string:=" " --question of design: add space_string at the beginning of each line
|
||||
width:=line(n).count
|
||||
space_string.multiply (width)
|
||||
from
|
||||
i:=1
|
||||
until
|
||||
i>n
|
||||
loop
|
||||
space_string.remove_tail (1)
|
||||
io.put_string (space_string)
|
||||
across line(i) as c
|
||||
loop
|
||||
if
|
||||
c.item/=0
|
||||
then
|
||||
io.put_string (c.item.out+" ")
|
||||
end
|
||||
end
|
||||
io.new_line
|
||||
i:=i+1
|
||||
end
|
||||
end
|
||||
|
||||
feature --Access
|
||||
pascal_lines:HASH_TABLE[ARRAY[INTEGER],INTEGER]
|
||||
--Contains all already calculated lines
|
||||
end
|
||||
12
Task/Pascals-triangle/Elixir/pascals-triangle.elixir
Normal file
12
Task/Pascals-triangle/Elixir/pascals-triangle.elixir
Normal 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)
|
||||
10
Task/Pascals-triangle/Emacs-Lisp/pascals-triangle-1.l
Normal file
10
Task/Pascals-triangle/Emacs-Lisp/pascals-triangle-1.l
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(require 'cl-lib)
|
||||
|
||||
(defun next-row (row)
|
||||
(cl-mapcar #'+ (cons 0 row)
|
||||
(append row '(0))))
|
||||
|
||||
(defun triangle (row rows)
|
||||
(if (= rows 0)
|
||||
'()
|
||||
(cons row (triangle (next-row row) (- rows 1)))))
|
||||
8
Task/Pascals-triangle/Emacs-Lisp/pascals-triangle-2.l
Normal file
8
Task/Pascals-triangle/Emacs-Lisp/pascals-triangle-2.l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(defun pascal (r)
|
||||
(dotimes (i r)
|
||||
(let ((c 1))
|
||||
(dotimes (k (+ i 1))
|
||||
(princ (format "%d " c))
|
||||
(setq c (/ (* c (- i k))
|
||||
(+ k 1))))
|
||||
(terpri))))
|
||||
10
Task/Pascals-triangle/Emacs-Lisp/pascals-triangle-3.l
Normal file
10
Task/Pascals-triangle/Emacs-Lisp/pascals-triangle-3.l
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(defun pascal (r)
|
||||
(let ((out ""))
|
||||
(dotimes (i r)
|
||||
(let ((c 1))
|
||||
(dotimes (k (+ i 1))
|
||||
(setq out (concat out (format "%d " c)))
|
||||
(setq c (/ (* c (- i k))
|
||||
(+ k 1))))
|
||||
(setq out (concat out "\n"))))
|
||||
out))
|
||||
8
Task/Pascals-triangle/Erlang/pascals-triangle.erl
Normal file
8
Task/Pascals-triangle/Erlang/pascals-triangle.erl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
-import(lists).
|
||||
-export([pascal/1]).
|
||||
|
||||
pascal(1)-> [[1]];
|
||||
pascal(N) ->
|
||||
L = pascal(N-1),
|
||||
[H|_] = L,
|
||||
[lists:zipwith(fun(X,Y)->X+Y end,[0]++H,H++[0])|L].
|
||||
10
Task/Pascals-triangle/Euphoria/pascals-triangle.euphoria
Normal file
10
Task/Pascals-triangle/Euphoria/pascals-triangle.euphoria
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
sequence row
|
||||
row = {}
|
||||
for m = 1 to 10 do
|
||||
row = row & 1
|
||||
for n = length(row)-1 to 2 by -1 do
|
||||
row[n] += row[n-1]
|
||||
end for
|
||||
print(1,row)
|
||||
puts(1,'\n')
|
||||
end for
|
||||
14
Task/Pascals-triangle/Excel/pascals-triangle-1.excel
Normal file
14
Task/Pascals-triangle/Excel/pascals-triangle-1.excel
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
PASCAL
|
||||
=LAMBDA(n,
|
||||
BINCOEFF(n - 1)(
|
||||
SEQUENCE(1, n, 0, 1)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
BINCOEFF
|
||||
=LAMBDA(n,
|
||||
LAMBDA(k,
|
||||
QUOTIENT(FACT(n), FACT(k) * FACT(n - k))
|
||||
)
|
||||
)
|
||||
12
Task/Pascals-triangle/Excel/pascals-triangle-2.excel
Normal file
12
Task/Pascals-triangle/Excel/pascals-triangle-2.excel
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
TRIANGLE
|
||||
=LAMBDA(n,
|
||||
LET(
|
||||
ixs, SEQUENCE(n, n, 0, 1),
|
||||
x, MOD(ixs, n),
|
||||
y, QUOTIENT(ixs, n),
|
||||
IF(x <= y,
|
||||
BINCOEFF(y)(x),
|
||||
""
|
||||
)
|
||||
)
|
||||
)
|
||||
12
Task/Pascals-triangle/F-Sharp/pascals-triangle.fs
Normal file
12
Task/Pascals-triangle/F-Sharp/pascals-triangle.fs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
let rec nextrow l =
|
||||
match l with
|
||||
| [] -> []
|
||||
| h :: [] -> [1]
|
||||
| h :: t -> h + t.Head :: nextrow t
|
||||
|
||||
let pascalTri n = List.scan(fun l i -> 1 :: nextrow l) [1] [1 .. n]
|
||||
|
||||
for row in pascalTri(10) do
|
||||
for i in row do
|
||||
printf "%s" (i.ToString() + ", ")
|
||||
printfn "%s" "\n"
|
||||
11
Task/Pascals-triangle/FOCAL/pascals-triangle.focal
Normal file
11
Task/Pascals-triangle/FOCAL/pascals-triangle.focal
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
1.1 S OLD(1)=1; T %4.0, 1, !
|
||||
1.2 F N=1,10; D 2
|
||||
1.3 Q
|
||||
|
||||
2.1 S NEW(1)=1
|
||||
2.2 F X=1,N; S NEW(X+1)=OLD(X)+OLD(X+1)
|
||||
2.3 F X=1,N+1; D 3
|
||||
2.4 T !
|
||||
|
||||
3.1 S OLD(X)=NEW(X)
|
||||
3.2 T %4.0, OLD(X)
|
||||
7
Task/Pascals-triangle/Factor/pascals-triangle-1.factor
Normal file
7
Task/Pascals-triangle/Factor/pascals-triangle-1.factor
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
USING: grouping kernel math sequences ;
|
||||
|
||||
: (pascal) ( seq -- newseq )
|
||||
dup last 0 prefix 0 suffix 2 <clumps> [ sum ] map suffix ;
|
||||
|
||||
: pascal ( n -- seq )
|
||||
1 - { { 1 } } swap [ (pascal) ] times ;
|
||||
2
Task/Pascals-triangle/Factor/pascals-triangle-2.factor
Normal file
2
Task/Pascals-triangle/Factor/pascals-triangle-2.factor
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
5 pascal .
|
||||
{ { 1 } { 1 1 } { 1 2 1 } { 1 3 3 1 } { 1 4 6 4 1 } }
|
||||
29
Task/Pascals-triangle/Fantom/pascals-triangle.fantom
Normal file
29
Task/Pascals-triangle/Fantom/pascals-triangle.fantom
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
class Main
|
||||
{
|
||||
Int[] next_row (Int[] row)
|
||||
{
|
||||
new_row := [1]
|
||||
(row.size-1).times |i|
|
||||
{
|
||||
new_row.add (row[i] + row[i+1])
|
||||
}
|
||||
new_row.add (1)
|
||||
|
||||
return new_row
|
||||
}
|
||||
|
||||
Void print_pascal (Int n) // no output for n <= 0
|
||||
{
|
||||
current_row := [1]
|
||||
n.times
|
||||
{
|
||||
echo (current_row.join(" "))
|
||||
current_row = next_row (current_row)
|
||||
}
|
||||
}
|
||||
|
||||
Void main ()
|
||||
{
|
||||
print_pascal (10)
|
||||
}
|
||||
}
|
||||
11
Task/Pascals-triangle/Forth/pascals-triangle-1.fth
Normal file
11
Task/Pascals-triangle/Forth/pascals-triangle-1.fth
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
: init ( n -- )
|
||||
here swap cells erase 1 here ! ;
|
||||
: .line ( n -- )
|
||||
cr here swap 0 do dup @ . cell+ loop drop ;
|
||||
: next ( n -- )
|
||||
here swap 1- cells here + do
|
||||
i @ i cell+ +!
|
||||
-1 cells +loop ;
|
||||
: pascal ( n -- )
|
||||
dup init 1 .line
|
||||
1 ?do i next i 1+ .line loop ;
|
||||
8
Task/Pascals-triangle/Forth/pascals-triangle-2.fth
Normal file
8
Task/Pascals-triangle/Forth/pascals-triangle-2.fth
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
: PascTriangle
|
||||
cr dup 0
|
||||
?do
|
||||
1 over 1- i - 2* spaces i 1+ 0 ?do dup 4 .r j i - * i 1+ / loop cr drop
|
||||
loop drop
|
||||
;
|
||||
|
||||
13 PascTriangle
|
||||
26
Task/Pascals-triangle/Fortran/pascals-triangle.f
Normal file
26
Task/Pascals-triangle/Fortran/pascals-triangle.f
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
PROGRAM Pascals_Triangle
|
||||
|
||||
CALL Print_Triangle(8)
|
||||
|
||||
END PROGRAM Pascals_Triangle
|
||||
|
||||
SUBROUTINE Print_Triangle(n)
|
||||
|
||||
IMPLICIT NONE
|
||||
INTEGER, INTENT(IN) :: n
|
||||
INTEGER :: c, i, j, k, spaces
|
||||
|
||||
DO i = 0, n-1
|
||||
c = 1
|
||||
spaces = 3 * (n - 1 - i)
|
||||
DO j = 1, spaces
|
||||
WRITE(*,"(A)", ADVANCE="NO") " "
|
||||
END DO
|
||||
DO k = 0, i
|
||||
WRITE(*,"(I6)", ADVANCE="NO") c
|
||||
c = c * (i - k) / (k + 1)
|
||||
END DO
|
||||
WRITE(*,*)
|
||||
END DO
|
||||
|
||||
END SUBROUTINE Print_Triangle
|
||||
37
Task/Pascals-triangle/FreeBASIC/pascals-triangle.basic
Normal file
37
Task/Pascals-triangle/FreeBASIC/pascals-triangle.basic
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub pascalTriangle(n As UInteger)
|
||||
If n = 0 Then Return
|
||||
Dim prevRow(1 To n) As UInteger
|
||||
Dim currRow(1 To n) As UInteger
|
||||
Dim start(1 To n) As UInteger ''stores starting column for each row
|
||||
start(n) = 1
|
||||
For i As Integer = n - 1 To 1 Step -1
|
||||
start(i) = start(i + 1) + 3
|
||||
Next
|
||||
prevRow(1) = 1
|
||||
Print Tab(start(1));
|
||||
Print 1U
|
||||
For i As UInteger = 2 To n
|
||||
For j As UInteger = 1 To i
|
||||
If j = 1 Then
|
||||
Print Tab(start(i)); "1";
|
||||
currRow(1) = 1
|
||||
ElseIf j = i Then
|
||||
Print " 1"
|
||||
currRow(i) = 1
|
||||
Else
|
||||
currRow(j) = prevRow(j - 1) + prevRow(j)
|
||||
Print Using "######"; currRow(j); " ";
|
||||
End If
|
||||
Next j
|
||||
For j As UInteger = 1 To i
|
||||
prevRow(j) = currRow(j)
|
||||
Next j
|
||||
Next i
|
||||
End Sub
|
||||
|
||||
pascalTriangle(14)
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
15
Task/Pascals-triangle/Frink/pascals-triangle.frink
Normal file
15
Task/Pascals-triangle/Frink/pascals-triangle.frink
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
pascal[rows] :=
|
||||
{
|
||||
widest = length[toString[binomial[rows-1, (rows-1) div 2]]]
|
||||
|
||||
for row = 0 to rows-1
|
||||
{
|
||||
line = repeat[" ", round[(rows-row)* (widest+1)/2]]
|
||||
for col = 0 to row
|
||||
line = line + padRight[binomial[row, col], widest+1, " "]
|
||||
|
||||
println[line]
|
||||
}
|
||||
}
|
||||
|
||||
pascal[10]
|
||||
5
Task/Pascals-triangle/FunL/pascals-triangle-1.funl
Normal file
5
Task/Pascals-triangle/FunL/pascals-triangle-1.funl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
import lists.zip
|
||||
|
||||
def
|
||||
pascal( 1 ) = [1]
|
||||
pascal( n ) = [1] + map( (a, b) -> a + b, zip(pascal(n-1), pascal(n-1).tail()) ) + [1]
|
||||
3
Task/Pascals-triangle/FunL/pascals-triangle-2.funl
Normal file
3
Task/Pascals-triangle/FunL/pascals-triangle-2.funl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import integers.choose
|
||||
|
||||
def pascal( n ) = [choose( n - 1, k ) | k <- 0..n-1]
|
||||
11
Task/Pascals-triangle/FunL/pascals-triangle-3.funl
Normal file
11
Task/Pascals-triangle/FunL/pascals-triangle-3.funl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def triangle( height ) =
|
||||
width = max( map(a -> a.toString().length(), pascal(height)) )
|
||||
|
||||
if 2|width
|
||||
width++
|
||||
|
||||
for n <- 1..height
|
||||
print( ' '*((width + 1)\2)*(height - n) )
|
||||
println( map(a -> format('%' + width + 'd ', a), pascal(n)).mkString() )
|
||||
|
||||
triangle( 10 )
|
||||
19
Task/Pascals-triangle/GAP/pascals-triangle.gap
Normal file
19
Task/Pascals-triangle/GAP/pascals-triangle.gap
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
Pascal := function(n)
|
||||
local i, v;
|
||||
v := [1];
|
||||
for i in [1 .. n] do
|
||||
Display(v);
|
||||
v := Concatenation([0], v) + Concatenation(v, [0]);
|
||||
od;
|
||||
end;
|
||||
|
||||
Pascal(9);
|
||||
# [ 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 ]
|
||||
9
Task/Pascals-triangle/GW-BASIC/pascals-triangle.basic
Normal file
9
Task/Pascals-triangle/GW-BASIC/pascals-triangle.basic
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
10 INPUT "Number of rows? ",R
|
||||
20 FOR I=0 TO R-1
|
||||
30 C=1
|
||||
40 FOR K=0 TO I
|
||||
50 PRINT USING "####";C;
|
||||
60 C=C*(I-K)/(K+1)
|
||||
70 NEXT
|
||||
80 PRINT
|
||||
90 NEXT
|
||||
42
Task/Pascals-triangle/Go/pascals-triangle.go
Normal file
42
Task/Pascals-triangle/Go/pascals-triangle.go
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func printTriangle(n int) {
|
||||
// degenerate cases
|
||||
if n <= 0 {
|
||||
return
|
||||
}
|
||||
fmt.Println(1)
|
||||
if n == 1 {
|
||||
return
|
||||
}
|
||||
// iterate over rows, zero based
|
||||
a := make([]int, (n+1)/2)
|
||||
a[0] = 1
|
||||
for row, middle := 1, 0; row < n; row++ {
|
||||
// generate new row
|
||||
even := row&1 == 0
|
||||
if even {
|
||||
a[middle+1] = a[middle] * 2
|
||||
}
|
||||
for i := middle; i > 0; i-- {
|
||||
a[i] += a[i-1]
|
||||
}
|
||||
// print row
|
||||
for i := 0; i <= middle; i++ {
|
||||
fmt.Print(a[i], " ")
|
||||
}
|
||||
if even {
|
||||
middle++
|
||||
}
|
||||
for i := middle; i >= 0; i-- {
|
||||
fmt.Print(a[i], " ")
|
||||
}
|
||||
fmt.Println("")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
printTriangle(4)
|
||||
}
|
||||
2
Task/Pascals-triangle/Groovy/pascals-triangle-1.groovy
Normal file
2
Task/Pascals-triangle/Groovy/pascals-triangle-1.groovy
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def pascal
|
||||
pascal = { n -> (n <= 1) ? [1] : [[0] + pascal(n - 1), pascal(n - 1) + [0]].transpose().collect { it.sum() } }
|
||||
4
Task/Pascals-triangle/Groovy/pascals-triangle-2.groovy
Normal file
4
Task/Pascals-triangle/Groovy/pascals-triangle-2.groovy
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def count = 15
|
||||
(1..count).each { n ->
|
||||
printf ("%2d:", n); (0..(count-n)).each { print " " }; pascal(n).each{ printf("%6d ", it) }; println ""
|
||||
}
|
||||
4
Task/Pascals-triangle/Haskell/pascals-triangle-1.hs
Normal file
4
Task/Pascals-triangle/Haskell/pascals-triangle-1.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
zapWith :: (a -> a -> a) -> [a] -> [a] -> [a]
|
||||
zapWith f xs [] = xs
|
||||
zapWith f [] ys = ys
|
||||
zapWith f (x:xs) (y:ys) = f x y : zapWith f xs ys
|
||||
2
Task/Pascals-triangle/Haskell/pascals-triangle-2.hs
Normal file
2
Task/Pascals-triangle/Haskell/pascals-triangle-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
extendWith f [] = []
|
||||
extendWith f xs@(x:ys) = x : zapWith f xs ys
|
||||
1
Task/Pascals-triangle/Haskell/pascals-triangle-3.hs
Normal file
1
Task/Pascals-triangle/Haskell/pascals-triangle-3.hs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pascal = iterate (extendWith (+)) [1]
|
||||
2
Task/Pascals-triangle/Haskell/pascals-triangle-4.hs
Normal file
2
Task/Pascals-triangle/Haskell/pascals-triangle-4.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
*Main> take 6 pascal
|
||||
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1],[1,5,10,10,5,1]]
|
||||
5
Task/Pascals-triangle/Haskell/pascals-triangle-5.hs
Normal file
5
Task/Pascals-triangle/Haskell/pascals-triangle-5.hs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
-- generate next row from current row
|
||||
nextRow row = zipWith (+) ([0] ++ row) (row ++ [0])
|
||||
|
||||
-- returns the first n rows
|
||||
pascal = iterate nextRow [1]
|
||||
4
Task/Pascals-triangle/Haskell/pascals-triangle-6.hs
Normal file
4
Task/Pascals-triangle/Haskell/pascals-triangle-6.hs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pascal :: [[Integer]]
|
||||
pascal =
|
||||
(1 : [ 0 | _ <- head pascal])
|
||||
: [zipWith (+) (0:row) row | row <- pascal]
|
||||
2
Task/Pascals-triangle/Haskell/pascals-triangle-7.hs
Normal file
2
Task/Pascals-triangle/Haskell/pascals-triangle-7.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
*Pascal> take 5 <$> (take 5 $ triangle)
|
||||
[[1,0,0,0,0],[1,1,0,0,0],[1,2,1,0,0],[1,3,3,1,0],[1,4,6,4,1]]
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue