Square but not cube: Difference between revisions

Content added Content deleted
(→‎{{header|dc}}: Add implementation.)
Line 941: Line 941:
1024
1024
1089</pre>
1089</pre>

=={{header|dc}}==
{{trans|BASIC}}
<lang dc>
[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</lang>

{{Out}}
<pre>Squares but not cubes:
4, 9, 16, 25, 36, 49, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089

Both squares and cubes:
1, 64, 729</pre>


=={{header|Delphi}}==
=={{header|Delphi}}==