dice5; create dice7 that generates a pseudo-random integer from
+1 to 7 in equal probability using only dice5 as a source of random
+numbers, and check the distribution for at least 1000000 calls using the function created in [[Simple Random Distribution Checker]].
+
+'''Implementation suggestion:'''
+dice7 might call dice5 twice, re-call if four of the 25
+combinations are given, otherwise split the other 21 combinations
+into 7 groups of three, and return the group index from the rolls.
+
+(Task adapted from an answer [http://stackoverflow.com/questions/90715/what-are-the-best-programming-puzzles-you-came-across here])
diff --git a/Task/Seven-sided-dice-from-five-sided-dice/1META.yaml b/Task/Seven-sided-dice-from-five-sided-dice/1META.yaml
new file mode 100644
index 0000000000..29aeacfd2a
--- /dev/null
+++ b/Task/Seven-sided-dice-from-five-sided-dice/1META.yaml
@@ -0,0 +1,2 @@
+---
+note: Probability and statistics
diff --git a/Task/Seven-sided-dice-from-five-sided-dice/ALGOL-68/seven-sided-dice-from-five-sided-dice.alg b/Task/Seven-sided-dice-from-five-sided-dice/ALGOL-68/seven-sided-dice-from-five-sided-dice.alg
new file mode 100644
index 0000000000..b8490234c6
--- /dev/null
+++ b/Task/Seven-sided-dice-from-five-sided-dice/ALGOL-68/seven-sided-dice-from-five-sided-dice.alg
@@ -0,0 +1,35 @@
+PROC dice5 = INT:
+ 1 + ENTIER (5*random);
+
+PROC mulby5 = (INT n)INT:
+ ABS (BIN n SHL 2) + n;
+
+PROC dice7 = INT: (
+ INT d55 := 0;
+ INT m := 1;
+ WHILE
+ m := ABS ((2r1 AND BIN m) SHL 2) + ABS (BIN m SHR 1); # repeats 4 - 2 - 1 #
+ d55 := mulby5(mulby5(d55)) + mulby5(dice5) + dice5 - 6;
+# WHILE # d55 < m DO SKIP OD;
+
+ m := 1;
+ WHILE d55>0 DO
+ d55 +:= m;
+ m := ABS (BIN d55 AND 2r111); # modulas by 8 #
+ d55 := ABS (BIN d55 SHR 3) # divide by 8 #
+ OD;
+ m
+);
+
+PROC distcheck = (PROC INT dice, INT count, upb)VOID: (
+ [upb]INT sum; FOR i TO UPB sum DO sum[i] := 0 OD;
+ FOR i TO count DO sum[dice]+:=1 OD;
+ FOR i TO UPB sum WHILE print(whole(sum[i],0)); i /= UPB sum DO print(", ") OD;
+ print(new line)
+);
+
+main:
+(
+ distcheck(dice5, 1000000, 5);
+ distcheck(dice7, 1000000, 7)
+)
diff --git a/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-1.ada b/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-1.ada
new file mode 100644
index 0000000000..9cd42ceca5
--- /dev/null
+++ b/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-1.ada
@@ -0,0 +1,10 @@
+package Random_57 is
+
+ type Mod_7 is mod 7;
+
+ function Random7 return Mod_7;
+ -- a "fast" implementation, minimazing the calls to the Random5 generator
+ function Simple_Random7 return Mod_7;
+ -- a simple implementation
+
+end Random_57;
diff --git a/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-2.ada b/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-2.ada
new file mode 100644
index 0000000000..7e7e881613
--- /dev/null
+++ b/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-2.ada
@@ -0,0 +1,57 @@
+ with Ada.Numerics.Discrete_Random;
+
+package body Random_57 is
+ type M5 is mod 5;
+
+ package Rand_5 is new Ada.Numerics.Discrete_Random(M5);
+ Gen: Rand_5.Generator;
+ function Random7 return Mod_7 is
+ N: Natural;
+
+ begin
+ loop
+ N := Integer(Rand_5.Random(Gen))* 5 + Integer(Rand_5.Random(Gen));
+ -- N is uniformly distributed in 0 .. 24
+ if N < 21 then
+ return Mod_7(N/3);
+ else -- (N-21) is in 0 .. 3
+ N := (N-21) * 5 + Integer(Rand_5.Random(Gen)); -- N is in 0 .. 19
+ if N < 14 then
+ return Mod_7(N / 2);
+ else -- (N-14) is in 0 .. 5
+ N := (N-14) * 5 + Integer(Rand_5.Random(Gen)); -- N is in 0 .. 29
+ if N < 28 then
+ return Mod_7(N/4);
+ else -- (N-28) is in 0 .. 1
+ N := (N-28) * 5 + Integer(Rand_5.Random(Gen)); -- 0 .. 9
+ if N < 7 then
+ return Mod_7(N);
+ else -- (N-7) is in 0, 1, 2
+ N := (N-7)* 5 + Integer(Rand_5.Random(Gen)); -- 0 .. 14
+ if N < 14 then
+ return Mod_7(N/2);
+ else -- (N-14) is 0. This is not useful for us!
+ null;
+ end if;
+ end if;
+ end if;
+ end if;
+ end if;
+ end loop;
+
+ end Random7;
+
+ function Simple_Random7 return Mod_7 is
+ N: Natural :=
+ Integer(Rand_5.Random(Gen))* 5 + Integer(Rand_5.Random(Gen));
+ -- N is uniformly distributed in 0 .. 24
+ begin
+ while N > 20 loop
+ N := Integer(Rand_5.Random(Gen))* 5 + Integer(Rand_5.Random(Gen));
+ end loop; -- Now I <= 20
+ return Mod_7(N / 3);
+ end Simple_Random7;
+
+begin
+ Rand_5.Reset(Gen);
+end Random_57;
diff --git a/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-3.ada b/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-3.ada
new file mode 100644
index 0000000000..0d15b8dbae
--- /dev/null
+++ b/Task/Seven-sided-dice-from-five-sided-dice/Ada/seven-sided-dice-from-five-sided-dice-3.ada
@@ -0,0 +1,51 @@
+with Ada.Text_IO, Random_57;
+
+procedure R57 is
+
+ use Random_57;
+
+ type Fun is access function return Mod_7;
+
+ function Rand return Mod_7 renames Random_57.Random7;
+ -- change this to "... renames Random_57.Simple_Random;" if you like
+
+ procedure Test(Sample_Size: Positive; Rand: Fun; Precision: Float := 0.3) is
+
+ Counter: array(Mod_7) of Natural := (others => 0);
+ Expected: Natural := Sample_Size/7;
+ Small: Mod_7 := Mod_7'First;
+ Large: Mod_7 := Mod_7'First;
+
+ Result: Mod_7;
+ begin
+ Ada.Text_IO.New_Line;
+ Ada.Text_IO.Put_Line("Sample Size: " & Integer'Image(Sample_Size));
+ Ada.Text_IO.Put( " Bins:");
+ for I in 1 .. Sample_Size loop
+ Result := Rand.all;
+ Counter(Result) := Counter(Result) + 1;
+ end loop;
+ for J in Mod_7 loop
+ Ada.Text_IO.Put(Integer'Image(Counter(J)));
+ if Counter(J) < Counter(Small) then Small := J; end if;
+ if Counter(J) > Counter(Large) then Large := J; end if;
+ end loop;
+ Ada.Text_IO.New_Line;
+ Ada.Text_IO.Put_Line(" Small Bin:" & Integer'Image(Counter(Small)));
+ Ada.Text_IO.Put_Line(" Large Bin: " & Integer'Image(Counter(Large)));
+
+ if Float(Counter(Small)*7) * (1.0+Precision) < Float(Sample_Size) then
+ Ada.Text_IO.Put_Line("Failed! Small too small!");
+ elsif Float(Counter(Large)*7) * (1.0-Precision) > Float(Sample_Size) then
+ Ada.Text_IO.Put_Line("Failed! Large too large!");
+ else
+ Ada.Text_IO.Put_Line("Passed");
+ end if;
+ end Test;
+
+begin
+ Test( 10_000, Rand'Access, 0.08);
+ Test( 100_000, Rand'Access, 0.04);
+ Test( 1_000_000, Rand'Access, 0.02);
+ Test(10_000_000, Rand'Access, 0.01);
+end R57;
diff --git a/Task/Seven-sided-dice-from-five-sided-dice/AutoHotkey/seven-sided-dice-from-five-sided-dice.ahk b/Task/Seven-sided-dice-from-five-sided-dice/AutoHotkey/seven-sided-dice-from-five-sided-dice.ahk
new file mode 100644
index 0000000000..9cf348068c
--- /dev/null
+++ b/Task/Seven-sided-dice-from-five-sided-dice/AutoHotkey/seven-sided-dice-from-five-sided-dice.ahk
@@ -0,0 +1,11 @@
+dice5()
+{ Random, v, 1, 5
+ Return, v
+}
+
+dice7()
+{ Loop
+ { v := 5 * dice5() + dice5() - 6
+ IfLess v, 21, Return, (v // 3) + 1
+ }
+}
diff --git a/Task/Seven-sided-dice-from-five-sided-dice/BBC-BASIC/seven-sided-dice-from-five-sided-dice.bbc b/Task/Seven-sided-dice-from-five-sided-dice/BBC-BASIC/seven-sided-dice-from-five-sided-dice.bbc
new file mode 100644
index 0000000000..c6a12d561f
--- /dev/null
+++ b/Task/Seven-sided-dice-from-five-sided-dice/BBC-BASIC/seven-sided-dice-from-five-sided-dice.bbc
@@ -0,0 +1,31 @@
+ MAXRND = 7
+ FOR r% = 2 TO 5
+ check% = FNdistcheck(FNdice7, 10^r%, 0.1)
+ PRINT "Over "; 10^r% " runs dice7 ";
+ IF check% THEN
+ PRINT "failed distribution check with "; check% " bin(s) out of range"
+ ELSE
+ PRINT "passed distribution check"
+ ENDIF
+ NEXT
+ END
+
+ DEF FNdice7
+ LOCAL x% : x% = FNdice5 + 5*FNdice5
+ IF x%>26 THEN = FNdice7 ELSE = (x%+1) MOD 7 + 1
+
+ DEF FNdice5 = RND(5)
+
+ DEF FNdistcheck(RETURN func%, repet%, delta)
+ LOCAL i%, m%, r%, s%, bins%()
+ DIM bins%(MAXRND)
+ FOR i% = 1 TO repet%
+ r% = FN(^func%)
+ bins%(r%) += 1
+ IF r%>m% m% = r%
+ NEXT
+ FOR i% = 1 TO m%
+ IF bins%(i%)/(repet%/m%) > 1+delta s% += 1
+ IF bins%(i%)/(repet%/m%) < 1-delta s% += 1
+ NEXT
+ = s%
diff --git a/Task/Seven-sided-dice-from-five-sided-dice/C++/seven-sided-dice-from-five-sided-dice.cpp b/Task/Seven-sided-dice-from-five-sided-dice/C++/seven-sided-dice-from-five-sided-dice.cpp
new file mode 100644
index 0000000000..d91289dca9
--- /dev/null
+++ b/Task/Seven-sided-dice-from-five-sided-dice/C++/seven-sided-dice-from-five-sided-dice.cpp
@@ -0,0 +1,49 @@
+templateand):
+:x = a() and b()
+Then it would be best to not compute the value of if the value of is computed as , as the value of can then only ever be .
+
+Similarly, if we needed to compute the disjunction (or):
+:y = a() or b()
+Then it would be best to not compute the value of if the value of is computed as , as the value of can then only ever be .
+
+Some languages will stop further computation of boolean equations as soon as the result is known, so-called [[wp:Short-circuit evaluation|short-circuit evaluation]] of boolean expressions
+
+;Task Description
+The task is to create two functions named and , that take and return the same boolean value. The functions should also print their name whenever they are called. Calculate and assign the values of the following equations to a variable in such a way that function is only called when necessary:
+:x = a(i) and b(j)
+:y = a(i) or b(j)
+If the language does not have short-circuit evaluation, this might be achieved with nested if statements.
diff --git a/Task/Short-circuit-evaluation/1META.yaml b/Task/Short-circuit-evaluation/1META.yaml
new file mode 100644
index 0000000000..c78e2d9a80
--- /dev/null
+++ b/Task/Short-circuit-evaluation/1META.yaml
@@ -0,0 +1,2 @@
+---
+note: Programming language concepts
diff --git a/Task/Short-circuit-evaluation/ALGOL-68/short-circuit-evaluation-1.alg b/Task/Short-circuit-evaluation/ALGOL-68/short-circuit-evaluation-1.alg
new file mode 100644
index 0000000000..cbb7f93085
--- /dev/null
+++ b/Task/Short-circuit-evaluation/ALGOL-68/short-circuit-evaluation-1.alg
@@ -0,0 +1,38 @@
+PRIO ORELSE = 2, ANDTHEN = 3; # user defined operators #
+OP ORELSE = (BOOL a, PROC BOOL b)BOOL: ( a | a | b ),
+ ANDTHEN = (BOOL a, PROC BOOL b)BOOL: ( a | b | a );
+
+# user defined Short-circuit_evaluation procedures #
+PROC or else = (BOOL a, PROC BOOL b)BOOL: ( a | a | b ),
+ and then = (BOOL a, PROC BOOL b)BOOL: ( a | b | a );
+
+test:(
+
+ PROC a = (BOOL a)BOOL: ( print(("a=",a,", ")); a),
+ b = (BOOL b)BOOL: ( print(("b=",b,", ")); b);
+
+CO
+# Valid for Algol 68 Rev0: using "user defined" operators #
+# Note: here BOOL is being automatically "procedured" to PROC BOOL #
+ print(("T ORELSE F = ", a(TRUE) ORELSE b(FALSE), new line));
+ print(("F ANDTHEN T = ", a(FALSE) ANDTHEN b(TRUE), new line));
+
+ print(("or else(T, F) = ", or else(a(TRUE), b(FALSE)), new line));
+ print(("and then(F, T) = ", and then(a(FALSE), b(TRUE)), new line));
+END CO
+
+# Valid for Algol68 Rev1: using "user defined" operators #
+# Note: BOOL must be manually "procedured" to PROC BOOL #
+ print(("T ORELSE F = ", a(TRUE) ORELSE (BOOL:b(FALSE)), new line));
+ print(("T ORELSE T = ", a(TRUE) ORELSE (BOOL:b(TRUE)), new line));
+
+ print(("F ANDTHEN F = ", a(FALSE) ANDTHEN (BOOL:b(FALSE)), new line));
+ print(("F ANDTHEN T = ", a(FALSE) ANDTHEN (BOOL:b(TRUE)), new line));
+
+ print(("F ORELSE F = ", a(FALSE) ORELSE (BOOL:b(FALSE)), new line));
+ print(("F ORELSE T = ", a(FALSE) ORELSE (BOOL:b(TRUE)), new line));
+
+ print(("T ANDTHEN F = ", a(TRUE) ANDTHEN (BOOL:b(FALSE)), new line));
+ print(("T ANDTHEN T = ", a(TRUE) ANDTHEN (BOOL:b(TRUE)), new line))
+
+)
diff --git a/Task/Short-circuit-evaluation/ALGOL-68/short-circuit-evaluation-2.alg b/Task/Short-circuit-evaluation/ALGOL-68/short-circuit-evaluation-2.alg
new file mode 100644
index 0000000000..b5e9a90e33
--- /dev/null
+++ b/Task/Short-circuit-evaluation/ALGOL-68/short-circuit-evaluation-2.alg
@@ -0,0 +1,25 @@
+test:(
+
+ PROC a = (BOOL a)BOOL: ( print(("a=",a,", ")); a),
+ b = (BOOL b)BOOL: ( print(("b=",b,", ")); b);
+
+# Valid for Algol 68G and 68RS using non standard operators #
+ print(("T OREL F = ", a(TRUE) OREL b(FALSE), new line));
+ print(("T OREL T = ", a(TRUE) OREL b(TRUE), new line));
+
+ print(("F ANDTH F = ", a(FALSE) ANDTH b(FALSE), new line));
+ print(("F ANDTH T = ", a(FALSE) ANDTH b(TRUE), new line));
+
+ print(("F OREL F = ", a(FALSE) OREL b(FALSE), new line));
+ print(("F OREL T = ", a(FALSE) OREL b(TRUE), new line));
+
+ print(("T ANDTH F = ", a(TRUE) ANDTH b(FALSE), new line));
+ print(("T ANDTH T = ", a(TRUE) ANDTH b(TRUE), new line))
+
+CO;
+# Valid for Algol 68G and 68C using non standard operators #
+ print(("T ORF F = ", a(TRUE) ORF b(FALSE), new line));
+ print(("F ANDF T = ", a(FALSE) ANDF b(TRUE), new line))
+END CO
+
+)
diff --git a/Task/Short-circuit-evaluation/Ada/short-circuit-evaluation.ada b/Task/Short-circuit-evaluation/Ada/short-circuit-evaluation.ada
new file mode 100644
index 0000000000..b6931bf0c5
--- /dev/null
+++ b/Task/Short-circuit-evaluation/Ada/short-circuit-evaluation.ada
@@ -0,0 +1,27 @@
+with Ada.Text_IO; use Ada.Text_IO;
+
+procedure Test_Short_Circuit is
+ function A (Value : Boolean) return Boolean is
+ begin
+ Put (" A=" & Boolean'Image (Value));
+ return Value;
+ end A;
+ function B (Value : Boolean) return Boolean is
+ begin
+ Put (" B=" & Boolean'Image (Value));
+ return Value;
+ end B;
+begin
+ for I in Boolean'Range loop
+ for J in Boolean'Range loop
+ Put (" (A and then B)=" & Boolean'Image (A (I) and then B (J)));
+ New_Line;
+ end loop;
+ end loop;
+ for I in Boolean'Range loop
+ for J in Boolean'Range loop
+ Put (" (A or else B)=" & Boolean'Image (A (I) or else B (J)));
+ New_Line;
+ end loop;
+ end loop;
+end Test_Short_Circuit;
diff --git a/Task/Short-circuit-evaluation/AutoHotkey/short-circuit-evaluation.ahk b/Task/Short-circuit-evaluation/AutoHotkey/short-circuit-evaluation.ahk
new file mode 100644
index 0000000000..dcbe2b1d30
--- /dev/null
+++ b/Task/Short-circuit-evaluation/AutoHotkey/short-circuit-evaluation.ahk
@@ -0,0 +1,16 @@
+i = 1
+j = 1
+x := a(i) and b(j)
+y := a(i) or b(j)
+
+a(p)
+{
+ MsgBox, a() was called with the parameter "%p%".
+ Return, p
+}
+
+b(p)
+{
+ MsgBox, b() was called with the parameter "%p%".
+ Return, p
+}
diff --git a/Task/Short-circuit-evaluation/BBC-BASIC/short-circuit-evaluation.bbc b/Task/Short-circuit-evaluation/BBC-BASIC/short-circuit-evaluation.bbc
new file mode 100644
index 0000000000..6fe0058de5
--- /dev/null
+++ b/Task/Short-circuit-evaluation/BBC-BASIC/short-circuit-evaluation.bbc
@@ -0,0 +1,28 @@
+ REM TRUE is represented as -1, FALSE as 0
+ FOR i% = TRUE TO FALSE
+ FOR j% = TRUE TO FALSE
+ PRINT "For x=a(";FNboolstring(i%);") AND b(";FNboolstring(j%);")"
+ x% = FALSE
+ REM Short-circuit AND can be simulated by cascaded IFs:
+ IF FNa(i%) IF FNb(j%) THEN x%=TRUE
+ PRINT "x is ";FNboolstring(x%)
+ PRINT
+ PRINT "For y=a(";FNboolstring(i%);") OR b(";FNboolstring(j%);")"
+ y% = FALSE
+ REM Short-circuit OR can be simulated by De Morgan's laws:
+ IF NOTFNa(i%) IF NOTFNb(j%) ELSE y%=TRUE : REM Note ELSE without THEN
+ PRINT "y is ";FNboolstring(y%)
+ PRINT
+ NEXT:NEXT
+ END
+
+ DEFFNa(bool%)
+ PRINT "Function A used; ";
+ =bool%
+
+ DEFFNb(bool%)
+ PRINT "Function B used; ";
+ =bool%
+
+ DEFFNboolstring(bool%)
+ IF bool%=0 THEN ="FALSE" ELSE="TRUE"
diff --git a/Task/Short-circuit-evaluation/C++/short-circuit-evaluation.cpp b/Task/Short-circuit-evaluation/C++/short-circuit-evaluation.cpp
new file mode 100644
index 0000000000..d75d9ba633
--- /dev/null
+++ b/Task/Short-circuit-evaluation/C++/short-circuit-evaluation.cpp
@@ -0,0 +1,27 @@
+#include ########################### +# ## ## ## ## ## ## ## ## # +########################### +### ###### ###### ### +# # # ## # # ## # # # +### ###### ###### ### +########################### +# ## ## ## ## ## ## ## ## # +########################### +######### ######### +# ## ## # # ## ## # +######### ######### +### ### ### ### +# # # # # # # # +### ### ### ### +######### ######### +# ## ## # # ## ## # +######### ######### +########################### +# ## ## ## ## ## ## ## ## # +########################### +### ###### ###### ### +# # # ## # # ## # # # +### ###### ###### ### +########################### +# ## ## ## ## ## ## ## ## # +###########################+ +The use of # characters is not rigidly required for ASCII art. The important requirement is the placement of whitespace and non-whitespace characters. + +See also [[Sierpinski triangle]] diff --git a/Task/Sierpinski-carpet/1META.yaml b/Task/Sierpinski-carpet/1META.yaml new file mode 100644 index 0000000000..bf76cfa1e6 --- /dev/null +++ b/Task/Sierpinski-carpet/1META.yaml @@ -0,0 +1,2 @@ +--- +note: Fractals diff --git a/Task/Sierpinski-carpet/ALGOL-68/sierpinski-carpet.alg b/Task/Sierpinski-carpet/ALGOL-68/sierpinski-carpet.alg new file mode 100644 index 0000000000..0ca176a033 --- /dev/null +++ b/Task/Sierpinski-carpet/ALGOL-68/sierpinski-carpet.alg @@ -0,0 +1,29 @@ +PROC in carpet = (INT in x, in y)BOOL: ( + INT x := in x, y := in y; + BOOL out; + DO + IF x = 0 OR y = 0 THEN + out := TRUE; GO TO stop iteration + ELIF x MOD 3 = 1 AND y MOD 3 = 1 THEN + out := FALSE; GO TO stop iteration + FI; + + x %:= 3; + y %:= 3 + OD; + stop iteration: out +); + +PROC carpet = (INT n)VOID: + FOR i TO 3 ** n DO + FOR j TO 3 ** n DO + IF in carpet(i-1, j-1) THEN + print("* ") + ELSE + print(" ") + FI + OD; + print(new line) + OD; + +carpet(3) diff --git a/Task/Sierpinski-carpet/AWK/sierpinski-carpet.awk b/Task/Sierpinski-carpet/AWK/sierpinski-carpet.awk new file mode 100644 index 0000000000..f7634094b0 --- /dev/null +++ b/Task/Sierpinski-carpet/AWK/sierpinski-carpet.awk @@ -0,0 +1,64 @@ +# WSC.AWK - Waclaw Sierpinski's carpet contributed by Dan Nielsen +# +# syntax: GAWK -f WSC.AWK [-v o={a|A}{b|B}] [-v X=anychar] iterations +# +# -v o=ab default +# a|A loose weave | tight weave +# b|B don't show | show how the carpet is built +# -v X=? Carpet is built with X's. The character assigned to X replaces all X's. +# +# iterations +# The number of iterations. The default is 0 which produces one carpet. +# +# what is the difference between a loose weave and a tight weave: +# loose tight +# X X X X X X X X X XXXXXXXXX +# X X X X X X X XX XX X +# X X X X X X X X X XXXXXXXXX +# X X X X X X XXX XXX +# X X X X X X X X +# X X X X X X XXX XXX +# X X X X X X X X X XXXXXXXXX +# X X X X X X X XX XX X +# X X X X X X X X X XXXXXXXXX +# +# examples: +# GAWK -f WSC.AWK 2 +# GAWK -f WSC.AWK -v o=Ab -v X=# 2 +# GAWK -f WSC.AWK -v o=Ab -v X=\xDB 2 +# +BEGIN { + optns = (o == "") ? "ab" : o + n = ARGV[1] + 0 # iterations + if (n !~ /^[0-9]+$/) { exit(1) } + seed = (optns ~ /A/) ? "XXX,X X,XXX" : "X X X ,X X ,X X X " # tight/loose weave + leng = row = split(seed,A,",") # seed the array + for (i=1; i<=n; i++) { # build carpet + for (a=1; a<=3; a++) { + row = 0 + for (b=1; b<=3; b++) { + for (c=1; c<=leng; c++) { + row++ + tmp = (a == 2 && b == 2) ? sprintf("%*s",length(A[c]),"") : A[c] + B[row] = B[row] tmp + } + if (optns ~ /B/) { # show how the carpet is built + if (max_row < row+0) { max_row = row } + for (r=1; r<=max_row; r++) { + printf("i=%d row=%02d a=%d b=%d '%s'\n",i,r,a,b,B[r]) + } + print("") + } + } + } + leng = row + for (j=1; j<=row; j++) { A[j] = B[j] } # re-seed the array + for (j in B) { delete B[j] } # delete work array + } + for (j=1; j<=row; j++) { # print carpet + if (X != "") { gsub(/X/,substr(X,1,1),A[j]) } + sub(/ +$/,"",A[j]) + printf("%s\n",A[j]) + } + exit(0) +} diff --git a/Task/Sierpinski-carpet/Ada/sierpinski-carpet.ada b/Task/Sierpinski-carpet/Ada/sierpinski-carpet.ada new file mode 100644 index 0000000000..1de0331b5e --- /dev/null +++ b/Task/Sierpinski-carpet/Ada/sierpinski-carpet.ada @@ -0,0 +1,76 @@ +with Ada.Text_Io; use Ada.Text_Io; + +procedure Sierpinski_Carpet is + subtype Index_Type is Integer range 1..81; + type Pattern_Array is array(Index_Type range <>, Index_Type range <>) of Boolean; + Pattern : Pattern_Array(1..81,1..81) := (Others =>(others => true)); + procedure Clear_Center(P : in out Pattern_Array; X1 : Index_Type; X2 : Index_Type; + Y1 : Index_Type; Y2 : Index_Type) is + Xfirst : Index_Type; + Xlast : Index_Type; + Yfirst : Index_Type; + Ylast : Index_Type; + Diff : Integer; + begin + Xfirst :=(X2 - X1 + 1) / 3 + X1; + Diff := Xfirst - X1; + Xlast := Xfirst + Diff; + Yfirst := (Y2 - Y1) / 3 + Y1; + YLast := YFirst + Diff; + + for I in XFirst..XLast loop + for J in YFirst..YLast loop + P(I, J) := False; + end loop; + end loop; + end Clear_Center; + + procedure Print(P : Pattern_Array) is + begin + for I in P'range(1) loop + for J in P'range(2) loop + if P(I,J) then + Put('*'); + else + Put(' '); + end if; + end loop; + New_Line; + end loop; + end Print; + + procedure Divide_Square(P : in out Pattern_Array; Order : Positive) is + Factor : Natural := 0; + X1, X2 : Index_Type; + Y1, Y2 : Index_Type; + Division : Index_Type; + Num_Sections : Index_Type; + begin + while Factor < Order loop + Num_Sections := 3**Factor; + Factor := Factor + 1; + X1 := P'First; + Division := P'Last / Num_Sections; + X2 := Division; + Y1 := X1; + Y2 := X2; + loop + loop + Clear_Center(P, X1, X2, Y1, Y2); + exit when X2 = P'Last; + X1 := X2; + X2 := X2 + Division; + end loop; + exit when Y2 = P'Last; + Y1 := Y2; + Y2 := Y2 + Division; + X1 := P'First; + X2 := Division; + end loop; + end loop; + end Divide_Square; + +begin + Divide_Square(Pattern, 3); + Print(Pattern); +end Sierpinski_Carpet; diff --git a/Task/Sierpinski-carpet/Asymptote/sierpinski-carpet.asymptote b/Task/Sierpinski-carpet/Asymptote/sierpinski-carpet.asymptote new file mode 100644 index 0000000000..cae76192db --- /dev/null +++ b/Task/Sierpinski-carpet/Asymptote/sierpinski-carpet.asymptote @@ -0,0 +1,50 @@ +path across(path p, real node) { + return + point(p, node + 1/3) + point(p, node - 1/3) - point(p, node); +} + +path corner_subquad(path p, real node) { + return + point(p, node) -- + point(p, node + 1/3) -- + across(p, node) -- + point(p, node - 1/3) -- + cycle; +} + +path noncorner_subquad(path p, real node1, real node2) { + return + point(p, node1 + 1/3) -- + across(p, node1) -- + across(p, node2) -- + point(p, node2 - 1/3) -- + cycle; +} + +void carpet(path p, int order) { + if (order == 0) + fill(p); + else { + for (real node : sequence(0, 3)) { + carpet(corner_subquad(p, node), order - 1); + carpet(noncorner_subquad(p, node, node + 1), order - 1); + } + } +} + +path q = + // A square + unitsquare + // An oblong rhombus + // (0, 0) -- (5, 3) -- (0, 6) -- (-5, 3) -- cycle + // A trapezoid + // (0, 0) -- (4, 2) -- (6, 2) -- (10, 0) -- cycle + // A less regular quadrilateral + // (0, 0) -- (4, 1) -- (9, -4) -- (1, -1) -- cycle + // A concave shape + // (0, 0) -- (5, 3) -- (10, 0) -- (5, 1) -- cycle + ; + +size(9 inches, 6 inches); + +carpet(q, 5); diff --git a/Task/Sierpinski-carpet/AutoHotkey/sierpinski-carpet.ahk b/Task/Sierpinski-carpet/AutoHotkey/sierpinski-carpet.ahk new file mode 100644 index 0000000000..2f94b1a1f5 --- /dev/null +++ b/Task/Sierpinski-carpet/AutoHotkey/sierpinski-carpet.ahk @@ -0,0 +1,20 @@ +Loop 4 + MsgBox % Carpet(A_Index) + +Carpet(n) { + Loop % 3**n { + x := A_Index-1 + Loop % 3**n + t .= Dot(x,A_Index-1) + t .= "`n" + } + Return t +} + +Dot(x,y) { + While x>0 && y>0 + If (mod(x,3)=1 && mod(y,3)=1) + Return " " + Else x //= 3, y //= 3 + Return "." +} diff --git a/Task/Sierpinski-carpet/BBC-BASIC/sierpinski-carpet.bbc b/Task/Sierpinski-carpet/BBC-BASIC/sierpinski-carpet.bbc new file mode 100644 index 0000000000..9b83f4a952 --- /dev/null +++ b/Task/Sierpinski-carpet/BBC-BASIC/sierpinski-carpet.bbc @@ -0,0 +1,18 @@ + Order% = 3 + side% = 3^Order% + VDU 23,22,8*side%;8*side%;64,64,16,128 + FOR Y% = 0 TO side%-1 + FOR X% = 0 TO side%-1 + IF FNincarpet(X%,Y%) PLOT X%*16,Y%*16+15 + NEXT + NEXT Y% + REPEAT WAIT 1 : UNTIL FALSE + END + + DEF FNincarpet(X%,Y%) + REPEAT + IF X% MOD 3 = 1 IF Y% MOD 3 = 1 THEN = FALSE + X% DIV= 3 + Y% DIV= 3 + UNTIL X%=0 AND Y%=0 + = TRUE diff --git a/Task/Sierpinski-carpet/C/sierpinski-carpet-1.c b/Task/Sierpinski-carpet/C/sierpinski-carpet-1.c new file mode 100644 index 0000000000..4789e1ce3d --- /dev/null +++ b/Task/Sierpinski-carpet/C/sierpinski-carpet-1.c @@ -0,0 +1,21 @@ +#include
| Client Maintenance | |
| Client Num | " + textbox #clientNum,clientNum$,5 + +html " |
| Name | " + textbox #name,name$,30 + +html " |
| Client Date | " + textbox #clientDate,clientDate$,19 + +html " |
| Category | " + textbox #category,category$,10 + +html " |
| " + button #acd, "Add", [addIt] + button #ex, "Exit", [sho] +html " | |
| Client Num | Name | Client Date | Category |
| ";clientNum;" | ";name$;" | ";clientDate$;" | ";category$;" |