Cantor set: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
(20 intermediate revisions by 9 users not shown)
Line 205:
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|Amazing Hopper}}==
{{Trans|C}}
VERSION 1:
<syntaxhighlight lang="c">
#include <basico.h>
 
#define WIDTH 81
#define HEIGHT 5
 
#proto cantor(_X_,_Y_,_Z_)
 
algoritmo
 
decimales '0'
dimensionar(HEIGHT,WIDTH) matriz rellena("#",líneas)
 
_cantor(1, WIDTH, 2)
fijar separador 'NULO', imprimir( líneas, NL)
 
terminar
 
subrutinas
 
cantor(inicio, largo, índice)
seg=0
#( seg:=(int(largo/3))), no es cero?, entonces{
#basic{
líneas[índice:HEIGHT, (inicio+seg):((inicio+seg*2)-1)] = " ")
cantor( inicio, seg, índice+1 ) )
cantor( (inicio+(seg*2)), seg, índice+1 ) )
}
}
retornar
</syntaxhighlight>
{{out}}
<pre>
#################################################################################
########################### ###########################
######### ######### ######### #########
### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # #
</pre>
{{Trans|Ruby}}
VERSION 2:
<syntaxhighlight lang="c">
#include <basico.h>
 
#define HEIGHT 5
 
algoritmo
 
decimales '0'
cantor="", j=0
iterar
i=0
cadenas 's,v'
iterar grupo ( ++i, #(i< (3^j)),\
#( v = occurs("1", dectobase(i,3)) ? " " : "#"; )\
#( s = s $ replicate(v, 3^(HEIGHT-j-1) )) )
#(cantor = cantor $ s $ NL)
mientras ' #(j<=HEIGHT); ++j '
imprimir(cantor)
 
terminar
</syntaxhighlight>
{{out}}
<pre>
#################################################################################
########################### ###########################
######### ######### ######### #########
### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|AppleScript}}==
 
Line 449 ⟶ 525:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|BASIC}}==
{{works with|QBasic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="gwbasic">10 DEFINT A-Z
20 N = 4
Line 469 ⟶ 549:
### ### ### ###
# # # # # # # #</pre>
 
=={{header|BASIC256}}==
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256vb">global ancho, alto, intervalo
global ancho, alto, intervalo
ancho = 81 : alto = 5
dim intervalo(alto, ancho)
Line 504 ⟶ 584:
print
next i
end</syntaxhighlight>
End
 
</syntaxhighlight>
==={{header|Chipmunk Basic}}===
{{out}}
{{trans|FreeBASIC}}
<pre>
{{works with|Chipmunk Basic|3.6.4}}
Igual que la entrada de FreeBASIC.
<syntaxhighlight lang="qbasic">100 cls
</pre>
110 ancho = 81
120 alto = 5
130 dim intervalo$(alto,ancho)
140 '
150 sub cantor()
160 for i = 0 to alto-1
170 for j = 0 to ancho-1
180 intervalo$(i,j) = chr$(254)
190 next j
200 next i
210 end sub
220 '
230 sub conjcantor(inicio,longitud,indice)
240 segmento = longitud/3
250 if segmento = 0 then exit sub
260 for i = indice to alto-1
270 for j = inicio+segmento to inicio+segmento*2-1
280 intervalo$(i,j) = chr$(32)
290 next j
300 next i
310 conjcantor(inicio,segmento,indice+1)
320 conjcantor(inicio+segmento*2,segmento,indice+1)
330 end sub
340 '
350 cantor()
360 conjcantor(0,ancho,1)
370 for i = 0 to alto-1
380 for j = 0 to ancho-1
390 print intervalo$(i,j);
400 next j
410 print
420 next i
430 end</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#BASIC|BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">SUB Cantor
FOR i = 0 TO alto - 1
FOR j = 0 TO ancho - 1
intervalo$(i, j) = CHR$(254) '"#"
NEXT j
NEXT i
END SUB
 
SUB ConjCantor (inicio, longitud, indice)
segmento = INT(longitud / 3)
IF segmento = 0 THEN EXIT SUB
FOR i = indice TO alto - 1
FOR j = inicio + segmento TO inicio + segmento * 2 - 1
intervalo$(i, j) = CHR$(32) '" "
NEXT j
NEXT i
CALL ConjCantor(inicio, segmento, indice + 1)
CALL ConjCantor(inicio + segmento * 2, segmento, indice + 1)
END SUB
 
CONST ancho = 81
CONST alto = 5
DIM SHARED intervalo$(alto, ancho)
 
CLS
CALL Cantor
CALL ConjCantor(0, ancho, 1)
FOR i = 0 TO alto - 1
FOR j = 0 TO ancho - 1
PRINT intervalo$(i, j);
NEXT j
PRINT
NEXT i
END</syntaxhighlight>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">LET ancho = 81
LET alto = 5
DIM intervalo$(0,0)
MAT REDIM intervalo$(0 TO alto, 0 TO ancho)
 
SUB cantor
FOR i = 0 TO alto-1
FOR j = 0 TO ancho-1
LET intervalo$(i, j) = "#" !CHR$(254)
NEXT j
NEXT i
END SUB
 
SUB conjcantor (inicio,longitud,indice)
LET segmento = INT(longitud/3)
IF segmento = 0 THEN EXIT SUB
FOR i = indice TO alto-1
FOR j = inicio+segmento TO inicio+segmento*2-1
LET intervalo$(i, j) = CHR$(32) !" "
NEXT j
NEXT i
CALL conjcantor (inicio, segmento, indice+1)
CALL conjcantor (inicio+segmento*2, segmento, indice+1)
END SUB
 
CALL cantor
CALL conjcantor (0, ancho, 1)
FOR i = 0 TO alto-1
FOR j = 0 TO ancho-1
PRINT intervalo$(i, j);
NEXT j
PRINT
NEXT i
END</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vb">ancho = 81
alto = 5
dim intervalo$(alto, ancho)
 
Cantor()
ConjCantor(0, ancho, 1)
for i = 0 to alto - 1
for j = 0 to ancho - 1
print intervalo$(i, j);
next j
print
next i
end
 
sub Cantor()
for i = 0 to alto - 1
for j = 0 to ancho - 1
intervalo$(i, j) = chr$(254) //"#"
next j
next i
end sub
 
sub ConjCantor(inicio, longitud, indice)
segmento = longitud / 3
if segmento = 0 return
for i = indice to alto - 1
for j = inicio + segmento to inicio + segmento * 2 - 1
intervalo$(i, j) = " "
next j
next i
ConjCantor(inicio, segmento, indice + 1)
ConjCantor(inicio + segmento * 2, segmento, indice + 1)
end sub</syntaxhighlight>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Cantor ← {" •" ⊏˜ >⥊¨(¯1⊸⊏⊢¨¨⊢)1‿0‿1∧⌜⍟(↕𝕩)1}</syntaxhighlight>
Line 857 ⟶ 1,092:
{{out}}
Same result of Java
 
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=S87PyS9SMDU15Sooyk9WSE7MKwHyKxQqFYqrFPQU9LgUFBQy00AcOwUDPUMQFwiKq4wVbEGC+grGUKHc/LJUsD5dkCxUsCg1uQSkDCGCZAFYITYpbQUjBS2wJJoiPS49LqgqAwULAwVDAwMuAA== Run it]
 
<syntaxhighlight>
color 555
proc cantor x y sz . .
if sz > 0.1
sz3 = sz / 3
move x y - sz3
rect sz sz3
cantor x y - sz3 sz3
cantor x + 2 * sz3 y - sz3 sz3
.
.
cantor 0 80 100
</syntaxhighlight>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
defmodule Cantor do
@pos "█"
@neg " "
 
def run(lines) do
Enum.map(0..lines, fn line ->
segment_size = 3 ** (lines - line - 1)
chars = (3 ** line)
 
Enum.map(0..chars, fn char ->
char
|> Integer.digits(3)
|> Enum.any?(fn x -> x === 1 end)
|> case do
true -> @neg
false -> @pos
end
end)
|> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end)
|> Enum.join()
|> String.trim_trailing()
end)
|> Enum.filter(fn line -> line !== "" end)
end
 
def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)]
def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc
end
 
Cantor.run(5) |> IO.inspect()
</syntaxhighlight>
{{out}}
<pre>["█████████████████████████████████████████████████████████████████████████████████",
"███████████████████████████ ███████████████████████████",
"█████████ █████████ █████████ █████████",
"███ ███ ███ ███ ███ ███ ███ ███",
"█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █"]</pre>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
int WIDTH = 81
int HEIGHT = 5
List lines = text[].with(HEIGHT) # a list with HEIGHT empty texts
for each int i in range(0, HEIGHT) do lines[i] = text("█", WIDTH) end
fun cantor = void by int start, int len, int index
int seg = len / 3
if seg == 0 do return end
for int i = index; i < HEIGHT; i++
for int j = start + seg; j < start + seg * 2; j++
lines[i][j] = " "
end
end
cantor(start, seg, index + 1)
cantor(start + seg * 2, seg, index + 1)
end
cantor(0, WIDTH, 1)
for each text line in lines do writeLine(line) end
</syntaxhighlight>
{{out}}
<pre>
█████████████████████████████████████████████████████████████████████████████████
███████████████████████████ ███████████████████████████
█████████ █████████ █████████ █████████
███ ███ ███ ███ ███ ███ ███ ███
█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █
</pre>
 
=={{header|Excel}}==
===LAMBDA===
Line 1,451 ⟶ 1,774:
[[File:FutureBasic Cantor Set.png]]
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/The_Cantor_set}}
 
'''Solution'''
 
Strictly speaking, a visualization of the perfect Cantor set is impossible, it consists of a infinite set of isolated points in the interval [0, 1]. The most we can do is drawing the early steps of its construction.
 
Several ways of visualization of the Cantor set are shown.
 
'''Preliminaries'''
 
Let us start with a definition of the points that define the central points of the intervals of the Cantos set on any step:
 
[[File:Fōrmulæ - Cantor set 01.png]]
 
[[File:Fōrmulæ - Cantor set 02.png]]
 
[[File:Fōrmulæ - Cantor set 03.png]]
 
The followign function defines the segments on each step:
 
[[File:Fōrmulæ - Cantor set 04.png]]
 
[[File:Fōrmulæ - Cantor set 05.png]]
 
[[File:Fōrmulæ - Cantor set 06.png]]
 
=== Plotting the central points ===
 
[[File:Fōrmulæ - Cantor set 07.png]]
 
[[File:Fōrmulæ - Cantor set 08.png]]
 
=== Common method ===
 
[[File:Fōrmulæ - Cantor set 09.png]]
 
[[File:Fōrmulæ - Cantor set 10.png]]
 
[[File:Fōrmulæ - Cantor set 11.png]]
 
=== Cantor dust ===
 
[[File:Fōrmulæ - Cantor set 12.png]]
 
[[File:Fōrmulæ - Cantor set 13.png]]
 
[[File:Fōrmulæ - Cantor set 14.png]]
 
=== [https://en.wikipedia.org/wiki/Knaster%E2%80%93Kuratowski_fan Knaster–Kuratowski fan or "Cantor teepee"] ===
 
[[File:Fōrmulæ - Cantor set 15.png]]
 
[[File:Fōrmulæ - Cantor set 16.png]]
 
[[File:Fōrmulæ - Cantor set 17.png]]
 
 
The following forms of visualization are taken from [https://en.wikipedia.org/wiki/Point-set_topology point-set topology]. They are not intended as forms of visualization of the Cantor set, instead, these structures make use of the Cantor set.
 
=== [https://en.wikipedia.org/wiki/Indecomposable_continuum Brouwer–Janiszewski–Knaster continuum, or "the bucket handle"] ===
 
[[File:Fōrmulæ - Cantor set 18.png]]
 
[[File:Fōrmulæ - Cantor set 19.png]]
 
[[File:Fōrmulæ - Cantor set 20.png]]
 
=== "Double Knaster" ===
 
[[File:Fōrmulæ - Cantor set 21.png]]
 
[[File:Fōrmulæ - Cantor set 22.png]]
 
[[File:Fōrmulæ - Cantor set 23.png]]
 
=={{header|Go}}==
Line 2,548 ⟶ 2,946:
{{out}}
A graphic of a Cantor set is shown
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
cantorSet = function(start, length, depth)
if depth == 0 then return [[start, start+length - 1]]
newLen = length / 3
leftInterval = cantorSet(start, newLen, depth - 1)
rightInterval = cantorSet(start + 2 * newLen, newLen, depth - 1)
return leftInterval + rightInterval
end function
 
for depth in range(0, 4)
output =[" "] * 81
segments = cantorSet(1, 81,depth)
for segment in segments
for x in range(segment[0] - 1, segment[1]-1)
output[x] = "#"
end for
end for
print output.join("")
end for
</syntaxhighlight>
{{out}}
<pre>
#################################################################################
########################### ###########################
######### ######### ######### #########
### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # #
</pre>
 
=={{header|Modula-2}}==
{{trans|Kotlin}}
Line 2,821 ⟶ 3,253:
 
=== Press any key to exit ===</pre>
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* DRAW A CANTOR SET USING ASCII */
 
/* BDOS SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( F, A ); DECLARE F BYTE, A ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
 
/* DRAW A CANTOR SET */
 
DECLARE LINES LITERALLY '4';
DECLARE WIDTH LITERALLY '27'; /* MUST BE 3**(LINES-1) */
 
DECLARE LINE (WIDTH)BYTE;
DECLARE ( I, L, C, W, S, SEGMENTS ) BYTE;
 
DO I = 0 TO LAST( LINE );
LINE( I ) = 023H;
CALL PR$CHAR( LINE( I ) );
END;
CALL PR$NL;
W = WIDTH;
SEGMENTS = 1;
DO L = 2 TO LINES;
W = W / 3;
SEGMENTS = SEGMENTS * 3;
C = 0;
DO S = 1 TO SEGMENTS;
DO I = 1 TO W;
IF NOT S THEN LINE( C ) = ' '; /* EVEN SEGMENT - BLANK IT */
CALL PR$CHAR( LINE( C ) );
C = C + 1;
END;
END;
CALL PR$NL;
END;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
###########################
######### #########
### ### ### ###
# # # # # # # #
</pre>
 
=={{header|Processing}}==
<syntaxhighlight lang="java">
Line 2,839 ⟶ 3,320:
}
</syntaxhighlight>
 
=={{header|Python}}==
===Imperative===
Line 3,744 ⟶ 4,226:
### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # #</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">const (
width = 81
height = 5
)
fn cantor(mut lines [][]u8, start int, len int, index int) {
seg := len / 3
if seg == 0 {
return
}
for i in index.. height {
for j in start + seg..start + 2 * seg {
lines[i][j] = ' '[0]
}
}
cantor(mut lines, start, seg, index + 1)
cantor(mut lines, start + seg * 2, seg, index + 1)
}
fn main() {
mut lines := [][]u8{len:height, init: []u8{len:width, init:'*'[0]}}
cantor(mut lines, 0, width, 1)
for line in lines {
println(line.bytestr())
}
}</syntaxhighlight>
 
{{out}}
<pre>
*********************************************************************************
*************************** ***************************
********* ********* ********* *********
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
Line 3,792 ⟶ 4,312:
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *</pre>
=={{header|Vlang}}==
{{trans|Go}}
<syntaxhighlight lang="vlang">const (
width = 81
height = 5
)
fn cantor(mut lines [][]u8, start int, len int, index int) {
seg := len / 3
if seg == 0 {
return
}
for i in index.. height {
for j in start + seg..start + 2 * seg {
lines[i][j] = ' '[0]
}
}
cantor(mut lines, start, seg, index + 1)
cantor(mut lines, start + seg * 2, seg, index + 1)
}
fn main() {
mut lines := [][]u8{len:height, init: []u8{len:width, init:'*'[0]}}
cantor(mut lines, 0, width, 1)
for line in lines {
println(line.bytestr())
}
}</syntaxhighlight>
 
=={{header|VTL-2}}==
<syntaxhighlight lang="vtl2">
1010 L=4
1020 I=1
1030 X=1
1040 I=I+1
1050 X=X*3
1060 #=I<L*1040
2010 I=0
2020 :I)=35
2030 $=:I)
2040 I=I+1
2050 #=I<X*2020
2060 ?=""
2070 W=X
2080 G=1
2090 W=W/3
2100 G=G*3
2110 S=1
2120 C=0
2130 I=1
2140 #=S/2*0+%=1*2160
2150 :C)=32
2160 $=:C)
2170 C=C+1
2180 I=I+1
2190 #=W>I*2140
2200 S=S+1
2210 #=C<X*2130
2220 ?=""
2230 L=L-1
2240 #=1<L*2090
</syntaxhighlight>
{{out}}
<pre>
###########################
*********************************************************************************
######### #########
*************************** ***************************
### ### ### ###
********* ********* ********* *********
***# # *** # # *** # ***# # *** *** *** ***#
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var width = 81
var height = 5
 
Line 3,859 ⟶ 4,384:
* * * * * * * * * * * * * * * *
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">proc Cantor(N, LineSeg, Len); \Delete middle third of LineSeg
9,482

edits