Sierpinski carpet: Difference between revisions

Added Fōrmulæ entry
(Added Fōrmulæ entry)
 
(13 intermediate revisions by 7 users not shown)
Line 323:
###########################</pre>
=={{header|AppleScript}}==
===Functional===
 
{{Trans|JavaScript}}
(ES5 Functional version)
Line 696:
█ ██ ██ █
█████████</pre>
----
===Simple===
<syntaxhighlight lang="applescript">on SierpinskiCarpet(n, char)
if (n < 0) then return {}
script o
property lst1 : {char}
property lst2 : missing value
end script
set gap to space
repeat with k from 0 to (n - 1)
copy o's lst1 to o's lst2
repeat with i from 1 to (3 ^ k)
set str to o's lst1's item i
set o's lst1's item i to str & str & str
set o's lst2's item i to str & gap & str
end repeat
set o's lst1 to o's lst1 & o's lst2 & o's lst1
set gap to gap & gap & gap
end repeat
return join(o's lst1, linefeed)
end SierpinskiCarpet
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
return SierpinskiCarpet(3, "#")</syntaxhighlight>
 
{{output}}
<pre>###########################
# ## ## ## ## ## ## ## ## #
###########################
### ###### ###### ###
# # # ## # # ## # # #
### ###### ###### ###
###########################
# ## ## ## ## ## ## ## ## #
###########################
######### #########
# ## ## # # ## ## #
######### #########
### ### ### ###
# # # # # # # #
### ### ### ###
######### #########
# ## ## # # ## ## #
######### #########
###########################
# ## ## ## ## ## ## ## ## #
###########################
### ###### ###### ###
# # # ## # # ## # # #
### ###### ###### ###
###########################
# ## ## ## ## ## ## ## ## #
###########################</pre>
 
=={{header|Arturo}}==
Line 1,105 ⟶ 1,167:
Sleep
</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Sierpins.bas"
110 LET O=3
120 LET SZ=3^O
130 FOR Y=0 TO SZ-1
140 FOR X=0 TO SZ-1
150 LET CH$=CHR$(159)
160 LET X1=X:LET Y1=Y
170 DO UNTIL X1=0
180 IF MOD(X1,3)=1 AND MOD(Y1,3)=1 THEN LET CH$=" ":EXIT DO
190 LET X1=INT(X1/3):LET Y1=INT(Y1/3)
200 LOOP
210 PRINT CH$;
220 NEXT
230 PRINT
240 NEXT </syntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">NoMainWin
WindowWidth = 508
Line 1,152 ⟶ 1,232:
End If
End Sub</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{trans|BBC BASIC}}
Adapted to text mode. In some systems the screen scrolls for an order greater than 2.
{{works with|BASICA}}
{{works with|Commodore BASIC|3.5}}
{{works with|IS-BASIC}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic" >
10 REM Sierpinski carpet
20 REM R - order; S - size.
30 LET R = 3
40 LET S = 3^R
50 FOR I = 0 TO S-1
60 FOR J = 0 TO S-1
70 LET X = J
80 LET Y = I
90 GOSUB 500
100 IF C = 1 THEN 130
110 PRINT " ";
120 GOTO 140
130 PRINT "*";
140 NEXT J
150 PRINT
160 NEXT I
170 END
 
490 REM Is (X,Y) in the carpet? Returns C = 0 (no) or C = 1 (yes).
500 LET C = 0
510 X3 = INT(X/3)
520 Y3 = INT(Y/3)
530 REM If (X mod 3 = 1) and (Y mod 3 = 1) then return
540 IF (X-X3*3)*(Y-Y3*3) = 1 THEN 600
550 LET X = X3
560 LET Y = Y3
570 IF X > 0 THEN 510
580 IF Y > 0 THEN 510
590 LET C = 1
600 RETURN
</syntaxhighlight>
 
==={{header|Nascom BASIC}}===
Line 1,273 ⟶ 1,393:
40 LET S = 1
50 FOR P = 1 TO R
60 LET S = R3 * S
70 NEXT P
80 REM Now S (size) is 3 to the power of R (order)
Line 1,318 ⟶ 1,438:
170 LET C=1
180 RETURN</syntaxhighlight>
 
==={{header|Tiny BASIC}}===
{{trans|Minimal BASIC}}
In some systems the screen scrolls for an order greater than 2.
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">
10 REM SIERPINSKI CARPET
20 REM R - ORDER; S - SIZE.
30 LET R=3
40 LET S=1
50 LET P=1
60 IF P>R THEN GOTO 100
70 LET S=3*S
80 LET P=P+1
90 GOTO 60
100 REM NOW S IS 3 TO THE POWER OF R
110 LET I=0
120 LET J=0
130 LET X=J
140 LET Y=I
150 GOSUB 500
160 IF C=1 THEN GOTO 190
170 PRINT " ";
180 GOTO 200
190 PRINT "*";
200 LET J=J+1
210 IF J=S THEN GOTO 230
220 GOTO 130
230 PRINT
240 LET I=I+1
250 IF I=S THEN GOTO 270
260 GOTO 120
270 END
 
490 REM IS (X,Y) IN THE CARPET? RETURNS C = 0 (NO) OR C = 1 (YES).
500 LET C=0
510 W=X/3
520 Z=Y/3
530 IF X-W*3=1 IF Y-Z*3=1 THEN RETURN
540 LET X=W
550 LET Y=Z
560 IF X>0 THEN GOTO 510
570 IF Y>0 THEN GOTO 510
580 LET C=1
590 RETURN
</syntaxhighlight>
{{out}}
<pre>
***************************
* ** ** ** ** ** ** ** ** *
***************************
*** ****** ****** ***
* * * ** * * ** * * *
*** ****** ****** ***
***************************
* ** ** ** ** ** ** ** ** *
***************************
********* *********
* ** ** * * ** ** *
********* *********
*** *** *** ***
* * * * * * * *
*** *** *** ***
********* *********
* ** ** * * ** ** *
********* *********
***************************
* ** ** ** ** ** ** ** ** *
***************************
*** ****** ****** ***
* * * ** * * ** * * *
*** ****** ****** ***
***************************
* ** ** ** ** ** ** ** ** *
***************************
</pre>
 
==={{header|uBasic/4tH}}===
Line 2,378 ⟶ 2,574:
}
}</syntaxhighlight>
 
=={{header|EasyLang}}==
 
[https://easylang.dev/ide/#cod=dY+xDsIwDER3f8XtEcEQlQ3+JZhCEYVUKSDo1xOHIDGklgffnf2iDDEIxMcBL7wxTrCwBOAanm2yFmotsU7Zb9Q0tnJXOU6qzkedd2DbqEzVYfvddsUoL2RGQXXVqO6ayoGZZ5kqy8yz6pj/XUuW9l4upxgetwOYmaRvfSQJfYjYOEf5qmHtFXP+/Ac= Run it]
 
<syntaxhighlight lang="easylang">
proc carp x y sz . .
move x - sz / 2 y - sz / 2
rect sz sz
if sz > 0.5
h = sz / 3
carp x - sz y - sz h
carp x - sz y h
carp x - sz y + sz h
carp x + sz y - sz h
carp x + sz y h
carp x + sz y + sz h
carp x y - sz h
carp x y + sz h
.
.
background 000
clear
color 633
carp 50 50 100 / 3
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 3,100 ⟶ 3,322:
end subroutine Carpet
end program Sierpinski_carpet</syntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/L-system}}
 
'''Solution'''
 
It can be done using an [[wp:L-system|L-system]]. There are generic functions written in Fōrmulæ to compute an L-system in the page [[L-system#Fōrmulæ | L-system]].
 
The program that creates a Sierpiński carpet is:
 
[[File:Fōrmulæ - L-system - Sierpiński carpet 01.png]]
 
[[File:Fōrmulæ - L-system - Sierpiński carpet 02.png]]
 
=={{header|Gnuplot}}==
Line 4,400 ⟶ 4,636:
}
}</syntaxhighlight>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="Scheme">
 
{def sierpinsky
 
{def sierpinsky.r
{lambda {:n :w}
{if {= :n 0}
then :w
else {sierpinsky.r
{- :n 1}
{S.map {lambda {:x} :x:x:x} :w}
{S.map {lambda {:x} :x{S.replace ■ by o in :x}:x} :w}
{S.map {lambda {:x} :x:x:x} :w} }}}}
 
{lambda {:n}
{h2 n=:n}{S.replace o by space in
{S.replace \s by {div} in
{sierpinsky.r :n ■}}}}}
-> sierpinsky
 
{S.map sierpinsky 0 1 2 3}
->
 
S0
S1
■■■
■ ■
■■■
S2
■■■■■■■■■
■ ■■ ■■ ■
■■■■■■■■■
■■■ ■■■
■ ■ ■ ■
■■■ ■■■
■■■■■■■■■
■ ■■ ■■ ■
■■■■■■■■■
S3
■■■■■■■■■■■■■■■■■■■■■■■■■■■
■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■ ■■■■■■ ■■■■■■ ■■■
■ ■ ■ ■■ ■ ■ ■■ ■ ■ ■
■■■ ■■■■■■ ■■■■■■ ■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■
■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■■■■■■■ ■■■■■■■■■
■ ■■ ■■ ■ ■ ■■ ■■ ■
■■■■■■■■■ ■■■■■■■■■
■■■ ■■■ ■■■ ■■■
■ ■ ■ ■ ■ ■ ■ ■
■■■ ■■■ ■■■ ■■■
■■■■■■■■■ ■■■■■■■■■
■ ■■ ■■ ■ ■ ■■ ■■ ■
■■■■■■■■■ ■■■■■■■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■
■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■
■■■ ■■■■■■ ■■■■■■ ■■■
■ ■ ■ ■■ ■ ■ ■■ ■ ■ ■
■■■ ■■■■■■ ■■■■■■ ■■■
■■■■■■■■■■■■■■■■■■■■■■■■■■■
■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■■ ■
■■■■■■■■■■■■■■■■■■■■■■■■■■■
 
</syntaxhighlight>
 
=={{header|Lua}}==
Line 6,696 ⟶ 7,003:
# ## ## ## ## ## ## ## ## #
###########################
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Kotlin}}
<syntaxhighlight lang="Zig">
import math
 
fn main() {
carpet(3)
}
 
fn carpet(n int) {
power := int(math.pow(3.0, n))
for i in 0..power {
for j in 0..power {
if in_carpet(i, j) == true {print("*")} else{print(" ")}
}
println('')
}
}
 
fn in_carpet(x int, y int) bool {
mut xx := x
mut yy := y
for xx != 0 && yy != 0 {
if xx % 3 == 1 && yy % 3 == 1 {return false}
xx /= 3
yy /= 3
}
return true
}
</syntaxhighlight>
 
{{out}}
<pre>
***************************
* ** ** ** ** ** ** ** ** *
***************************
*** ****** ****** ***
* * * ** * * ** * * *
*** ****** ****** ***
***************************
* ** ** ** ** ** ** ** ** *
***************************
********* *********
* ** ** * * ** ** *
********* *********
*** *** *** ***
* * * * * * * *
*** *** *** ***
********* *********
* ** ** * * ** ** *
********* *********
***************************
* ** ** ** ** ** ** ** ** *
***************************
*** ****** ****** ***
* * * ** * * ** * * *
*** ****** ****** ***
***************************
* ** ** ** ** ** ** ** ** *
***************************
</pre>
 
=={{header|Wren}}==
{{trans|Python}}
<syntaxhighlight lang="ecmascriptwren">var inCarpet = Fn.new { |x, y|
while (true) {
if (x == 0 || y == 0) return true
2,120

edits