Square but not cube: Difference between revisions

Add MACRO-11
m ((Dialects of BASIC moved to the BASIC section.))
(Add MACRO-11)
 
(9 intermediate revisions by 5 users not shown)
Line 23:
n++</syntaxhighlight>
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="asm"> org 100h
mvi b,30 ; Counter
push b
loop: lhld curcub ; DE = current cube
xchg
lhld cursqr ; HL = current square
call cdehl
jc advcub ; DE < HL, next cube
jz advsqr ; DE = HL, both square and cube
call prhl ; HL = square but not cube, print it
pop b ; Get counter
dcr b ; Decrease counter
rz ; Stop when zero reached
push b ; Push counter back
advsqr: call nexsqr ; Next square
jmp loop
advcub: call nexcub ; Next cube
jmp loop
 
; compare DE to HL
cdehl: mov a,d
cmp h
rnz
mov a,e
cmp l
ret
 
; Get next square (starting with 1)
nexsqr: lhld sqdiff ; DE = current difference
xchg
lhld cursqr ; HL = current square
dad d ; Add difference to square
inx d ; Increment difference twice
inx d
shld cursqr ; Update current square
xchg
shld sqdiff ; Update current difference
ret
cursqr: dw 0 ; Current square
sqdiff: dw 1 ; Difference to next squre
 
; Get next cube (starting with 1)
nexcub: lhld csumst ; DE = start of current sum
xchg
lxi h,0 ; HL = current cube
lda csumn ; A = amount of numbers to sum
csumlp: dad d ; Add to current cube
inx d ; Next odd number
inx d
dcr a ; Until done summing
jnz csumlp
shld curcub ; Store next sum
xchg
shld csumst ; Store start of next sum
lxi h,csumn ; Increment sum counter
inr m
ret
curcub: dw 0 ; Current cube
csumst: dw 1 ; Start of current sum
csumn: db 1 ; Amount of numbers to sum
 
; Print HL as a decimal value
prhl: push h ; Store registers
push d
push b
lxi b,pnum ; Store pointer to buffer on stack
push b
lxi b,-10 ; Divide by 10 using trial subtraction
prdgt: lxi d,-1 ; D =
prdgtl: inx d
dad b
jc prdgtl
mvi a,'0'+10 ; ASCII digit + 10
add l ; L = remainder - 10
pop h ; Get pointer to buffer
dcx h ; Go back one digit
mov m,a ; Store digit
push h ; Store pointer to buffer
xchg ; HL = n/10
mov a,h ; Zero?
ora l
jnz prdgt ; If not, get more digits
mvi c,9 ; 9 = CP/M print string
pop d ; DE = buffer
call 5
pop b ; Restore registers
pop d
pop h
ret
db '*****' ; Number placeholder
pnum: db 13,10,'$'</syntaxhighlight>
{{out}}
<pre>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</pre>
=={{header|8086 Assembly}}==
<syntaxhighlight lang="asm"> cpu 8086
Line 75 ⟶ 198:
 
<pre>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 </pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">PUT 1 IN square.root
PUT 1 IN cube.root
PUT 30 IN amount
 
WHILE amount > 0:
WHILE square.root ** 2 > cube.root ** 3:
PUT cube.root + 1 IN cube.root
IF square.root ** 2 <> cube.root ** 3:
WRITE square.root ** 2/
PUT amount - 1 IN amount
PUT square.root + 1 IN square.root</syntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|Action!}}==
Line 485 ⟶ 652:
<pre> 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</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="freebasic">cont = 0 : n = 2
do
if is_pow(n, 2) and not is_pow(n, 3) then
print n; " ";
cont += 1
end if
n += 1
until cont = 30
print
 
cont = 0 : n = 2
do
if is_pow(n, 2) and is_pow(n, 3) then
print n; " ";
cont += 1
end if
n += 1
until cont = 3
end
 
function is_pow(n, q)
#tests if the number n is the q'th power of some other integer
r = int(n^(1.0/q))
for i = r-1 to r+1 #there might be a bit of floating point nonsense, so test adjacent numbers also
if i^q = n then return true
next i
return false
end function</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
==={{header|Commodore BASIC}}===
Line 684 ⟶ 883:
29 1024
30 1089</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="freebasic">// Rosetta Code problem: https://rosettacode.org/wiki/Square_but_not_cube
// by Jjuanhdez, 10/2022
 
count = 0 : n = 2
repeat
if isPow(n, 2) and not isPow(n, 3) then
print n, " ";
count = count + 1
fi
n = n + 1
until count = 30
print
 
count = 0 : n = 2
repeat
if isPow(n, 2) and isPow(n, 3) then
print n, " ";
count = count + 1
fi
n = n + 1
until count = 3
print
end
 
sub isPow(n, q)
//tests if the number n is the q'th power of some other integer
r = int(n^(1.0/q))
for i = r-1 to r+1 //there might be a bit of floating point nonsense, so test adjacent numbers also
if i^q = n return true
next i
return false
end sub</syntaxhighlight>
{{out}}
<pre>Same as FreeBASIC entry.</pre>
 
=={{header|BCPL}}==
Line 1,314 ⟶ 1,549:
{$IFNDEF UNIX} readln; {$ENDIF}
end.</syntaxhighlight>
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc main() void:
word sqrt, cbrt, sq, cb, seen;
sqrt := 1;
cbrt := 1;
seen := 0;
while seen < 30 do
sq := sqrt * sqrt;
while
cb := cbrt * cbrt * cbrt;
sq > cb
do
cbrt := cbrt + 1
od;
if sq /= cb then
seen := seen + 1;
write(sq:5);
if seen % 10 = 0 then writeln() fi
fi;
sqrt := sqrt + 1
od
corp</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func iscube x .
for i = 1 to x
h = i * i * i
if h = x
return 1
elif h > x
return 0
.
.
.
while cnt < 30
sq += 1
sq2 = sq * sq
if iscube sq2 = 0
write sq2 & " "
cnt += 1
.
.
</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|F_Sharp|F#}}==
Line 2,065 ⟶ 2,354:
676
729 is square and cube
784
841
900
961
1024
1089</pre>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="asm"> .TITLE SQRCUB
.MCALL .TTYOUT,.EXIT
 
SQRCUB::MOV #^D30,R5
JSR PC,NEXCUB
1$: JSR PC,NEXSQR
2$: CMP R4,R3
BLT 3$
BEQ 1$
MOV R3,R0
JSR PC,PR0
SOB R5,1$
.EXIT
3$: JSR PC,NEXCUB
BR 2$
 
; PUT SUCCESSIVE SQUARES IN R3
NEXSQR: MOV 1$,R3
ADD 2$,R3
MOV R3,1$
ADD #2,2$
RTS PC
1$: .WORD 0
2$: .WORD 1
 
; PUT SUCCESSIVE CUBES IN R4
NEXCUB: CLR R4
MOV 3$,R1
1$: ADD 2$,R4
ADD #2,2$
SOB R1,1$
INC 3$
RTS PC
2$: .WORD 1
3$: .WORD 1
 
; PRINT R0 AS DECIMAL WITH NEWLINE
PR0: MOV #4$,R2
1$: MOV #-1,R1
2$: INC R1
SUB #12,R0
BCC 2$
ADD #72,R0
MOVB R0,-(R2)
MOV R1,R0
BNE 1$
3$: MOVB (R2)+,R0
.TTYOUT
BNE 3$
RTS PC
.BLKB 5
4$: .BYTE 15,12,0
.END SQRCUB</syntaxhighlight>
{{out}}
<pre>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
Line 2,138 ⟶ 2,513:
<pre>{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}
{1, 64, 729}</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map show squarenotcube))]
where squarenotcube = take 30 (squares $notin cubes)
 
squares :: [num]
squares = map (^ 2) [1..]
 
cubes :: [num]
cubes = map (^ 3) [1..]
 
|| Values in as not in bs, assuming as and bs are sorted
notin :: [num] -> [num] -> [num]
notin as [] = as
notin (a:as) (b:bs) = a:notin as (b:bs), if a < b
= notin as bs, if a = b
= notin (a:as) bs, if a > b</syntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|MiniScript}}==
Line 2,800 ⟶ 3,224:
1 64 729 4096 15625 46656 117649 262144 531441 1000000 1771561 2985984 4826809 7529536 11390625</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <SquareCube 30>>;
};
 
SquareCube {
s.Max = <SquareCube s.Max 1 1>;
0 s.Sqrt s.Cbrt = ;
 
s.Max s.Sqrt s.Cbrt,
<Square s.Sqrt>: s.Square,
<Cube s.Cbrt>: s.Cube,
<Compare s.Square s.Cube>: {
'-' = s.Square
<SquareCube <- s.Max 1> <+ 1 s.Sqrt> s.Cbrt>;
'0' = <SquareCube s.Max <+ 1 s.Sqrt> s.Cbrt>;
'+' = <SquareCube s.Max s.Sqrt <+ 1 s.Cbrt>>;
};
};
 
Square { s.N = <* s.N s.N>; };
Cube { s.N = <* s.N <* s.N s.N>>; };</syntaxhighlight>
{{out}}
<pre>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</pre>
=={{header|REXX}}==
Programming note: &nbsp; extra code was added to support an additional output format &nbsp; (see the 2<sup>nd</sup> '''output''' section).
Line 2,993 ⟶ 3,441:
</pre>
 
=={{header|RPL}}==
{{trans|11l}}
≪ → eq n
≪ { } 1
'''WHILE''' OVER SIZE n < '''REPEAT'''
DUP SQ DUP 3 INV ^ 1E-6 + FLOOR 3 ^
'''IF''' eq EVAL '''THEN''' SWAP OVER SQ + SWAP '''END'''
1 + '''END'''
DROP
≫ ≫ ''''TASK'''' STO
{{in}}
<pre>
≪ ≠ ≫ 30 TASK
≪ == ≫ 3 TASK
</pre>
{{out}}
<pre>
2: { 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 }
1: { 1 64 729 }
</pre>
=={{header|Ruby}}==
==== Class based ====
Line 3,397 ⟶ 3,865:
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/math" for Math
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var i = 1
Line 3,405 ⟶ 3,873:
while (sqnc.count < 30 || sqcb.count < 3) {
var sq = i * i
var cb = Mathsq.cbrt(sq).round
if (cb*cb*cb != sq) {
sqnc.add(sq)
2,096

edits