Sierpinski carpet: Difference between revisions
Content added Content deleted
Puppydrum64 (talk | contribs) m (→{{header|C}}: typo fix) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 48: | Line 48: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F sierpinski_carpet(n) |
||
V carpet = [String(‘#’)] |
V carpet = [String(‘#’)] |
||
L 1..n |
L 1..n |
||
Line 56: | Line 56: | ||
R carpet.join("\n") |
R carpet.join("\n") |
||
print(sierpinski_carpet(3))</ |
print(sierpinski_carpet(3))</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">BYTE FUNC InCarpet(BYTE x,y) |
||
DO |
DO |
||
IF x MOD 3=1 AND y MOD 3=1 THEN |
IF x MOD 3=1 AND y MOD 3=1 THEN |
||
Line 102: | Line 102: | ||
DO UNTIL CH#$FF OD |
DO UNTIL CH#$FF OD |
||
CH=$FF |
CH=$FF |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_carpet.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_carpet.png Screenshot from Atari 8-bit computer] |
||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_Io; use Ada.Text_Io; |
||
procedure Sierpinski_Carpet is |
procedure Sierpinski_Carpet is |
||
Line 182: | Line 182: | ||
Divide_Square(Pattern, 3); |
Divide_Square(Pattern, 3); |
||
Print(Pattern); |
Print(Pattern); |
||
end Sierpinski_Carpet;</ |
end Sierpinski_Carpet;</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 189: | Line 189: | ||
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}} |
||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}} |
||
< |
<syntaxhighlight lang="algol68">PROC in carpet = (INT in x, in y)BOOL: ( |
||
INT x := in x, y := in y; |
INT x := in x, y := in y; |
||
BOOL out; |
BOOL out; |
||
Line 217: | Line 217: | ||
OD; |
OD; |
||
carpet(3)</ |
carpet(3)</syntaxhighlight> |
||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
{{Trans|C}} |
{{Trans|C}} |
||
As with the first C sample, uses pairs of characters for each point to give a squarer appreaence. |
As with the first C sample, uses pairs of characters for each point to give a squarer appreaence. |
||
< |
<syntaxhighlight lang="algolw">begin |
||
for depth := 3 do begin |
for depth := 3 do begin |
||
integer dim; |
integer dim; |
||
Line 241: | Line 241: | ||
end for_depth |
end for_depth |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 275: | Line 275: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">carpet←{{⊃⍪/,⌿3 3⍴4 0 4\⊂⍵}⍣⍵⊢⍪'#'}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 327: | Line 327: | ||
(ES5 Functional version) |
(ES5 Functional version) |
||
< |
<syntaxhighlight lang="applescript">----------------------- CARPET MODEL --------------------- |
||
-- sierpinskiCarpet :: Int -> [[Bool]] |
-- sierpinskiCarpet :: Int -> [[Bool]] |
||
Line 451: | Line 451: | ||
end script |
end script |
||
end if |
end if |
||
end mReturn</ |
end mReturn</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>███ |
<pre>███ |
||
Line 498: | Line 498: | ||
Or, defining the Sierpinski carpet weave more simply in terms of generic abstractions like '''zipWith''' and '''concatMap''': |
Or, defining the Sierpinski carpet weave more simply in terms of generic abstractions like '''zipWith''' and '''concatMap''': |
||
< |
<syntaxhighlight lang="applescript">-- weave :: [String] -> [String] |
||
on weave(xs) |
on weave(xs) |
||
script thread |
script thread |
||
Line 685: | Line 685: | ||
end |λ| |
end |λ| |
||
end script |
end script |
||
end zipWith</ |
end zipWith</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>█████████ |
<pre>█████████ |
||
Line 698: | Line 698: | ||
=={{header|Applesoft BASIC}}== |
=={{header|Applesoft BASIC}}== |
||
< |
<syntaxhighlight lang="applesoftbasic"> 100 HGR |
||
110 POKE 49234,0 |
110 POKE 49234,0 |
||
120 DEF FN M(X) = X - INT (D * 3) * INT (X / INT (D * 3)) |
120 DEF FN M(X) = X - INT (D * 3) * INT (X / INT (D * 3)) |
||
Line 711: | Line 711: | ||
210 HPLOT J,I |
210 HPLOT J,I |
||
220 NEXT J |
220 NEXT J |
||
230 NEXT I</ |
230 NEXT I</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">inCarpet?: function [x,y][ |
||
X: x |
X: x |
||
Y: y |
Y: y |
||
Line 739: | Line 739: | ||
] |
] |
||
carpet 3</ |
carpet 3</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 772: | Line 772: | ||
=={{header|Asymptote}}== |
=={{header|Asymptote}}== |
||
< |
<syntaxhighlight lang="asymptote">path across(path p, real node) { |
||
return |
return |
||
point(p, node + 1/3) + point(p, node - 1/3) - point(p, node); |
point(p, node + 1/3) + point(p, node - 1/3) - point(p, node); |
||
Line 821: | Line 821: | ||
size(9 inches, 6 inches); |
size(9 inches, 6 inches); |
||
carpet(q, 5);</ |
carpet(q, 5);</syntaxhighlight> |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
ahk [http://www.autohotkey.com/forum/topic44657-150.html discussion] |
ahk [http://www.autohotkey.com/forum/topic44657-150.html discussion] |
||
< |
<syntaxhighlight lang="autohotkey">Loop 4 |
||
MsgBox % Carpet(A_Index) |
MsgBox % Carpet(A_Index) |
||
Line 845: | Line 845: | ||
Else x //= 3, y //= 3 |
Else x //= 3, y //= 3 |
||
Return "." |
Return "." |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk"># 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 |
# syntax: GAWK -f WSC.AWK [-v o={a|A}{b|B}] [-v X=anychar] iterations |
||
Line 911: | Line 911: | ||
} |
} |
||
exit(0) |
exit(0) |
||
}</ |
}</syntaxhighlight> |
||
{{out|Sample}} |
{{out|Sample}} |
||
<pre> |
<pre> |
||
Line 941: | Line 941: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
<syntaxhighlight lang="basic256"> |
|||
<lang BASIC256> |
|||
function in_carpet(x, y) |
function in_carpet(x, y) |
||
while x <> 0 and y <> 0 |
while x <> 0 and y <> 0 |
||
Line 967: | Line 967: | ||
next k |
next k |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> Order% = 3 |
||
side% = 3^Order% |
side% = 3^Order% |
||
VDU 23,22,8*side%;8*side%;64,64,16,128 |
VDU 23,22,8*side%;8*side%;64,64,16,128 |
||
Line 989: | Line 989: | ||
Y% DIV= 3 |
Y% DIV= 3 |
||
UNTIL X%=0 AND Y%=0 |
UNTIL X%=0 AND Y%=0 |
||
= TRUE</ |
= TRUE</syntaxhighlight> |
||
[[File:sierpinski_carpet_bbc.gif]] |
[[File:sierpinski_carpet_bbc.gif]] |
||
Line 997: | Line 997: | ||
The order, N, is specified by the first number on the stack. The upper limit is implementation dependent and is determined by the interpreter's cell size. |
The order, N, is specified by the first number on the stack. The upper limit is implementation dependent and is determined by the interpreter's cell size. |
||
< |
<syntaxhighlight lang="befunge">311>*#3\>#-:#1_$:00p00g-#@_010p0>:20p10g30v |
||
>p>40p"#"30g40g*!#v_$48*30g3%1-v^ >$55+,1v> |
>p>40p"#"30g40g*!#v_$48*30g3%1-v^ >$55+,1v> |
||
0 ^p03/3g03/3g04$_v#!*!-1%3g04!<^_^#- g00 < |
0 ^p03/3g03/3g04$_v#!*!-1%3g04!<^_^#- g00 < |
||
^3g01p02:0p01_@#-g>#0,#02#:0#+g#11#g+#0:#<^</ |
^3g01p02:0p01_@#-g>#0,#02#:0#+g#11#g+#0:#<^</syntaxhighlight> |
||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
Line 1,010: | Line 1,010: | ||
1) a trit in the x-coordinate is 1, and |
1) a trit in the x-coordinate is 1, and |
||
2) the trit in the same position in the y-coordinate is also 1.</blockquote> |
2) the trit in the same position in the y-coordinate is also 1.</blockquote> |
||
< |
<syntaxhighlight lang="bqn">_decode ← {⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)} |
||
Carpet ← { # 2D Array method using ∾. |
Carpet ← { # 2D Array method using ∾. |
||
{∾(3‿3⥊4≠↕9)⊏⟨(≢𝕩)⥊0,𝕩⟩}⍟(𝕩-1) 1‿1⥊1 |
{∾(3‿3⥊4≠↕9)⊏⟨(≢𝕩)⥊0,𝕩⟩}⍟(𝕩-1) 1‿1⥊1 |
||
Line 1,019: | Line 1,019: | ||
•Show " #"⊏˜Carpet 4 |
•Show " #"⊏˜Carpet 4 |
||
•Show (Carpet ≡ Carpet1) 4</ |
•Show (Carpet ≡ Carpet1) 4</syntaxhighlight><syntaxhighlight lang="text">┌─ |
||
╵"########################### |
╵"########################### |
||
# ## ## ## ## ## ## ## ## # |
# ## ## ## ## ## ## ## ## # |
||
Line 1,048: | Line 1,048: | ||
###########################" |
###########################" |
||
┘ |
┘ |
||
1</ |
1</syntaxhighlight> |
||
Line 1,056: | Line 1,056: | ||
=={{header|Brainf***}}== |
=={{header|Brainf***}}== |
||
Double the first two prints for squarer output. |
Double the first two prints for squarer output. |
||
< |
<syntaxhighlight lang="brainf***">input order and print the associated Sierpinski carpet |
||
orders over 5 require larger cell sizes |
orders over 5 require larger cell sizes |
||
Line 1,070: | Line 1,070: | ||
+<<<-]>>>[<<<+>>>-]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[>[>+<-]<-]>[-]+>[[-]<->]<[->+<]> |
+<<<-]>>>[<<<+>>>-]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[>[>+<-]<-]>[-]+>[[-]<->]<[->+<]> |
||
[<<+>>-]<<[>>+++++[<+++++++>-]<.[-]<<[-]<[-]<<<->>>>>-]<<<-]<<]<-]++++++++++.[-] |
[<<+>>-]<<[>>+++++[<+++++++>-]<.[-]<<[-]<[-]<<<->>>>>-]<<<-]<<]<-]++++++++++.[-] |
||
<-]</ |
<-]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,103: | Line 1,103: | ||
=={{header|C}}== |
=={{header|C}}== |
||
If you write coordinates of any point on the carpet in base 3, the pixel is blank if and only if any matching pair of digits are (1, 1). |
If you write coordinates of any point on the carpet in base 3, the pixel is blank if and only if any matching pair of digits are (1, 1). |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int main() |
int main() |
||
Line 1,123: | Line 1,123: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 1,216: | Line 1,216: | ||
// fclose(f); |
// fclose(f); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Recursive version: |
Recursive version: |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 1,298: | Line 1,298: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
{{works with|C sharp|C#|3.0+}} |
{{works with|C sharp|C#|3.0+}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 1,326: | Line 1,326: | ||
Console.WriteLine(s); |
Console.WriteLine(s); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Performance focused variant (about 7x faster than the div/mod solutions at AMD Ryzen 7 4800H) |
Performance focused variant (about 7x faster than the div/mod solutions at AMD Ryzen 7 4800H) |
||
< |
<syntaxhighlight lang="cpp">// contributed to rosettacode.org by Peter Helcmanovsky |
||
// BCT = Binary-Coded Ternary: pairs of bits form one digit [0,1,2] (0b11 is invalid digit) |
// BCT = Binary-Coded Ternary: pairs of bits form one digit [0,1,2] (0b11 is invalid digit) |
||
Line 1,369: | Line 1,369: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
[[File:sierpinski_cpp.png|300px]] |
[[File:sierpinski_cpp.png|300px]] |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <windows.h> |
#include <windows.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 1,495: | Line 1,495: | ||
} |
} |
||
//-------------------------------------------------------------------------------------------------- |
//-------------------------------------------------------------------------------------------------- |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
{{trans|Scheme}} |
{{trans|Scheme}} |
||
< |
<syntaxhighlight lang="clojure">(ns example |
||
(:require [clojure.contrib.math :as math])) |
(:require [clojure.contrib.math :as math])) |
||
Line 1,518: | Line 1,518: | ||
(if (in-carpet? x y) "*" " "))))))) |
(if (in-carpet? x y) "*" " "))))))) |
||
(println (carpet 3))</ |
(println (carpet 3))</syntaxhighlight> |
||
=={{header|Commodore BASIC}}== |
=={{header|Commodore BASIC}}== |
||
< |
<syntaxhighlight lang="basic">100 PRINT CHR$(147); CHR$(18); "**** SIERPINSKI CARPET ****" |
||
110 PRINT |
110 PRINT |
||
120 INPUT "ORDER"; O$ |
120 INPUT "ORDER"; O$ |
||
Line 1,544: | Line 1,544: | ||
310 PRINT |
310 PRINT |
||
320 NEXT Y |
320 NEXT Y |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 1,568: | Line 1,568: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
This solution works by printing a square of # except where both of the coordinates of a cell contain a 1 in the same digit position in base 3. For example, the central empty square has a 1 in the highest base-3 digit of all its cells, and the smallest empty squares have 1s in the lowest base-3 digit. |
This solution works by printing a square of # except where both of the coordinates of a cell contain a 1 in the same digit position in base 3. For example, the central empty square has a 1 in the highest base-3 digit of all its cells, and the smallest empty squares have 1s in the lowest base-3 digit. |
||
< |
<syntaxhighlight lang="lisp">(defun print-carpet (order) |
||
(let ((size (expt 3 order))) |
(let ((size (expt 3 order))) |
||
(flet ((trinary (x) (format nil "~3,vR" order x)) |
(flet ((trinary (x) (format nil "~3,vR" order x)) |
||
Line 1,577: | Line 1,577: | ||
(princ (if (some #'ones (trinary i) (trinary j)) |
(princ (if (some #'ones (trinary i) (trinary j)) |
||
" " |
" " |
||
"#")))))))</ |
"#")))))))</syntaxhighlight> |
||
=={{header|Crystal}}== |
=={{header|Crystal}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">def sierpinski_carpet(n) |
||
carpet = ["#"] |
carpet = ["#"] |
||
n.times do |
n.times do |
||
Line 1,591: | Line 1,591: | ||
end |
end |
||
5.times{ |i| puts "\nN=#{i}"; sierpinski_carpet(i).each { |row| puts row } }</ |
5.times{ |i| puts "\nN=#{i}"; sierpinski_carpet(i).each { |row| puts row } }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,729: | Line 1,729: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.string, std.algorithm, std.array; |
||
auto sierpinskiCarpet(in int n) pure nothrow @safe { |
auto sierpinskiCarpet(in int n) pure nothrow @safe { |
||
Line 1,742: | Line 1,742: | ||
void main() { |
void main() { |
||
3.sierpinskiCarpet.writeln; |
3.sierpinskiCarpet.writeln; |
||
}</ |
}</syntaxhighlight> |
||
More functional style: |
More functional style: |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.functional; |
||
auto nextCarpet(in string[] c) pure nothrow { |
auto nextCarpet(in string[] c) pure nothrow { |
||
Line 1,757: | Line 1,757: | ||
.front |
.front |
||
.binaryReverseArgs!writefln("%-(%s\n%)"); |
.binaryReverseArgs!writefln("%-(%s\n%)"); |
||
}</ |
}</syntaxhighlight> |
||
A more direct and efficient version: |
A more direct and efficient version: |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.array; |
||
char[][] sierpinskiCarpet(in size_t n) pure nothrow @safe { |
char[][] sierpinskiCarpet(in size_t n) pure nothrow @safe { |
||
Line 1,786: | Line 1,786: | ||
writefln("%-(%s\n%)", 3.sierpinskiCarpet); |
writefln("%-(%s\n%)", 3.sierpinskiCarpet); |
||
7.sierpinskiCarpet.length.writeln; |
7.sierpinskiCarpet.length.writeln; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
Line 1,793: | Line 1,793: | ||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
{{Trans|Java}} |
{{Trans|Java}} |
||
< |
<syntaxhighlight lang="delphi">function InCarpet(x, y : Integer) : Boolean; |
||
begin |
begin |
||
while (x<>0) and (y<>0) do begin |
while (x<>0) and (y<>0) do begin |
||
Line 1,820: | Line 1,820: | ||
end; |
end; |
||
Carpet(3);</ |
Carpet(3);</syntaxhighlight> |
||
Line 1,826: | Line 1,826: | ||
=={{header|E}}== |
=={{header|E}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="e">def inCarpet(var x, var y) { |
||
while (x > 0 && y > 0) { |
while (x > 0 && y > 0) { |
||
if (x %% 3 <=> 1 && y %% 3 <=> 1) { |
if (x %% 3 <=> 1 && y %% 3 <=> 1) { |
||
Line 1,844: | Line 1,844: | ||
println() |
println() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def sierpinski_carpet(n), do: sierpinski_carpet(n, ["#"]) |
def sierpinski_carpet(n), do: sierpinski_carpet(n, ["#"]) |
||
Line 1,863: | Line 1,863: | ||
IO.puts "\nN=#{n}" |
IO.puts "\nN=#{n}" |
||
Enum.each(RC.sierpinski_carpet(n), fn line -> IO.puts line end) |
Enum.each(RC.sierpinski_carpet(n), fn line -> IO.puts line end) |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,917: | Line 1,917: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">% Implemented by Arjun Sunel |
||
-module(carpet). |
-module(carpet). |
||
-export([main/0]). |
-export([main/0]). |
||
Line 1,936: | Line 1,936: | ||
carpet(X div 3, Y div 3) |
carpet(X div 3, Y div 3) |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>*************************** |
<pre>*************************** |
||
Line 1,969: | Line 1,969: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM SIERP_CARPET |
PROGRAM SIERP_CARPET |
||
Line 2,001: | Line 2,001: | ||
CLOSE(1) |
CLOSE(1) |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output is redirected to file OUT.PRN: you can change this to SCRN: to screen or "LPTx:" for a parallel printer. |
Output is redirected to file OUT.PRN: you can change this to SCRN: to screen or "LPTx:" for a parallel printer. |
||
Output taken from OUT.PRN file: |
Output taken from OUT.PRN file: |
||
Line 2,036: | Line 2,036: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria"> |
||
include std/math.e |
include std/math.e |
||
Line 2,063: | Line 2,063: | ||
puts(1,'\n') |
puts(1,'\n') |
||
end for |
end for |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Excel}}== |
=={{header|Excel}}== |
||
Line 2,073: | Line 2,073: | ||
{{Works with|Office 365 betas 2021}} |
{{Works with|Office 365 betas 2021}} |
||
< |
<syntaxhighlight lang="lisp">SHOWBLOCKS |
||
=LAMBDA(xs, |
=LAMBDA(xs, |
||
IF(0 <> xs, "█", " ") |
IF(0 <> xs, "█", " ") |
||
Line 2,100: | Line 2,100: | ||
)(triple) |
)(triple) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
and also assuming the following generic bindings in the Name Manager for the WorkBook: |
and also assuming the following generic bindings in the Name Manager for the WorkBook: |
||
< |
<syntaxhighlight lang="lisp">APPENDCOLS |
||
=LAMBDA(xs, |
=LAMBDA(xs, |
||
LAMBDA(ys, |
LAMBDA(ys, |
||
Line 2,178: | Line 2,178: | ||
) |
) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,415: | Line 2,415: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
{{trans|OCaml}}{{trans|Ruby}} |
{{trans|OCaml}}{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="fsharp">open System |
||
let blank x = new String(' ', String.length x) |
let blank x = new String(' ', String.length x) |
||
Line 2,430: | Line 2,430: | ||
aux n ["#"] |
aux n ["#"] |
||
List.iter (printfn "%s") (sierpinskiCarpet 3)</ |
List.iter (printfn "%s") (sierpinskiCarpet 3)</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
The order n sierpinski carpet is the [[Kronecker product| Kronecker product]] of order n-1 and order 1. |
The order n sierpinski carpet is the [[Kronecker product| Kronecker product]] of order n-1 and order 1. |
||
< |
<syntaxhighlight lang="factor">USING: kernel math math.matrices prettyprint ; |
||
: sierpinski ( n -- ) |
: sierpinski ( n -- ) |
||
Line 2,440: | Line 2,440: | ||
curry times [ 1 = "#" " " ? ] matrix-map simple-table. ; |
curry times [ 1 = "#" " " ? ] matrix-map simple-table. ; |
||
3 sierpinski</ |
3 sierpinski</syntaxhighlight> |
||
=={{header|Fan}}== |
=={{header|Fan}}== |
||
<syntaxhighlight lang="fan">** |
|||
<lang Fan>** |
|||
** Generates a square Sierpinski gasket |
** Generates a square Sierpinski gasket |
||
** |
** |
||
Line 2,482: | Line 2,482: | ||
carpet(4) |
carpet(4) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Fennel}}== |
=={{header|Fennel}}== |
||
< |
<syntaxhighlight lang="fennel">(fn in-carpet? [x y] |
||
(if |
(if |
||
(or (= 0 x) (= 0 y)) true |
(or (= 0 x) (= 0 y)) true |
||
Line 2,501: | Line 2,501: | ||
(for [i 0 3] |
(for [i 0 3] |
||
(make-carpet i) |
(make-carpet i) |
||
(print))</ |
(print))</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{trans|Fan}} |
{{trans|Fan}} |
||
< |
<syntaxhighlight lang="forth">\ Generates a square Sierpinski gasket |
||
: 1? over 3 mod 1 = ; ( n1 n2 -- n1 n2 f) |
: 1? over 3 mod 1 = ; ( n1 n2 -- n1 n2 f) |
||
: 3/ 3 / swap ; ( n1 n2 -- n2/3 n1) |
: 3/ 3 / swap ; ( n1 n2 -- n2/3 n1) |
||
Line 2,520: | Line 2,520: | ||
; |
; |
||
cr 4 carpet</ |
cr 4 carpet</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="fortran">program Sierpinski_carpet |
||
implicit none |
implicit none |
||
Line 2,566: | Line 2,566: | ||
end do |
end do |
||
end subroutine Carpet |
end subroutine Carpet |
||
end program Sierpinski_carpet</ |
end program Sierpinski_carpet</syntaxhighlight> |
||
Line 2,572: | Line 2,572: | ||
{{trans|QB64}} |
{{trans|QB64}} |
||
===versión ASCII=== |
===versión ASCII=== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Function in_carpet(x As Uinteger, y As Uinteger) As Boolean |
Function in_carpet(x As Uinteger, y As Uinteger) As Boolean |
||
While x <> 0 And y <> 0 |
While x <> 0 And y <> 0 |
||
Line 2,597: | Line 2,597: | ||
Next k |
Next k |
||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{trans|QB64}} |
{{trans|QB64}} |
||
===versión gráfica=== |
===versión gráfica=== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Screenres 500, 545, 8 |
Screenres 500, 545, 8 |
||
Windowtitle "Sierpinski Carpet" |
Windowtitle "Sierpinski Carpet" |
||
Line 2,636: | Line 2,636: | ||
carpet(253, 293, 243, 3) |
carpet(253, 293, 243, 3) |
||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 2,647: | Line 2,647: | ||
[[File:SC5gp1.png|right|thumb|Output SC5gp1.png]] |
[[File:SC5gp1.png|right|thumb|Output SC5gp1.png]] |
||
< |
<syntaxhighlight lang="gnuplot"> |
||
## SCff.gp 1/14/17 aev |
## SCff.gp 1/14/17 aev |
||
## Plotting Sierpinski carpet fractal. |
## Plotting Sierpinski carpet fractal. |
||
Line 2,659: | Line 2,659: | ||
ttl = "Sierpinski carpet fractal, v.#1" |
ttl = "Sierpinski carpet fractal, v.#1" |
||
load "plotff.gp" |
load "plotff.gp" |
||
</ |
</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,675: | Line 2,675: | ||
;plotscf.gp: |
;plotscf.gp: |
||
< |
<syntaxhighlight lang="gnuplot"> |
||
## plotscf.gp 12/7/16 aev |
## plotscf.gp 12/7/16 aev |
||
## Plotting a Sierpinski carpet fractal to the png-file. |
## Plotting a Sierpinski carpet fractal to the png-file. |
||
Line 2,696: | Line 2,696: | ||
plot -100 |
plot -100 |
||
set output |
set output |
||
</syntaxhighlight> |
|||
</lang> |
|||
;plotscf1.gp: |
;plotscf1.gp: |
||
< |
<syntaxhighlight lang="gnuplot"> |
||
## plotscf1.gp 12/7/16 aev |
## plotscf1.gp 12/7/16 aev |
||
## Plotting a Sierpinski carpet fractal to the png-file. |
## Plotting a Sierpinski carpet fractal to the png-file. |
||
Line 2,727: | Line 2,727: | ||
splot sc(x,y) |
splot sc(x,y) |
||
set output |
set output |
||
</syntaxhighlight> |
|||
</lang> |
|||
;Plotting v.#2 and v.#3: |
;Plotting v.#2 and v.#3: |
||
< |
<syntaxhighlight lang="gnuplot"> |
||
## pSCF.gp 12/7/16 aev |
## pSCF.gp 12/7/16 aev |
||
## Plotting Sierpinski carpet fractals. |
## Plotting Sierpinski carpet fractals. |
||
Line 2,751: | Line 2,751: | ||
filename = "SCF31gp"; ttl = "Sierpinski carpet fractal #31, ord ".ord; |
filename = "SCF31gp"; ttl = "Sierpinski carpet fractal #31, ord ".ord; |
||
load "plotscf1.gp" |
load "plotscf1.gp" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,760: | Line 2,760: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Variable "grain" shown set to "#" here, but it's fun to experiment with other values. "|", ". ", "[]", "___", "██", "░░"... |
Variable "grain" shown set to "#" here, but it's fun to experiment with other values. "|", ". ", "[]", "___", "██", "░░"... |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,787: | Line 2,787: | ||
fmt.Println(r) |
fmt.Println(r) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution, uses list-indexing of base 3 string representation: |
Solution, uses list-indexing of base 3 string representation: |
||
< |
<syntaxhighlight lang="groovy">def base3 = { BigInteger i -> i.toString(3) } |
||
def sierpinskiCarpet = { int order -> |
def sierpinskiCarpet = { int order -> |
||
Line 2,809: | Line 2,809: | ||
} |
} |
||
sb.toString() |
sb.toString() |
||
}</ |
}</syntaxhighlight> |
||
Test Program: |
Test Program: |
||
< |
<syntaxhighlight lang="groovy">(0..4).each { println sierpinskiCarpet(it) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:30ex;overflow:scroll;"> |
<pre style="height:30ex;overflow:scroll;"> |
||
Line 2,941: | Line 2,941: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">inCarpet :: Int -> Int -> Bool |
||
inCarpet 0 _ = True |
inCarpet 0 _ = True |
||
inCarpet _ 0 = True |
inCarpet _ 0 = True |
||
Line 2,956: | Line 2,956: | ||
printCarpet :: Int -> IO () |
printCarpet :: Int -> IO () |
||
printCarpet = mapM_ putStrLn . carpet</ |
printCarpet = mapM_ putStrLn . carpet</syntaxhighlight> |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="haskell">nextCarpet :: [String] -> [String] |
||
nextCarpet carpet = border ++ map f carpet ++ border |
nextCarpet carpet = border ++ map f carpet ++ border |
||
where border = map (concat . replicate 3) carpet |
where border = map (concat . replicate 3) carpet |
||
Line 2,968: | Line 2,968: | ||
main :: IO () |
main :: IO () |
||
main = mapM_ putStrLn $ sierpinskiCarpet 3</ |
main = mapM_ putStrLn $ sierpinskiCarpet 3</syntaxhighlight> |
||
Seems not very different from version above, |
Seems not very different from version above, |
||
< |
<syntaxhighlight lang="haskell">main :: IO () |
||
main = putStr . unlines . (!!3) $ iterate next ["#"] |
main = putStr . unlines . (!!3) $ iterate next ["#"] |
||
Line 2,983: | Line 2,983: | ||
where |
where |
||
(!) = zipWith (++) |
(!) = zipWith (++) |
||
center = map (map $ const ' ') block</ |
center = map (map $ const ' ') block</syntaxhighlight> |
||
which we could also read as: |
which we could also read as: |
||
< |
<syntaxhighlight lang="haskell">carpet :: Int -> String |
||
carpet = unlines . (iterate weave ["██"] !!) |
carpet = unlines . (iterate weave ["██"] !!) |
||
Line 3,002: | Line 3,002: | ||
main :: IO () |
main :: IO () |
||
main = mapM_ (putStrLn . carpet) [0 .. 2]</ |
main = mapM_ (putStrLn . carpet) [0 .. 2]</syntaxhighlight> |
||
Or more applicatively, representing different phases of the weaving shuttle as <*> and <>: |
Or more applicatively, representing different phases of the weaving shuttle as <*> and <>: |
||
< |
<syntaxhighlight lang="haskell">carpet :: Int -> String |
||
carpet = unlines . (iterate weave ["██"] !!) |
carpet = unlines . (iterate weave ["██"] !!) |
||
Line 3,021: | Line 3,021: | ||
main :: IO () |
main :: IO () |
||
main = mapM_ (putStrLn . carpet) [0 .. 2]</ |
main = mapM_ (putStrLn . carpet) [0 .. 2]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>██ |
<pre>██ |
||
Line 3,043: | Line 3,043: | ||
{{works with|GHC}} |
{{works with|GHC}} |
||
{{libheader|diagrams}} |
{{libheader|diagrams}} |
||
< |
<syntaxhighlight lang="haskell">{-# LANGUAGE DoRec #-} |
||
import Control.Monad.Trans (lift) |
import Control.Monad.Trans (lift) |
||
import Data.Colour (Colour) |
import Data.Colour (Colour) |
||
Line 3,106: | Line 3,106: | ||
cell :: Colour Float -> Diagram Cairo R2 |
cell :: Colour Float -> Diagram Cairo R2 |
||
cell color = square 1 # lineWidth 0 # fillColor color |
cell color = square 1 # lineWidth 0 # fillColor color |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
The IsFilled procedure is a translation of Java and Python. |
The IsFilled procedure is a translation of Java and Python. |
||
< |
<syntaxhighlight lang="icon">$define FILLER "*" # the filler character |
||
procedure main(A) |
procedure main(A) |
||
Line 3,133: | Line 3,133: | ||
} |
} |
||
return |
return |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Carpet order= 2 |
<pre>Carpet order= 2 |
||
Line 3,149: | Line 3,149: | ||
=={{header|Io}}== |
=={{header|Io}}== |
||
Based on Python translation of Ruby. |
Based on Python translation of Ruby. |
||
< |
<syntaxhighlight lang="io">sierpinskiCarpet := method(n, |
||
carpet := list("@") |
carpet := list("@") |
||
n repeat( |
n repeat( |
||
Line 3,161: | Line 3,161: | ||
) |
) |
||
sierpinskiCarpet(3) println</ |
sierpinskiCarpet(3) println</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
<pre>@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
||
Line 3,193: | Line 3,193: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Like the sierpinski triangle, the carpet is straightforward to produce in J. One approach is based on repeatedly putting a function's argument in a box, forming 9 copies of it into a 3 by 3 array, and then replacing the contents of the middle box with blanks: |
Like the sierpinski triangle, the carpet is straightforward to produce in J. One approach is based on repeatedly putting a function's argument in a box, forming 9 copies of it into a 3 by 3 array, and then replacing the contents of the middle box with blanks: |
||
< |
<syntaxhighlight lang="j">N=:3 |
||
(a:(<1;1)}3 3$<)^:N' '</ |
(a:(<1;1)}3 3$<)^:N' '</syntaxhighlight> |
||
But N=:3 is big, so let's use N=:2 |
But N=:3 is big, so let's use N=:2 |
||
< |
<syntaxhighlight lang="j"> N=:2 |
||
(a:(<1;1)}3 3$<)^:N' ' |
(a:(<1;1)}3 3$<)^:N' ' |
||
┌─────────────┬─────────────┬─────────────┐ |
┌─────────────┬─────────────┬─────────────┐ |
||
Line 3,224: | Line 3,224: | ||
││ │ │ │││ │ │ │││ │ │ ││ |
││ │ │ │││ │ │ │││ │ │ ││ |
||
│└───┴───┴───┘│└───┴───┴───┘│└───┴───┴───┘│ |
│└───┴───┴───┘│└───┴───┴───┘│└───┴───┴───┘│ |
||
└─────────────┴─────────────┴─────────────┘</ |
└─────────────┴─────────────┴─────────────┘</syntaxhighlight> |
||
or another way of getting the same image starts with the boolean array |
or another way of getting the same image starts with the boolean array |
||
< |
<syntaxhighlight lang="j"> #:7 5 7 |
||
1 1 1 |
1 1 1 |
||
1 0 1 |
1 0 1 |
||
1 1 1</ |
1 1 1</syntaxhighlight> and uses that to select either a blank box or a boxed copy if the function's argument: |
||
< |
<syntaxhighlight lang="j"> N=:2 |
||
((#:7 5 7){_2{.<)^:N' ' |
((#:7 5 7){_2{.<)^:N' ' |
||
┌─────────────┬─────────────┬─────────────┐ |
┌─────────────┬─────────────┬─────────────┐ |
||
Line 3,258: | Line 3,258: | ||
││ │ │ │││ │ │ │││ │ │ ││ |
││ │ │ │││ │ │ │││ │ │ ││ |
||
│└───┴───┴───┘│└───┴───┴───┘│└───┴───┴───┘│ |
│└───┴───┴───┘│└───┴───┴───┘│└───┴───┴───┘│ |
||
└─────────────┴─────────────┴─────────────┘</ |
└─────────────┴─────────────┴─────────────┘</syntaxhighlight> |
||
That said, using spaces and '#' characters takes a bit more work. One approach would be: |
That said, using spaces and '#' characters takes a bit more work. One approach would be: |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
scarp=:{{' #'{~(#:7 5 7) ,/@(1 3 ,/"2@|: */)^:y ,.1}} |
scarp=:{{' #'{~(#:7 5 7) ,/@(1 3 ,/"2@|: */)^:y ,.1}} |
||
scarp 2 |
scarp 2 |
||
Line 3,301: | Line 3,301: | ||
########################### |
########################### |
||
# ## ## ## ## ## ## ## ## # |
# ## ## ## ## ## ## ## ## # |
||
###########################</ |
###########################</syntaxhighlight> |
||
Here, what we are doing is forming a tensor product of our #:7 5 7 boolean array with our argument and then collapsing two of the dimensions so they line up right. Our starting argument is the 1 by 1 array with the value 1. Once we have repeated this process enough times, we select spaces for our zeros and pound signs for our 1s. |
Here, what we are doing is forming a tensor product of our #:7 5 7 boolean array with our argument and then collapsing two of the dimensions so they line up right. Our starting argument is the 1 by 1 array with the value 1. Once we have repeated this process enough times, we select spaces for our zeros and pound signs for our 1s. |
||
Line 3,307: | Line 3,307: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="java">public static boolean inCarpet(long x, long y) { |
||
while (x!=0 && y!=0) { |
while (x!=0 && y!=0) { |
||
if (x % 3 == 1 && y % 3 == 1) |
if (x % 3 == 1 && y % 3 == 1) |
||
Line 3,325: | Line 3,325: | ||
System.out.println(); |
System.out.println(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
===Animated version=== |
===Animated version=== |
||
[[File:sierpinski_carpet_java.png|300px|thumb|right]] |
[[File:sierpinski_carpet_java.png|300px|thumb|right]] |
||
{{works with|java|8}} |
{{works with|java|8}} |
||
< |
<syntaxhighlight lang="java">import java.awt.*; |
||
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
||
import javax.swing.*; |
import javax.swing.*; |
||
Line 3,388: | Line 3,388: | ||
}); |
}); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 3,398: | Line 3,398: | ||
{{works with|Firefox|1.5+}} |
{{works with|Firefox|1.5+}} |
||
This version also produces a "graphic" via HTML and CSS. |
This version also produces a "graphic" via HTML and CSS. |
||
< |
<syntaxhighlight lang="html4strict"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
||
<html> |
<html> |
||
<head> |
<head> |
||
Line 3,469: | Line 3,469: | ||
</body> |
</body> |
||
</html></ |
</html></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[[File:Sierpinski carpet js.png]] |
[[File:Sierpinski carpet js.png]] |
||
Line 3,478: | Line 3,478: | ||
Creates an N by N array of boolean values, which are mapped to lines of characters for output. |
Creates an N by N array of boolean values, which are mapped to lines of characters for output. |
||
< |
<syntaxhighlight lang="javascript">// Orders 1, 2 and 3 of the Sierpinski Carpet |
||
// as lines of text. |
// as lines of text. |
||
Line 3,533: | Line 3,533: | ||
}).join('\n'); |
}).join('\n'); |
||
}).join('\n\n');</ |
}).join('\n\n');</syntaxhighlight> |
||
Output (orders 1, 2 and 3): |
Output (orders 1, 2 and 3): |
||
Line 3,580: | Line 3,580: | ||
===ES6=== |
===ES6=== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 3,622: | Line 3,622: | ||
return [1, 2, 3] |
return [1, 2, 3] |
||
.map(sierpinskiCarpet); |
.map(sierpinskiCarpet); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 3,670: | Line 3,670: | ||
Or, defining the Sierpinksi carpet weave declaratively, in terms of '''zipWith''' and '''concatMap''': |
Or, defining the Sierpinksi carpet weave declaratively, in terms of '''zipWith''' and '''concatMap''': |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 3,761: | Line 3,761: | ||
// MAIN ----------------------------------------------- |
// MAIN ----------------------------------------------- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>█████████ |
<pre>█████████ |
||
Line 3,780: | Line 3,780: | ||
This is like a "for" loop within a "for" loop in C-like languages, because range(0;n) generates a stream of n integers beginning at 0. The -1 is used to signal that a newline character is required. |
This is like a "for" loop within a "for" loop in C-like languages, because range(0;n) generates a stream of n integers beginning at 0. The -1 is used to signal that a newline character is required. |
||
<syntaxhighlight lang="jq"> |
|||
<lang jq> |
|||
def inCarpet(x; y): |
def inCarpet(x; y): |
||
x as $x | y as $y | |
x as $x | y as $y | |
||
Line 3,798: | Line 3,798: | ||
carpet(3)</ |
carpet(3)</syntaxhighlight>The following command produces the required pattern, and so the output is not repeated here: |
||
< |
<syntaxhighlight lang="sh">jq -n -r -c -f sierpinski.jq</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{works with|Julia|0.6}} |
{{works with|Julia|0.6}} |
||
< |
<syntaxhighlight lang="julia">function sierpinski(n::Integer, token::AbstractString="*") |
||
x = fill(token, 1, 1) |
x = fill(token, 1, 1) |
||
for _ in 1:n |
for _ in 1:n |
||
Line 3,819: | Line 3,819: | ||
end |
end |
||
sierpinski(2, "#") |> printsierpinski</ |
sierpinski(2, "#") |> printsierpinski</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 3,825: | Line 3,825: | ||
===ASCII Art Version=== |
===ASCII Art Version=== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun inCarpet(x: Int, y: Int): Boolean { |
fun inCarpet(x: Int, y: Int): Boolean { |
||
Line 3,846: | Line 3,846: | ||
} |
} |
||
fun main(args: Array<String>) = carpet(3)</ |
fun main(args: Array<String>) = carpet(3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,881: | Line 3,881: | ||
===Graphical Animated Version=== |
===Graphical Animated Version=== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.awt.* |
import java.awt.* |
||
Line 3,937: | Line 3,937: | ||
f.isVisible = true |
f.isVisible = true |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
An excellent opportunity to show off tail calls, so, recursively.. |
An excellent opportunity to show off tail calls, so, recursively.. |
||
< |
<syntaxhighlight lang="lua">local function carpet(n, f) |
||
print("n = " .. n) |
print("n = " .. n) |
||
local function S(x, y) |
local function S(x, y) |
||
Line 3,959: | Line 3,959: | ||
for n = 0, 4 do |
for n = 0, 4 do |
||
carpet(n, function(b) return b and "■ " or " " end) |
carpet(n, function(b) return b and "■ " or " " end) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="font-size:50%">n = 0 |
<pre style="font-size:50%">n = 0 |
||
Line 4,093: | Line 4,093: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
< |
<syntaxhighlight lang="lb">NoMainWin |
||
WindowWidth = 508 |
WindowWidth = 508 |
||
WindowHeight = 575 |
WindowHeight = 575 |
||
Line 4,137: | Line 4,137: | ||
Call carpet newX-side, newY, side, order |
Call carpet newX-side, newY, side, order |
||
End If |
End If |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
Replace a empty spot with a 3x3 empty matrix, and replace a full spot with an empty spot surrounded by 8 full spots: |
Replace a empty spot with a 3x3 empty matrix, and replace a full spot with an empty spot surrounded by 8 full spots: |
||
< |
<syntaxhighlight lang="mathematica">full={{1,1,1},{1,0,1},{1,1,1}} |
||
empty={{0,0,0},{0,0,0},{0,0,0}} |
empty={{0,0,0},{0,0,0},{0,0,0}} |
||
n=3; |
n=3; |
||
Grid[Nest[ArrayFlatten[#/.{0->empty,1->full}]&,{{1}},n]//.{0->" ",1->"#"}]</ |
Grid[Nest[ArrayFlatten[#/.{0->empty,1->full}]&,{{1}},n]//.{0->" ",1->"#"}]</syntaxhighlight> |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">n = 3; |
||
c = string('#'); |
c = string('#'); |
||
for k = 1 : n |
for k = 1 : n |
||
c = [c + c + c, c + c.replace('#', ' ') + c, c + c + c]; |
c = [c + c + c, c + c.replace('#', ' ') + c, c + c + c]; |
||
end |
end |
||
disp(c.join(char(10)))</ |
disp(c.join(char(10)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>########################### |
<pre>########################### |
||
Line 4,185: | Line 4,185: | ||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
{{works with|Nascom ROM BASIC|4.7}} |
{{works with|Nascom ROM BASIC|4.7}} |
||
< |
<syntaxhighlight lang="basic"> |
||
10 REM Sierpinski carpet |
10 REM Sierpinski carpet |
||
20 CLS |
20 CLS |
||
Line 4,222: | Line 4,222: | ||
410 DATA 27085,14336,-13564,6399,18178,10927 |
410 DATA 27085,14336,-13564,6399,18178,10927 |
||
420 DATA -8179,233 |
420 DATA -8179,233 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 4,271: | Line 4,271: | ||
end edge |
end edge |
||
return isFilled |
return isFilled |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Sample shown with order "2". |
Sample shown with order "2". |
||
Line 4,288: | Line 4,288: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import math |
||
proc inCarpet(x, y: int): bool = |
proc inCarpet(x, y: int): bool = |
||
Line 4,307: | Line 4,307: | ||
echo() |
echo() |
||
carpet(3)</ |
carpet(3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>* * * * * * * * * * * * * * * * * * * * * * * * * * * |
<pre>* * * * * * * * * * * * * * * * * * * * * * * * * * * |
||
Line 4,339: | Line 4,339: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="objeck">class SierpinskiCarpet { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
Carpet(3); |
Carpet(3); |
||
Line 4,367: | Line 4,367: | ||
}; |
}; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let rec in_carpet x y = |
||
if x = 0 || y = 0 then true |
if x = 0 || y = 0 then true |
||
else if x mod 3 = 1 && y mod 3 = 1 then false |
else if x mod 3 = 1 && y mod 3 = 1 then false |
||
Line 4,387: | Line 4,387: | ||
done; |
done; |
||
print_newline () |
print_newline () |
||
done</ |
done</syntaxhighlight> |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ocaml">let nextCarpet carpet = |
||
List.map (fun x -> x ^ x ^ x) carpet @ |
List.map (fun x -> x ^ x ^ x) carpet @ |
||
List.map (fun x -> x ^ String.make (String.length x) ' ' ^ x) carpet @ |
List.map (fun x -> x ^ String.make (String.length x) ' ' ^ x) carpet @ |
||
Line 4,402: | Line 4,402: | ||
let () = |
let () = |
||
List.iter print_endline (sierpinskiCarpet 3)</ |
List.iter print_endline (sierpinskiCarpet 3)</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: carpet(n) |
||
| dim i j k | |
| dim i j k | |
||
3 n pow ->dim |
3 n pow ->dim |
||
Line 4,420: | Line 4,420: | ||
] |
] |
||
printcr |
printcr |
||
] ;</ |
] ;</syntaxhighlight> |
||
=={{header|Order}}== |
=={{header|Order}}== |
||
Since the C Preprocessor cannot print newlines, this Order program produces a string for a simple C program to print: |
Since the C Preprocessor cannot print newlines, this Order program produces a string for a simple C program to print: |
||
< |
<syntaxhighlight lang="c">#include <order/interpreter.h> |
||
#define ORDER_PP_DEF_8in_carpet ORDER_PP_FN( \ |
#define ORDER_PP_DEF_8in_carpet ORDER_PP_FN( \ |
||
Line 4,462: | Line 4,462: | ||
printf(ORDER_PP( 8carpet_to_string(8carpet(3)) )); |
printf(ORDER_PP( 8carpet_to_string(8carpet(3)) )); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
(This example may take a long time to compile: change the <code>8carpet</code> parameter to 2 for a much quicker compile time and a smaller graphic.) |
(This example may take a long time to compile: change the <code>8carpet</code> parameter to 2 for a much quicker compile time and a smaller graphic.) |
||
Line 4,471: | Line 4,471: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">declare |
||
%% A carpet is a list of lines. |
%% A carpet is a list of lines. |
||
fun {NextCarpet Carpet} |
fun {NextCarpet Carpet} |
||
Line 4,492: | Line 4,492: | ||
in |
in |
||
%% print all lines of the Sierpinski carpet of order 3 |
%% print all lines of the Sierpinski carpet of order 3 |
||
{ForAll {Nth SierpinskiCarpets 4} System.showInfo}</ |
{ForAll {Nth SierpinskiCarpets 4} System.showInfo}</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Line 4,502: | Line 4,502: | ||
===Plotting helper functions=== |
===Plotting helper functions=== |
||
Note: wrtmat() can be found here on RC [[Brownian_tree#PARI.2FGP| Brownian tree page]]. |
Note: wrtmat() can be found here on RC [[Brownian_tree#PARI.2FGP| Brownian tree page]]. |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Improved simple plotting using matrix mat (color and scaling added). |
\\ Improved simple plotting using matrix mat (color and scaling added). |
||
\\ Matrix should be filled with 0/1. 7/6/16 aev |
\\ Matrix should be filled with 0/1. 7/6/16 aev |
||
Line 4,534: | Line 4,534: | ||
x\=3; y\=3;);\\wend |
x\=3; y\=3;);\\wend |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Sierpinski carpet fractal.=== |
===Sierpinski carpet fractal.=== |
||
< |
<syntaxhighlight lang="parigp"> |
||
\\ Sierpinski carpet fractal (n - order, clr - color, dfn - data file name) |
\\ Sierpinski carpet fractal (n - order, clr - color, dfn - data file name) |
||
\\ 6/10/16, upgraded 11/29/16 aev |
\\ 6/10/16, upgraded 11/29/16 aev |
||
Line 4,557: | Line 4,557: | ||
{pSierpinskiC(5,,"c:\\pariData\\SC5.dat"); |
{pSierpinskiC(5,,"c:\\pariData\\SC5.dat"); |
||
iPlotV2("c:\\pariData\\SC5.dat",10);} \\ SierpC5a.png, color - dark-green |
iPlotV2("c:\\pariData\\SC5.dat",10);} \\ SierpC5a.png, color - dark-green |
||
</ |
</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
Line 4,599: | Line 4,599: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program SierpinskiCarpet; |
||
uses |
uses |
||
Line 4,651: | Line 4,651: | ||
Carpet(3); |
Carpet(3); |
||
{$IFNDEF UNIX} readln; {$ENDIF} |
{$IFNDEF UNIX} readln; {$ENDIF} |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>:> ./SierpinskiCarpet |
<pre>:> ./SierpinskiCarpet |
||
Line 4,684: | Line 4,684: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">my @c = '##'; |
||
@c = (map($_ x 3, @c), map($_.(' ' x length).$_, @c), map($_ x 3, @c)) |
@c = (map($_ x 3, @c), map($_.(' ' x length).$_, @c), map($_ x 3, @c)) |
||
for 1 .. 3; |
for 1 .. 3; |
||
print join("\n", @c), "\n";</ |
print join("\n", @c), "\n";</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{Trans|Euphoria}} |
{{Trans|Euphoria}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">constant</span> <span style="color: #000000;">order</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span> |
<span style="color: #008080;">constant</span> <span style="color: #000000;">order</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">4</span> |
||
Line 4,711: | Line 4,711: | ||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span> |
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre style="font-size: 2px"> |
<pre style="font-size: 2px"> |
||
Line 4,799: | Line 4,799: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"><?php |
||
function isSierpinskiCarpetPixelFilled($x, $y) { |
function isSierpinskiCarpetPixelFilled($x, $y) { |
||
Line 4,826: | Line 4,826: | ||
sierpinskiCarpet($order); |
sierpinskiCarpet($order); |
||
echo PHP_EOL; |
echo PHP_EOL; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,880: | Line 4,880: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
{{works with|Picat}} |
{{works with|Picat}} |
||
<syntaxhighlight lang="picat"> |
|||
<lang Picat> |
|||
in_carpet(X, Y) => |
in_carpet(X, Y) => |
||
while (X != 0, Y != 0) |
while (X != 0, Y != 0) |
||
Line 4,902: | Line 4,902: | ||
nl |
nl |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,936: | Line 4,936: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="picolisp">(de carpet (N) |
||
(let Carpet '("#") |
(let Carpet '("#") |
||
(do N |
(do N |
||
Line 4,947: | Line 4,947: | ||
(mapcar '((S) (pack S S S)) Carpet) ) ) ) ) ) |
(mapcar '((S) (pack S S S)) Carpet) ) ) ) ) ) |
||
(mapc prinl (carpet 3))</ |
(mapc prinl (carpet 3))</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
/* Sierpinski carpet */ |
/* Sierpinski carpet */ |
||
Line 4,988: | Line 4,988: | ||
end Carpet; |
end Carpet; |
||
end Sierpinski_carpet; |
end Sierpinski_carpet; |
||
</syntaxhighlight> |
|||
</lang> |
|||
The above is a translation of the Fortran version. |
The above is a translation of the Fortran version. |
||
Output for n=3: |
Output for n=3: |
||
Line 5,022: | Line 5,022: | ||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
< |
<syntaxhighlight lang="postscript">%!PS-Adobe-3.0 |
||
%%BoundingBox 0 0 300 300 |
%%BoundingBox 0 0 300 300 |
||
Line 5,041: | Line 5,041: | ||
pop showpage |
pop showpage |
||
%%EOF</ |
%%EOF</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<h3>Text based solution</h3> |
<h3>Text based solution</h3> |
||
{{works with|PowerShell|2}} |
{{works with|PowerShell|2}} |
||
< |
<syntaxhighlight lang="powershell">function Draw-SierpinskiCarpet ( [int]$N ) |
||
{ |
{ |
||
$Carpet = @( '#' ) * [math]::Pow( 3, $N ) |
$Carpet = @( '#' ) * [math]::Pow( 3, $N ) |
||
Line 5,062: | Line 5,062: | ||
} |
} |
||
Draw-SierpinskiCarpet 3</ |
Draw-SierpinskiCarpet 3</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>########################### |
<pre>########################### |
||
Line 5,093: | Line 5,093: | ||
<h3>Graphics based solution</h3> |
<h3>Graphics based solution</h3> |
||
{{works with|PowerShell|3}} |
{{works with|PowerShell|3}} |
||
< |
<syntaxhighlight lang="powershell">Function Draw-SierpinskiCarpet ( [int]$N ) |
||
{ |
{ |
||
# Define form |
# Define form |
||
Line 5,146: | Line 5,146: | ||
} |
} |
||
Draw-SierpinskiCarpet 4</ |
Draw-SierpinskiCarpet 4</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
{{incomplete|powershell|Upload of files currently blocked. Needs output screenshot once file uploading is again allowed.}} |
{{incomplete|powershell|Upload of files currently blocked. Needs output screenshot once file uploading is again allowed.}} |
||
=={{header|Processing}}== |
=={{header|Processing}}== |
||
< |
<syntaxhighlight lang="java">float delta; |
||
void setup() { |
void setup() { |
||
Line 5,177: | Line 5,177: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight>'''The sketch can be run online''' :<BR> [https://www.openprocessing.org/sketch/953411 here.] |
||
Line 5,183: | Line 5,183: | ||
==={{header|Processing Python mode}}=== |
==={{header|Processing Python mode}}=== |
||
{{trans|Processing}} |
{{trans|Processing}} |
||
< |
<syntaxhighlight lang="python"> |
||
def setup(): |
def setup(): |
||
size(729, 729) |
size(729, 729) |
||
Line 5,202: | Line 5,202: | ||
rect(xx + delta, yy + delta, delta, delta) |
rect(xx + delta, yy + delta, delta, delta) |
||
rectangles(xx + s / 3, yy + s / 3, s / 3) |
rectangles(xx + s / 3, yy + s / 3, s / 3) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
This program produces an image file in SVG format. |
This program produces an image file in SVG format. |
||
< |
<syntaxhighlight lang="prolog">main:- |
||
write_sierpinski_carpet('sierpinski_carpet.svg', 486, 4). |
write_sierpinski_carpet('sierpinski_carpet.svg', 486, 4). |
||
Line 5,246: | Line 5,246: | ||
format(Stream, |
format(Stream, |
||
"<rect fill='black' x='~g' y='~g' width='~g' height='~g'/>\n", |
"<rect fill='black' x='~g' y='~g' width='~g' height='~g'/>\n", |
||
[X, Y, Side, Side]).</ |
[X, Y, Side, Side]).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,253: | Line 5,253: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="purebasic">Procedure in_carpet(x,y) |
||
While x>0 And y>0 |
While x>0 And y>0 |
||
If x%3=1 And y%3=1 |
If x%3=1 And y%3=1 |
||
Line 5,275: | Line 5,275: | ||
PrintN("") |
PrintN("") |
||
Next |
Next |
||
EndProcedure</ |
EndProcedure</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
This inserts a space after every character; but this makes the spacing look better anyway. |
This inserts a space after every character; but this makes the spacing look better anyway. |
||
< |
<syntaxhighlight lang="python">def in_carpet(x, y): |
||
while True: |
while True: |
||
if x == 0 or y == 0: |
if x == 0 or y == 0: |
||
Line 5,296: | Line 5,296: | ||
else: |
else: |
||
print ' ', |
print ' ', |
||
print</ |
print</syntaxhighlight> |
||
This version is elegant: |
This version is elegant: |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="python">def sierpinski_carpet(n): |
||
carpet = ["#"] |
carpet = ["#"] |
||
for i in xrange(n): |
for i in xrange(n): |
||
Line 5,307: | Line 5,307: | ||
return "\n".join(carpet) |
return "\n".join(carpet) |
||
print sierpinski_carpet(3)</ |
print sierpinski_carpet(3)</syntaxhighlight> |
||
Line 5,313: | Line 5,313: | ||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Iterations of the Sierpinski carpet''' |
||
from itertools import chain, islice |
from itertools import chain, islice |
||
Line 5,456: | Line 5,456: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Iterations of the Sierpinski carpet: |
<pre>Iterations of the Sierpinski carpet: |
||
Line 5,507: | Line 5,507: | ||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
< |
<syntaxhighlight lang="qb64">_Title "Sierpinski Carpet" |
||
Screen _NewImage(500, 545, 8) |
Screen _NewImage(500, 545, 8) |
||
Line 5,547: | Line 5,547: | ||
Call carpet(newX - side, newY, side, iorder) |
Call carpet(newX - side, newY, side, iorder) |
||
End If |
End If |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
Line 5,554: | Line 5,554: | ||
{{trans|Forth}} |
{{trans|Forth}} |
||
< |
<syntaxhighlight lang="quackery"> [ over 3 mod 1 = ] is 1? ( n1 n2 --> n1 n2 f ) |
||
[ 3 / swap ] is 3/ ( n1 n2 --> n2/3 n1 ) |
[ 3 / swap ] is 3/ ( n1 n2 --> n2/3 n1 ) |
||
Line 5,575: | Line 5,575: | ||
drop ] is carpet ( n --> ) |
drop ] is carpet ( n --> ) |
||
4 carpet</ |
4 carpet</syntaxhighlight> |
||
{{out}}Shown at half size. |
{{out}}Shown at half size. |
||
Line 5,667: | Line 5,667: | ||
{{Works with|R|3.3.3 and above}} |
{{Works with|R|3.3.3 and above}} |
||
[[File:SierpCRo5.png|200px|right|thumb|Output SierpCRo5.png]] |
[[File:SierpCRo5.png|200px|right|thumb|Output SierpCRo5.png]] |
||
<syntaxhighlight lang="r"> |
|||
<lang r> |
|||
## Are x,y inside Sierpinski carpet (and where)? (1-yes, 0-no) |
## Are x,y inside Sierpinski carpet (and where)? (1-yes, 0-no) |
||
inSC <- function(x, y) { |
inSC <- function(x, y) { |
||
Line 5,693: | Line 5,693: | ||
## Executing: |
## Executing: |
||
pSierpinskiC(5); |
pSierpinskiC(5); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 5,706: | Line 5,706: | ||
{{Works with|R|3.3.3 and above}} |
{{Works with|R|3.3.3 and above}} |
||
[[File:SierpCR2o5.png|200px|right|thumb|Output SierpCR2o5.png]] |
[[File:SierpCR2o5.png|200px|right|thumb|Output SierpCR2o5.png]] |
||
<syntaxhighlight lang="r"> |
|||
<lang r> |
|||
## Plotting Sierpinski carpet fractal v.2. aev 4/2/17 |
## Plotting Sierpinski carpet fractal v.2. aev 4/2/17 |
||
## ord - order, fn - file name, ttl - plot title, clr - color |
## ord - order, fn - file name, ttl - plot title, clr - color |
||
Line 5,724: | Line 5,724: | ||
## Executing: |
## Executing: |
||
pSierpinskiC2(5); |
pSierpinskiC2(5); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 5,735: | Line 5,735: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
(define (carpet n) |
(define (carpet n) |
||
Line 5,746: | Line 5,746: | ||
(map (λ(x) (~a x x x)) prev))))) |
(map (λ(x) (~a x x x)) prev))))) |
||
(for-each displayln (carpet 3)) |
(for-each displayln (carpet 3)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub carpet |
||
{ |
{ |
||
(['#'], -> @c { |
(['#'], -> @c { |
||
Line 5,777: | Line 5,777: | ||
say @carpet[3]; |
say @carpet[3]; |
||
# Output of both versions matches task example.</ |
# Output of both versions matches task example.</syntaxhighlight> |
||
=={{header|Relation}}== |
=={{header|Relation}}== |
||
Used _ instead of spaces beause wikitext compacts subsequents spaces |
Used _ instead of spaces beause wikitext compacts subsequents spaces |
||
<syntaxhighlight lang="relation"> |
|||
<lang Relation> |
|||
function incarpet(x,y) |
function incarpet(x,y) |
||
set a = x |
set a = x |
||
Line 5,817: | Line 5,817: | ||
run carpet(3) |
run carpet(3) |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Line 5,850: | Line 5,850: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program draws any order Sierpinski carpet (order 20 would be ≈ 3.4Gx3.4G carpet).*/ |
||
parse arg N char . /*get the order of the carpet. */ |
parse arg N char . /*get the order of the carpet. */ |
||
if N=='' | N=="," then N= 3 /*if none specified, then assume 3. */ |
if N=='' | N=="," then N= 3 /*if none specified, then assume 3. */ |
||
Line 5,875: | Line 5,875: | ||
if length(z)<width then say z /*display the line if it fits on screen*/ |
if length(z)<width then say z /*display the line if it fits on screen*/ |
||
call lineout 'Sierpinski.'N, z /*also, write the line to a (disk) file*/ |
call lineout 'Sierpinski.'N, z /*also, write the line to a (disk) file*/ |
||
end /*j*/ /*stick a fork in it, we're all done. */</ |
end /*j*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
This REXX program makes use of '''linesize''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console). |
This REXX program makes use of '''linesize''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console). |
||
Line 5,924: | Line 5,924: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "guilib.ring" |
load "guilib.ring" |
||
Line 5,981: | Line 5,981: | ||
end |
end |
||
return true} |
return true} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
[[File:CalmoSoftCarpet.jpg]] |
[[File:CalmoSoftCarpet.jpg]] |
||
Line 5,987: | Line 5,987: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Tcl}} |
{{trans|Tcl}} |
||
< |
<syntaxhighlight lang="ruby">def sierpinski_carpet(n) |
||
carpet = ["#"] |
carpet = ["#"] |
||
n.times do |
n.times do |
||
Line 5,997: | Line 5,997: | ||
end |
end |
||
4.times{|i| puts "\nN=#{i}", sierpinski_carpet(i)}</ |
4.times{|i| puts "\nN=#{i}", sierpinski_carpet(i)}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,052: | Line 6,052: | ||
{{libheader|RubyGems}} |
{{libheader|RubyGems}} |
||
{{libheader|JRubyArt}} |
{{libheader|JRubyArt}} |
||
< |
<syntaxhighlight lang="ruby"> |
||
attr_reader :limit |
attr_reader :limit |
||
Line 6,083: | Line 6,083: | ||
size(729, 729) |
size(729, 729) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="rust">fn main() { |
||
for i in 0..4 { |
for i in 0..4 { |
||
println!("\nN={}", i); |
println!("\nN={}", i); |
||
Line 6,111: | Line 6,111: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="scala">def nextCarpet(carpet: List[String]): List[String] = ( |
||
carpet.map(x => x + x + x) ::: |
carpet.map(x => x + x + x) ::: |
||
carpet.map(x => x + x.replace('#', ' ') + x) ::: |
carpet.map(x => x + x.replace('#', ' ') + x) ::: |
||
carpet.map(x => x + x + x)) |
carpet.map(x => x + x + x)) |
||
def sierpinskiCarpets(n: Int) = (Iterator.iterate(List("#"))(nextCarpet) drop n next) foreach println</ |
def sierpinskiCarpets(n: Int) = (Iterator.iterate(List("#"))(nextCarpet) drop n next) foreach println</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">(define (carpet n) |
||
(define (in-carpet? x y) |
(define (in-carpet? x y) |
||
(cond ((or (zero? x) (zero? y)) |
(cond ((or (zero? x) (zero? y)) |
||
Line 6,137: | Line 6,137: | ||
#\* |
#\* |
||
#\space))) |
#\space))) |
||
(newline)))</ |
(newline)))</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func boolean: inCarpet (in var integer: x, in var integer: y) is func |
const func boolean: inCarpet (in var integer: x, in var integer: y) is func |
||
Line 6,176: | Line 6,176: | ||
begin |
begin |
||
carpet(3); |
carpet(3); |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var c = ['##'] |
||
3.times { |
3.times { |
||
c = (c.map{|x| x * 3 } + |
c = (c.map{|x| x * 3 } + |
||
Line 6,185: | Line 6,185: | ||
c.map{|x| x * 3 }) |
c.map{|x| x * 3 }) |
||
} |
} |
||
say c.join("\n")</ |
say c.join("\n")</syntaxhighlight> |
||
=={{header|Sinclair ZX81 BASIC}}== |
=={{header|Sinclair ZX81 BASIC}}== |
||
{{trans|BBC BASIC}} |
{{trans|BBC BASIC}} |
||
Works with the unexpanded (1k RAM) ZX81. A screenshot of the output is [http://www.edmundgriffiths.com/zx81sierpcarpet.jpg here]. |
Works with the unexpanded (1k RAM) ZX81. A screenshot of the output is [http://www.edmundgriffiths.com/zx81sierpcarpet.jpg here]. |
||
< |
<syntaxhighlight lang="basic"> 10 LET O=3 |
||
20 LET S=3**O |
20 LET S=3**O |
||
30 FOR I=0 TO S-1 |
30 FOR I=0 TO S-1 |
||
Line 6,207: | Line 6,207: | ||
160 IF X>0 OR Y>0 THEN GOTO 130 |
160 IF X>0 OR Y>0 THEN GOTO 130 |
||
170 LET C=1 |
170 LET C=1 |
||
180 RETURN</ |
180 RETURN</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
func sierpinski_carpet(n:Int) -> String { |
func sierpinski_carpet(n:Int) -> String { |
||
func middle(str:String) -> String { |
func middle(str:String) -> String { |
||
Line 6,227: | Line 6,227: | ||
} |
} |
||
println(sierpinski_carpet(3))</ |
println(sierpinski_carpet(3))</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.5 |
||
proc map {lambda list} { |
proc map {lambda list} { |
||
Line 6,251: | Line 6,251: | ||
} |
} |
||
puts [sierpinski_carpet 3]</ |
puts [sierpinski_carpet 3]</syntaxhighlight> |
||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
<lang>Input "Carpet order: ";n |
<syntaxhighlight lang="text">Input "Carpet order: ";n |
||
l = (3^n) - 1 |
l = (3^n) - 1 |
||
Line 6,282: | Line 6,282: | ||
Loop |
Loop |
||
Return</ |
Return</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 6,290: | Line 6,290: | ||
Note that this code inserts a space between characters; some versions of [http://en.wikipedia.org/wiki/Paste_(Unix) paste(1)] (notably the one that ships with OS X) won't allow an empty delimiter. If yours does, you can replace the <tt>-d ' '</tt> in the function body with <tt>-d </tt>' '<tt> </tt>for more compact output. |
Note that this code inserts a space between characters; some versions of [http://en.wikipedia.org/wiki/Paste_(Unix) paste(1)] (notably the one that ships with OS X) won't allow an empty delimiter. If yours does, you can replace the <tt>-d ' '</tt> in the function body with <tt>-d </tt>' '<tt> </tt>for more compact output. |
||
< |
<syntaxhighlight lang="bash">#!/bin/bash |
||
sierpinski_carpet() { |
sierpinski_carpet() { |
||
Line 6,300: | Line 6,300: | ||
done |
done |
||
echo "$carpet" |
echo "$carpet" |
||
}</ |
}</syntaxhighlight> |
||
Sample run: |
Sample run: |
||
Line 6,339: | Line 6,339: | ||
{{works with|Zsh}} |
{{works with|Zsh}} |
||
< |
<syntaxhighlight lang="sh">sierpinski_carpet() { |
||
typeset -i n=${1:-3} |
typeset -i n=${1:-3} |
||
if (( n < 1 )); then |
if (( n < 1 )); then |
||
Line 6,360: | Line 6,360: | ||
done |
done |
||
} |
} |
||
sierpinski_carpet 3</ |
sierpinski_carpet 3</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 6,394: | Line 6,394: | ||
The carpet function works for any natural number n and is tested on 0,1,2, and 3. |
The carpet function works for any natural number n and is tested on 0,1,2, and 3. |
||
The carpet is stored as a list of lists of booleans but converted to characters for display. |
The carpet is stored as a list of lists of booleans but converted to characters for display. |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
#import nat |
#import nat |
||
Line 6,401: | Line 6,401: | ||
#show+ |
#show+ |
||
test = mat0 ~&?(`#!,` !)*** carpet* <0,1,2,3></ |
test = mat0 ~&?(`#!,` !)*** carpet* <0,1,2,3></syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:30ex;overflow:scroll;"> |
<pre style="height:30ex;overflow:scroll;"> |
||
Line 6,450: | Line 6,450: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang="vb">Const Order = 4 |
||
Function InCarpet(ByVal x As Integer, ByVal y As Integer) |
Function InCarpet(ByVal x As Integer, ByVal y As Integer) |
||
Line 6,472: | Line 6,472: | ||
Debug.Print |
Debug.Print |
||
Next i |
Next i |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>################################################################################# |
<pre>################################################################################# |
||
# ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## # |
# ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## # |
||
Line 6,556: | Line 6,556: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vbscript">Function InCarpet(i,j) |
||
If i > 0 And j > 0 Then |
If i > 0 And j > 0 Then |
||
Do While i > 0 And j > 0 |
Do While i > 0 And j > 0 |
||
Line 6,587: | Line 6,587: | ||
End Function |
End Function |
||
Carpet(WScript.Arguments(0))</ |
Carpet(WScript.Arguments(0))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,621: | Line 6,621: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ecmascript">var inCarpet = Fn.new { |x, y| |
||
while (true) { |
while (true) { |
||
if (x == 0 || y == 0) return true |
if (x == 0 || y == 0) return true |
||
Line 6,640: | Line 6,640: | ||
} |
} |
||
carpet.call(3)</ |
carpet.call(3)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 6,676: | Line 6,676: | ||
Uses magic number division to avoid repeatedly using the div instruction in a loop. |
Uses magic number division to avoid repeatedly using the div instruction in a loop. |
||
< |
<syntaxhighlight lang="asm">;x86-64 assembly code for Microsoft Windows |
||
;Tested in windows 7 Enterprise Service Pack 1 64 bit |
;Tested in windows 7 Enterprise Service Pack 1 64 bit |
||
;With the AMD FX(tm)-6300 processor |
;With the AMD FX(tm)-6300 processor |
||
Line 6,835: | Line 6,835: | ||
mov rcx,1 |
mov rcx,1 |
||
call exit</ |
call exit</syntaxhighlight> |
||
{{out|Sample}} |
{{out|Sample}} |
||
Line 6,895: | Line 6,895: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
[[File:CarpetXPL0.gif|right]] |
[[File:CarpetXPL0.gif|right]] |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
proc DrawPat(X0, Y0, S); \Draw 3x3 pattern with hole in middle |
proc DrawPat(X0, Y0, S); \Draw 3x3 pattern with hole in middle |
||
Line 6,913: | Line 6,913: | ||
if ChIn(1) then []; \wait for keystroke |
if ChIn(1) then []; \wait for keystroke |
||
SetVid($3); \restore normal text mode |
SetVid($3); \restore normal text mode |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">sub sp$(n) |
||
local i, s$ |
local i, s$ |
||
Line 6,964: | Line 6,964: | ||
end sub |
end sub |
||
print replace$(sierpinskiCarpet$(3), ",", "\n")</ |
print replace$(sierpinskiCarpet$(3), ",", "\n")</syntaxhighlight> |
||
=={{header|Z80 Assembly}}== |
=={{header|Z80 Assembly}}== |
||
8-bit BCT (Binary-Coded Ternary) values are used so maximum N is 4, sjasmplus syntax, CP/M executable: |
8-bit BCT (Binary-Coded Ternary) values are used so maximum N is 4, sjasmplus syntax, CP/M executable: |
||
< |
<syntaxhighlight lang="z80">; Sierpinski carpet in Z80 assembly (for CP/M OS - you can use `tnylpo` or `z88dk-ticks` on PC) |
||
OPT --syntax=abf : OUTPUT "sierpinc.com" ; asm syntax for z00m's variant of sjasmplus |
OPT --syntax=abf : OUTPUT "sierpinc.com" ; asm syntax for z00m's variant of sjasmplus |
||
ORG $100 |
ORG $100 |
||
Line 7,043: | Line 7,043: | ||
add a,c ; revert digits 3,2,1 back to 2,1,0 (0 -> 0) |
add a,c ; revert digits 3,2,1 back to 2,1,0 (0 -> 0) |
||
ret |
ret |
||
*/</ |
*/</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 7,081: | Line 7,081: | ||
{{trans|XPL0}} |
{{trans|XPL0}} |
||
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
Uses the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl |
||
< |
<syntaxhighlight lang="zkl">fcn drawPat(x0,y0,s,img){ // Draw 3x3 pattern with hole in middle |
||
foreach y,x in (3,3){ |
foreach y,x in (3,3){ |
||
if(x.isEven or y.isEven){ // don't draw middle pattern |
if(x.isEven or y.isEven){ // don't draw middle pattern |
||
Line 7,088: | Line 7,088: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">img:=PPM(800,800); |
||
drawPat(0,0,(3).pow(5),img); |
drawPat(0,0,(3).pow(5),img); |
||
img.write(File("foo.ppm","wb"));</ |
img.write(File("foo.ppm","wb"));</syntaxhighlight> |
||
[[Category:Geometry]] |
[[Category:Geometry]] |