|
http://mathworld.wolfram.com/CantorFunction.html
!'悪魔の階段
SET WINDOW 0,1,0,1
DRAW GRID(.1,.1)
LET H=1/128
FOR X=0 TO 1-H STEP H !' 0<=X<1
LET A$=TERNARY$(X)
IF POS(A$,"1")>0 THEN
FOR I=1 TO LEN(A$)
IF A$(I:I)="1" THEN
FOR J=I+1 TO LEN(A$)
IF A$(J:J)="1" OR A$(J:J)="2" THEN LET A$(J:J)="0"
NEXT J
EXIT FOR
END IF
NEXT I
END IF
IF POS(A$,"2")>0 THEN
FOR I=1 TO LEN(A$)
IF A$(I:I)="2" THEN LET A$(I:I)="1"
NEXT I
END IF
PLOT LINES: X,BINARY(A$);
NEXT X
PLOT LINES:1,1
END
EXTERNAL FUNCTION TERNARY$(X) !'10進小数 to 3進小数 (0<=X<1)
!'LET B$="0."
FOR I=1 TO 10
LET A$=MID$("012",MOD(INT(X*3),3)+1,1)
LET B$=B$&A$
LET X=X*3-INT(X*3)
NEXT I
LET TERNARY$=B$
END FUNCTION
EXTERNAL FUNCTION BINARY(X$) !'2進小数 to 10進小数
FOR I=1 TO LEN(X$)
IF X$(I:I)="1" THEN
LET S=S+2^(-I)
END IF
NEXT I
LET BINARY=S
END FUNCTION
-----------------------------------------------------------------------------------------
SET WINDOW 0,1,0,1
DRAW GRID(.1,.1)
FOR N=0 TO 3
SET LINE COLOR N+1
FOR X=0 TO 1 STEP 1/32
PLOT LINES: X,CANTOR(N,X);
NEXT X
PLOT LINES
NEXT N
END
EXTERNAL FUNCTION CANTOR(N,X) !'カントル関数
IF N=0 THEN
LET CANTOR=X
ELSE
IF X>=0 AND X<1/3 THEN
LET CANTOR=1/2*CANTOR(N-1,3*X)
ELSEIF X>=1/3 AND X<2/3 THEN
LET CANTOR=1/2
ELSEIF X>=2/3 AND X<=1 THEN
LET CANTOR=1/2+1/2*CANTOR(N-1,3*X-2)
END IF
END IF
END FUNCTION
|
|