RosettaCodeData/Task/Square-but-not-cube/Dc/square-but-not-cube.dc
2023-07-01 13:44:08 -04:00

66 lines
1.4 KiB
Text

[n = # of non-cube squares found; we stop when it hits 30]sz
[b = # found that are both squares and cubes]sz
0 d sn sb
[c = current cube, s = current square,
r = current cube root, q = current square root,
f = "first" flag, to control comma delimiting]sz
1 d sc d ss d sq d sr sf
[M = main loop]sz
[
lq d * d ss [square q into s]sz
lc r >I [if s > c then call Increment]sz
lc ls <F [if s < c then s is a non-cube square; call Found]sz
lc ls =R [if s = c then s is a cubic square; call Remember]sz
lq 1 + sq [increment q]sz
ln 30 >M [loop if n is still < 30]sz
]sM
[I = Increment. Bump r and c=r^3 until c >= s]sz
[
lr 1 + d sr
d d * * d sc
ls >I
]sI
[C = Comma. Print a comma and a space]sz
[
44P 32P
]sC
[F = Found. Print s and increment n]sz
[
ln 1 + sn
lf 0 =C 0 sf [print ", " if f is not set; clear f]sz
ls n
]sF
[R = Remember. Save s in array l for later.]sz
[
lb d ls r :l
1 + sb
]sR
[B = print Both. Print out the values saved in array l.]sz
[
lf 0 =C 0 sf [print ", " if f is not set; clear f]sz
li d ;l n
1 + d si
lb r <B
]sB
[Print label and newline]sz
[Squares but not cubes:]n 10P
[Run main loop]sz
lMx
[Print two more newlines]sz
10 d P P
[Print second label and newline]sz
[Both squares and cubes:]n 10P
[initialize i to 0, set f again, and call B to print out the values in l]sz
0 si 1 sf lBx 10P