Square but not cube: Difference between revisions

Add MACRO-11
m (→‎{{header|Commodore BASIC}}: Fix inner loop goto target)
(Add MACRO-11)
 
(28 intermediate revisions by 11 users not shown)
Line 10:
{{trans|C}}
 
<langsyntaxhighlight lang="11l">V n = 1
V count = 0
 
Line 21:
E
print(sq‘ is square and cube’)
n++</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="asm"> cpu 8086
org 100h
section .text
Line 70 ⟶ 193:
section .data
db '*****' ; Placeholder for number
num: db ' $'</langsyntaxhighlight>
 
{{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|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!}}==
<syntaxhighlight lang="action!">BYTE FUNC IsCube(INT n)
INT i,c
 
i=1
DO
c=i*i*i
IF c=n THEN
RETURN (1)
FI
i==+1
UNTIL c>n
OD
RETURN (0)
 
PROC Main()
INT n,sq,count
 
PrintE("First 30 squares but not cubes:")
n=1 count=0
WHILE count<30
DO
sq=n*n
IF IsCube(sq)=0 THEN
PrintF("%I ",sq)
count==+1
FI
n==+1
OD
 
PutE() PutE()
PrintE("First 3 squares and cubes:")
n=1 count=0
WHILE count<3
DO
sq=n*n
IF IsCube(sq) THEN
PrintF("%I ",sq)
count==+1
FI
n==+1
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Square_but_not_cube.png Screenshot from Atari 8-bit computer]
<pre>
First 30 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
 
First 3 squares and cubes:
1 64 729
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Square_But_Not_Cube is
Line 111 ⟶ 331:
begin
Show (Limit => 30);
end Square_But_Not_Cube;</langsyntaxhighlight>
 
{{out}}
Line 119 ⟶ 339:
=={{header|ALGOL 68}}==
Avoids computing cube roots.
<langsyntaxhighlight lang="algol68">BEGIN
# list the first 30 numbers that are squares but not cubes and also #
# show the numbers that are both squares and cubes #
Line 141 ⟶ 361:
print( ( newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 180 ⟶ 400:
 
=={{header|ALGOL-M}}==
<langsyntaxhighlight lang="algolm">begin
integer function square(x);
integer x;
Line 203 ⟶ 423:
s := s + 1;
end;
end</langsyntaxhighlight>
 
{{out}}
Line 214 ⟶ 434:
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="apl">(×⍨∘⍳ ~ (⊢×⊢×⊢)∘⍳) 33</langsyntaxhighlight>
 
{{out}}
Line 221 ⟶ 441:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">on run
script listing
on |λ|(x)
Line 291 ⟶ 511:
set my text item delimiters to dlm
str
end unlines</langsyntaxhighlight>
{{Out}}
<pre>1 (also cube)
Line 328 ⟶ 548:
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">squares: map 1..100 => [&^2]
cubes: map 1..100 => [&^3]
 
Line 334 ⟶ 554:
print first.n:30 select squares => [not? in? & cubes]
print "Square and cube:"
print first.n:3 select squares => [in? & cubes]</langsyntaxhighlight>
 
{{out}}
Line 344 ⟶ 564:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">cube := [], counter:=0
while counter<30 {
cube[(n := A_Index)**3] := true
Line 352 ⟶ 572:
res .= "[" n**2 "] "
}
MsgBox % Trim(res, " ")</langsyntaxhighlight>
square and cube numbers are denoted by square brackets:
Outputs:<pre>
Line 358 ⟶ 578:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SQUARE_BUT_NOT_CUBE.AWK
BEGIN {
Line 381 ⟶ 601:
return(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 420 ⟶ 640:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DEFINT C,S,Q,R,N: C=1: S=1: Q=1: R=1: N=1
20 IF N>30 THEN END
30 S=Q*Q
Line 426 ⟶ 646:
50 IF S<C THEN N=N+1: PRINT S;
60 Q=Q+1
70 GOTO 20</langsyntaxhighlight>
 
{{out}}
Line 432 ⟶ 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}}===
{{trans|BASIC}}
<syntaxhighlight lang="basic">100 DIM SC(2): SC = 0: REM REMEMBER SQUARE CUBES
110 PRINT "SQUARES BUT NOT CUBES:"
120 N = 0: REM NUMBER OF NON-CUBE SQUARES FOUND
130 SR = 1: REM CURRENT SQUARE ROOT
140 CR = 1: CU = 1: REM CURRENT CUBE ROOT AND CUBE
150 REM BEGIN LOOP
160 : IF N >= 30 THEN 230
170 : SQ = SR * SR
180 : IF SQ > CU THEN CR = CR + 1: CU = CR*CR*CR: GOTO 180
190 : IF SQ = CU THEN SC(SC) = SQ: SC = SC + 1
200 : IF SQ < CU THEN N = N + 1:PRINT SQ,
210 : SR = SR + 1
220 GOTO 160: REM END LOOP
230 PRINT: PRINT
240 PRINT "BOTH SQUARES AND CUBES:"
250 FOR I=0 TO SC-1: PRINT SC(I),: NEXT I
260 PRINT</syntaxhighlight>
 
{{works with|Commodore BASIC|3.5,7.0}}
This version uses the later BASIC DO ... LOOP structure:
<pre>100 DIM SC(2): SC = 0: REM REMEMBER SQUARE CUBES
110 PRINT "SQUARES BUT NOT CUBES:"
120 N = 0: REM NUMBER OF NON-CUBE SQUARES FOUND
130 SR = 1: REM CURRENT SQUARE ROOT
140 CR = 1: CU = 1: REM CURRENT CUBE ROOT AND CUBE
150 DO WHILE N < 30
170 : SQ = SR * SR
180 : DO WHILE SQ > CU: CR = CR + 1: CU = CR*CR*CR: LOOP
190 : IF SQ = CU THEN SC(SC) = SQ: SC = SC + 1
200 : IF SQ < CU THEN N = N + 1:PRINT SQ,
210 : SR = SR + 1
220 LOOP
230 PRINT: PRINT
240 PRINT "BOTH SQUARES AND CUBES:"
250 FOR I=0 TO SC-1: PRINT SC(I),: NEXT I
260 PRINT</pre>
 
{{Out}}
<pre>
READY.
RUN
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
 
READY.
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">function is_pow(n as integer, q as integer) as boolean
'tests if the number n is the q'th power of some other integer
dim as integer r = int( n^(1.0/q) )
for i as integer = 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
 
dim as integer count = 0, n = 2
do
if is_pow( n, 2 ) and not is_pow( n, 3 ) then
print n;" ";
count += 1
end if
n += 1
loop until count = 30
print
count = 0
n = 2
do
if is_pow( n, 2 ) and is_pow( n, 3 ) then
print n;" ";
count += 1
end if
n += 1
loop until count = 3
print</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
64 729 4096</pre>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">100 PROGRAM "Square.bas"
110 LET SQNOTCB,SQANDCB,SQNUM,CBNUM,CBN,SQN,D1=0:LET SQD,D2=1
120 DO
130 LET SQN=SQN+1:LET SQNUM=SQNUM+SQD:LET SQD=SQD+2
140 IF SQNUM>CBNUM THEN
150 LET CBN=CBN+1:LET CBNUM=CBNUM+D2
160 LET D1=D1+6:LET D2=D2+D1
170 END IF
180 IF SQNUM<>CBNUM THEN
190 PRINT SQNUM:LET SQNOTCB=SQNOTCB+1
200 ELSE
210 PRINT SQNUM,SQN;"*";SQN;"=";CBN;"*";CBN;"*";CBN
220 LET SQANDCB=SQANDCB+1
230 END IF
240 LOOP UNTIL SQNOTCB>=30
250 PRINT SQANDCB;"where numbers are square and cube."</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">OpenConsole()
lv=1
Repeat
s+1 : s2=s*s : Flg=#True
For i=lv To s
If s2=i*i*i
tx3$+Space(Len(tx2$)-Len(tx3$))+Str(s2)
tx2$+Space(Len(Str(s2))+1)
Flg=#False : lv=i : c-1 : Break
EndIf
Next
If Flg : tx2$+Str(s2)+" " : EndIf
c+1
Until c>=30
PrintN("s² : "+tx2$) : PrintN("s²&s³: "+tx3$)
Input()</syntaxhighlight>
{{out}}
<pre>s² : 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
s²&s³: 1 64 729</pre>
 
==={{header|Visual Basic .NET}}===
Inspired by the '''F#''' version, but no longer resembles it. Removed the recursion, multiplying (like the '''D''' and '''Pascal''' versions, only addition is used to calculate squares and cubes), '''match''' (Select Case) statement, and hard-coded limit.
<syntaxhighlight lang="vbnet">Module Module1
 
' flag / mask explanation:
' bit 0 (1) = increment square
' bit 1 (2) = increment cube
' bit 2 (4) = has output
 
' Checks flag against mask, then advances mask.
Function ChkFlg(flag As Integer, ByRef mask As Integer) As Boolean
ChkFlg = (flag And mask) = mask : mask <<= 1
End Function
 
Sub SwoC(limit As Integer)
Dim count, square, delta, cube, d1, d2, flag, mask As Integer, s as string = ""
count = 1 : square = 1 : delta = 1 : cube = 1 : d1 = 1 : d2 = 0
While count <= limit
flag = {5, 7, 2}(1 + square.CompareTo(cube))
If flag = 7 Then s = String. Format(" {0} (also cube)", square)
If flag = 5 Then s = String.Format("{0,-2} {1}", count, square) : count += 1
mask = 1 : If ChkFlg(flag, mask) Then delta += 2 : square += delta
If ChkFlg(flag, mask) Then d2 += 6 : d1 += d2 : cube += d1
If ChkFlg(flag, mask) Then Console.WriteLine(s)
End While
End Sub
 
Sub Main()
SwoC(30)
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre> 1 (also cube)
1 4
2 9
3 16
4 25
5 36
6 49
64 (also cube)
7 81
8 100
9 121
10 144
11 169
12 196
13 225
14 256
15 289
16 324
17 361
18 400
19 441
20 484
21 529
22 576
23 625
24 676
729 (also cube)
25 784
26 841
27 900
28 961
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}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let square(x) = x * x
Line 450 ⟶ 937:
s := s + 1
$)
$)</langsyntaxhighlight>
{{out}}
<pre> 4 9 16 25 36
Line 458 ⟶ 945:
529 576 625 676 784
841 900 961 1024 1089</pre>
=={{header|BQN}}==
<syntaxhighlight lang="bqn">∘‿5⥊ ((⊢⋆3˙)((¬∊)/⊢)⊢⋆2˙) ↕34</syntaxhighlight>
{{out}}
<pre>┌─
╵ 4 9 25 36 49
64 100 121 144 169
196 225 256 289 324
361 400 441 484 529
576 625 676 729 784
841 900 961 1024 1089
┘</pre>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 476 ⟶ 975:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 484 ⟶ 983:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using static System.Console;
Line 524 ⟶ 1,023:
 
}
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 563 ⟶ 1,062:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
 
Line 584 ⟶ 1,083:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1 is square and cube
Line 622 ⟶ 1,121:
=={{header|Clojure}}==
{{trans|Raku}}
<langsyntaxhighlight lang="clojure">(def squares (map #(* % %) (drop 1 (range))))
(def square-cubes (map #(int (. Math pow % 6)) (drop 1 (range))))
 
Line 631 ⟶ 1,130:
(println "Both squares and cubes:")
(println (take 15 square-cubes))
</syntaxhighlight>
</lang>
{{Out}}
<pre>Squares but not cubes:
Line 637 ⟶ 1,136:
Both squares and cubes:
(1 64 729 4096 15625 46656 117649 262144 531441 1000000 1771561 2985984 4826809 7529536 11390625)</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">square_not_cube = iter () yields (int)
cube_root: int := 1
square_root: int := 1
while true do
while cube_root ** 3 < square_root ** 2 do
cube_root := cube_root + 1
end
if square_root ** 2 ~= cube_root ** 3 then
yield(square_root ** 2)
end
square_root := square_root + 1
end
end square_not_cube
 
start_up = proc ()
amount = 30
po: stream := stream$primary_output()
n: int := 0
for i: int in square_not_cube() do
stream$putright(po, int$unparse(i), 5)
n := n + 1
if n // 10 = 0 then stream$putl(po, "") end
if n = amount then break end
end
end start_up</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|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. SQUARE-NOT-CUBE.
Line 667 ⟶ 1,199:
ADD 1 TO SQ-ROOT.
IF SEEN IS LESS THAN 30 GO TO SQUARE-STEP.
STOP RUN.</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'> 4
Line 700 ⟶ 1,232:
1089</pre>
 
=={{header|Commodore BASICComal}}==
<syntaxhighlight lang="comal">0010 ZONE 5
{{trans|BASIC}}
0020 cube_n#:=0;square_n#:=0;seen#:=0
<lang basic>100 DIM SC(2): SC = 0: REM REMEMBER SQUARE CUBES
0030 WHILE seen#<30 DO
110 PRINT "SQUARES BUT NOT CUBES:"
0040 WHILE cube_n#^3<square_n#^2 DO cube_n#:+1
120 N = 0: REM NUMBER OF NON-CUBE SQUARES FOUND
0050 IF cube_n#^3<>square_n#^2 THEN
130 SR = 1: REM CURRENT SQUARE ROOT
0060 PRINT square_n#^2,
140 CR = 1: CU = 1: REM CURRENT CUBE ROOT AND CUBE
0070 seen#:+1
150 REM BEGIN LOOP
1600080 : IF Nseen# >=MOD 305=0 THEN 230PRINT
0090 ENDIF
170 : SQ = SR * SR
0100 square_n#:+1
180 : IF SQ > CU THEN CR = CR + 1: CU = CR*CR*CR: GOTO 180
0110 ENDWHILE
190 : IF SQ = CU THEN SC(SC) = SQ: SC = SC + 1
0120 END</syntaxhighlight>
200 : IF SQ < CU THEN N = N + 1:PRINT SQ,
{{out}}
210 : SR = SR + 1
<pre>4 9 16 25 36
220 GOTO 160: REM END LOOP
49 81 100 121 144
230 PRINT: PRINT
169 196 225 256 289
240 PRINT "BOTH SQUARES AND CUBES:"
324 361 400 441 484
250 FOR I=0 TO SC-1: PRINT SC(I),: NEXT I
529 576 625 676 784
260 PRINT</lang>
841 900 961 1024 1089</pre>
 
=={{header|Common Lisp}}==
{{works with|Commodore BASIC|7.0}}
<syntaxhighlight lang="lisp">(defun cubep (n)
This version uses BASIC 7's DO ... LOOP structure:
(loop for i from 1
<pre>100 DIM SC(2): SC = 0: REM REMEMBER SQUARE CUBES
for c = (* i i i)
110 PRINT "SQUARES BUT NOT CUBES:"
while (<= c n)
120 N = 0: REM NUMBER OF NON-CUBE SQUARES FOUND
when (= c n) do (return t)
130 SR = 1: REM CURRENT SQUARE ROOT
finally (return nil)))
140 CR = 1: CU = 1: REM CURRENT CUBE ROOT AND CUBE
150 DO WHILE N < 30
170 : SQ = SR * SR
180 : DO WHILE SQ > CU: CR = CR + 1: CU = CR*CR*CR: LOOP
190 : IF SQ = CU THEN SC(SC) = SQ: SC = SC + 1
200 : IF SQ < CU THEN N = N + 1:PRINT SQ,
210 : SR = SR + 1
220 LOOP
230 PRINT: PRINT
240 PRINT "BOTH SQUARES AND CUBES:"
250 FOR I=0 TO SC-1: PRINT SC(I),: NEXT I
260 PRINT</pre>
 
{{Out}}
<pre>
READY.
RUN
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
 
(defparameter squares (let ((n 0)) (lambda () (incf n) (* n n))))
READY.
</pre>
 
=={{header|Common Lisp}}==
<lang lisp>(defun cubep (n) (loop for i from 1 to (expt n (/ 1 3))
when (= (* i i i) n) do (return t)
finally (return nil)))
 
(destructuring-bind (not-cubesnoncubes cubes)
(loop for ns from= 1(funcall squares) then (funcall squares)
forwhile s = 1 then(< (*length nnoncubes) n30)
untilif (=cubep 30s) (lengthcollect s into not-cubes))
if (not (cubep s)) collect s into cubesnoncubes
iffinally (notreturn (cubeplist snoncubes cubes))) collect s into not-cubes
(format t "Squares but not cubes:~%~A~%~%" noncubes)
finally (return (list not-cubes cubes)))
(format t "SquaresBoth butsquares notand cubes:~%~aA~%~%" not-cubes))</syntaxhighlight>
(format t "~%Both squares and cubes:~%~a~%" cubes))</lang>
 
{{Out}}
Line 782 ⟶ 1,280:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
 
var cube: uint16 := 1;
Line 803 ⟶ 1,301:
nsqr := nsqr + 1;
end loop;
print_nl();</langsyntaxhighlight>
 
{{out}}
Line 811 ⟶ 1,309:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.range;
import std.stdio;
Line 906 ⟶ 1,404:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>1 (also cube)
Line 941 ⟶ 1,439:
1024
1089</pre>
 
=={{header|dc}}==
{{trans|BASIC}}
<syntaxhighlight 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</syntaxhighlight>
 
{{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}}==
Line 946 ⟶ 1,521:
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Square_but_not_cube;
 
Line 973 ⟶ 1,548:
 
{$IFNDEF UNIX} readln; {$ENDIF}
end.</langsyntaxhighlight>
 
=={{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#}}==
<langsyntaxhighlight lang="fsharp">
let rec fN n g φ=if φ<31 then match compare(n*n)(g*g*g) with | -1->printfn "%d"(n*n);fN(n+1) g (φ+1)
| 0->printfn "%d cube and square"(n*n);fN(n+1)(g+1)φ
| 1->fN n (g+1) φ
fN 1 1 1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,021 ⟶ 1,650:
=={{header|Factor}}==
{{trans|F#}}
<langsyntaxhighlight lang="factor">USING: combinators interpolate io kernel prettyprint math
math.functions math.order pair-rocket ;
IN: rosetta-code.square-but-not-cube
Line 1,034 ⟶ 1,663:
] when ;
 
1 1 1 fn 3drop</langsyntaxhighlight>
{{out}}
<pre>
Line 1,073 ⟶ 1,702:
 
=={{header|FALSE}}==
<langsyntaxhighlight lang="false">1 1 1
[2O30>~][
[$$*2O$$**>][\1+\]#
Line 1,080 ⟶ 1,709:
]#
%%%
10,</langsyntaxhighlight>
 
{{out}}
Line 1,087 ⟶ 1,716:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.10 S C=1;S S=1;S Q=1;S R=1;S N=1
01.20 I (N-30)1.3,1.3,1.8
01.30 S S=Q*Q
Line 1,094 ⟶ 1,723:
01.60 S N=N+1;T %4,S,!
01.70 S Q=Q+1;G 1.2
01.80 Q</langsyntaxhighlight>
 
{{out}}
Line 1,130 ⟶ 1,759:
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: square dup * ;
: cube dup dup * * ;
: 30-non-cube-squares
Line 1,146 ⟶ 1,775:
;
 
30-non-cube-squares cr bye</langsyntaxhighlight>
 
{{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|FreeBASIC}}==
<lang freebasic>function is_pow(n as integer, q as integer) as boolean
'tests if the number n is the q'th power of some other integer
dim as integer r = int( n^(1.0/q) )
for i as integer = 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
 
dim as integer count = 0, n = 2
do
if is_pow( n, 2 ) and not is_pow( n, 3 ) then
print n;" ";
count += 1
end if
n += 1
loop until count = 30
print
count = 0
n = 2
do
if is_pow( n, 2 ) and is_pow( n, 3 ) then
print n;" ";
count += 1
end if
n += 1
loop until count = 3
print</lang>
{{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
64 729 4096</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,205 ⟶ 1,800:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,245 ⟶ 1,840:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-}
 
import Control.Monad (join)
Line 1,270 ⟶ 1,865:
( ((," (also cube)") <$> take 3 both)
<> ((,"") <$> take 30 only)
)</langsyntaxhighlight>
 
Or simply
 
<langsyntaxhighlight lang="haskell">import Control.Monad (join)
 
------------------- SQUARE BUT NOT CUBE ------------------
Line 1,295 ⟶ 1,890:
| isCube x = " (also cube of " <> show (cubeRoot x) <> ")"
| otherwise = []
</syntaxhighlight>
</lang>
 
Or, if we prefer a finite series to an infinite one
<langsyntaxhighlight lang="haskell">isCube :: Int -> Bool
isCube =
(==)
Line 1,315 ⟶ 1,910:
label n
| isCube n = " (also cube)"
| otherwise = ""</langsyntaxhighlight>
{{Out}}
<pre>1 (also cube)
Line 1,350 ⟶ 1,945:
1024
1089</pre>
 
=={{header|IS-BASIC}}==
<lang IS-BASIC>100 PROGRAM "Square.bas"
110 LET SQNOTCB,SQANDCB,SQNUM,CBNUM,CBN,SQN,D1=0:LET SQD,D2=1
120 DO
130 LET SQN=SQN+1:LET SQNUM=SQNUM+SQD:LET SQD=SQD+2
140 IF SQNUM>CBNUM THEN
150 LET CBN=CBN+1:LET CBNUM=CBNUM+D2
160 LET D1=D1+6:LET D2=D2+D1
170 END IF
180 IF SQNUM<>CBNUM THEN
190 PRINT SQNUM:LET SQNOTCB=SQNOTCB+1
200 ELSE
210 PRINT SQNUM,SQN;"*";SQN;"=";CBN;"*";CBN;"*";CBN
220 LET SQANDCB=SQANDCB+1
230 END IF
240 LOOP UNTIL SQNOTCB>=30
250 PRINT SQANDCB;"where numbers are square and cube."</lang>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">isSqrNotCubeofInt=: (*. -.)/@(= <.)@(2 3 %:/ ])
getN_Indicies=: adverb def '[ ({. I.) [ (] , [: u (i.200) + #@])^:(> +/)^:_ u@]'</langsyntaxhighlight>
 
'''Example Use:'''
<langsyntaxhighlight lang="j"> I. isSqrNotCubeofInt i.1090 NB. If we know the upper limit required to get first 30
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
30 isSqrNotCubeofInt getN_Indicies 0 NB. otherwise iteratively build list until first 30 found
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</langsyntaxhighlight>
 
'''Alternative Solution:'''
 
Breaking up the solution above into smaller chunks with comments...
<langsyntaxhighlight lang="j">isInt=: = <. NB. are numbers integers?
sqrcube=: 2 3 %:/ ] NB. table of 2nd and 3rd roots of y
isSqrNotCubeofInt=: (*. -.)/@isInt@sqrcube NB. is y the square but not cube of an integer?
Line 1,393 ⟶ 1,970:
while=: conjunction def 'u^:v^:_' NB. repeat u while v is true
 
process_until_enough=: adverb def 'u process_more while notEnough u'</langsyntaxhighlight>
 
'''Example Use:'''
<langsyntaxhighlight lang="j"> 30 ([ getIdx isSqrNotCubeofInt process_until_enough) 0
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
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class SquaresCubes {
public static boolean isPerfectCube(long n) {
long c = (long)Math.cbrt((double)n);
Line 1,425 ⟶ 2,002:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,463 ⟶ 2,040:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,512 ⟶ 2,089:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>1 (cube of 1 and square of 1)
Line 1,551 ⟶ 2,128:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
<lang jq>
# Emit an unbounded stream
def squares_not_cubes:
Line 1,561 ⟶ 2,138:
limit(30; squares_not_cubes)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,577 ⟶ 2,154:
=={{header|Julia}}==
 
<syntaxhighlight lang="julia">
<lang Julia>
iscube(n) = n == round(Int, cbrt(n))^3
 
println(collect(Iterators.take((n^2 for n in 1:10^6 if !iscube(n^2)), 30)))
</syntaxhighlight>
</lang>
{{Out}}
[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]
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// Version 1.2.60
 
fun main(args: Array<String>) {
Line 1,603 ⟶ 2,180:
n++
}
}</langsyntaxhighlight>
 
{{output}}
Line 1,609 ⟶ 2,186:
Same as Ring example.
</pre>
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
# First 30 positive integers which are squares but not cubes
# also, the first 3 positive integers which are both squares and cubes
 
######
# main #
######
 
integer n sq cr cnt=0
 
for (( n=1; cnt<30; n++ )); do
(( sq = n * n ))
(( cr = cbrt(sq) ))
if (( (cr * cr * cr) != sq )); then
(( cnt++ ))
print ${sq}
else
print "${sq} is square and cube"
fi
done</syntaxhighlight>
{{out}}<pre>
1 is square and cube
4
9
16
25
36
49
64 is square and cube
81
100
121
144
169
196
225
256
289
324
361
400
441
484
529
576
625
676
729 is square and cube
784
841
900
961
1024
1089</pre>
 
=={{header|LOLCODE}}==
{{trans|Commodore BASIC}}
<langsyntaxhighlight lang="lolcode">HAI 1.2
 
I HAS A SkwareKyoobs ITZ A BUKKIT
Line 1,658 ⟶ 2,293:
VISIBLE ""
 
KTHXBYE</langsyntaxhighlight>
 
{{Out}}
Line 1,670 ⟶ 2,305:
=={{header|Lua}}==
Calculating cube roots with x^(1/3) caused problems with floating-point 'dust' so the Newton-Raphson method is used instead.
<langsyntaxhighlight Lualang="lua">function nthroot (x, n)
local r = 1
for i = 1, 16 do
Line 1,690 ⟶ 2,325:
count = count + 1
end
end</langsyntaxhighlight>
{{out}}
<pre>1 is square and cube
Line 1,719 ⟶ 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 1,727 ⟶ 2,448:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
CUBE=1
Line 1,749 ⟶ 2,470:
VECTOR VALUES FMT = $I4*$
END OF PROGRAM </langsyntaxhighlight>
 
{{out}}
Line 1,785 ⟶ 2,506:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">s = Range[50]^2;
c = Range[1, Ceiling[Surd[Max[s], 3]]]^3;
Take[Complement[s, c], 30]
Intersection[s, c]</langsyntaxhighlight>
{{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}
{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}}==
<langsyntaxhighlight MiniScriptlang="miniscript">squares = []
tris = []
both = []
Line 1,809 ⟶ 2,579:
print squares[:30]
print "Both square and cube:"
print both[:3]</langsyntaxhighlight>
{{out}}
<pre>Square but not cube:
Line 1,815 ⟶ 2,585:
Both square and cube:
[1, 64, 729]</pre>
 
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE SquareNotCube;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
 
CONST
Amount = 30;
VAR
CubeRoot, SquareRoot,
Cube, Square,
Seen: CARDINAL;
 
BEGIN
Seen := 0;
SquareRoot := 1;
CubeRoot := 1;
Square := 1;
Cube := 1;
REPEAT
SquareRoot := SquareRoot + 1;
Square := SquareRoot * SquareRoot;
WHILE Square > Cube DO
CubeRoot := CubeRoot + 1;
Cube := CubeRoot * CubeRoot * CubeRoot;
END;
IF Square # Cube THEN
Seen := Seen + 1;
WriteCard(Square, 4);
WriteLn();
END;
UNTIL Seen = Amount
END SquareNotCube.</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|Nim}}==
<langsyntaxhighlight Nimlang="nim">var count = 0
var n, c, c3 = 1
 
Line 1,830 ⟶ 2,664:
echo sq
inc count
inc n</langsyntaxhighlight>
<pre>
1 is square and cube
Line 1,869 ⟶ 2,703:
=={{header|OCaml}}==
{{trans|F#}}
<langsyntaxhighlight lang="ocaml">let rec fN n g phi =
if phi < 31 then
match compare (n*n) (g*g*g) with
Line 1,878 ⟶ 2,712:
;;
 
fN 1 1 1</langsyntaxhighlight>
 
=={{header|Pascal}}==
Only using addition :-)
<langsyntaxhighlight lang="pascal">program SquareButNotCube;
var
sqN,
Line 1,929 ⟶ 2,763:
until CountSqNotCb >= 30;//sqrt(High(NativeUint));
writeln(CountSqANDCb,' where numbers are square and cube ');
end.</langsyntaxhighlight>
{{out}}
<pre> 1 1*1 = 1*1*1
Line 1,973 ⟶ 2,807:
==== Hash ====
Use a hash to track state (and avoid floating-point math).
<langsyntaxhighlight lang="perl">while ($cnt < 30) {
$n++;
$h{$n**2}++;
Line 1,984 ⟶ 2,818:
 
print "\n\nFirst 3 positive integers that are both a square and a cube:\n";
print "$_ " for sort { $a <=> $b } grep { $h{$_} == 0 } keys %h;</langsyntaxhighlight>
{{out}}
<pre>First 30 positive integers that are a square but not a cube:
Line 1,998 ⟶ 2,832:
Output is the same as previous.
 
<langsyntaxhighlight lang="perl"># return an anonymous subroutine that generates stream of specified powers
sub gen_pow {
my $m = shift;
Line 2,027 ⟶ 2,861:
my $pow6 = gen_pow(6);
print "\n\nFirst 3 positive integers that are both a square and a cube:\n";
print $pow6->() . ' ' for 1..3;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #004080;">integer</span> <span style="color: #000000;">square</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">squared</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">cube</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cubed</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
Line 2,049 ⟶ 2,883:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThe first 15 positive integers that are both a square and a cube: \n"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">15</span><span style="color: #0000FF;">),</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,089 ⟶ 2,923:
{1,64,729,4096,15625,46656,117649,262144,531441,1000000,1771561,2985984,4826809,7529536,11390625}
</pre>
 
=={{header|PILOT}}==
<syntaxhighlight lang="pilot">C :sqr=1
C :cbr=1
C :sq=1
C :cb=1
C :n=0
 
*square
U (sq>cb):*cube
C (sq<cb):n=n+1
T (sq<cb):#sq
C :sqr=sqr+1
C :sq=sqr*#sqr
J (n<30):*square
E :
 
*cube
C :cbr=cbr+1
C :cb=(cbr*#cbr)*#cbr
J (sq>cb):*cube
E :</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|PL/I}}==
<langsyntaxhighlight lang="pli">squareNotCube: procedure options(main);
square: procedure(n) returns(fixed);
Line 2,116 ⟶ 3,003:
end;
end;
end squareNotCube;</langsyntaxhighlight>
{{out}}
<pre> 4 9 16 25 36 49 81 100 121 144
Line 2,123 ⟶ 3,010:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plm">100H: /* CP/M OUTPUT */
BDOS: PROCEDURE (FN, ARG);
DECLARE FN BYTE, ARG ADDRESS;
Line 2,166 ⟶ 3,053:
 
CALL BDOS(0,0);
EOF</langsyntaxhighlight>
{{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|PureBasic}}==
<lang PureBasic>OpenConsole()
lv=1
Repeat
s+1 : s2=s*s : Flg=#True
For i=lv To s
If s2=i*i*i
tx3$+Space(Len(tx2$)-Len(tx3$))+Str(s2)
tx2$+Space(Len(Str(s2))+1)
Flg=#False : lv=i : c-1 : Break
EndIf
Next
If Flg : tx2$+Str(s2)+" " : EndIf
c+1
Until c>=30
PrintN("s² : "+tx2$) : PrintN("s²&s³: "+tx3$)
Input()</lang>
{{out}}
<pre>s² : 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
s²&s³: 1 64 729</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python"># nonCubeSquares :: Int -> [(Int, Bool)]
def nonCubeSquares(n):
upto = enumFromTo(1)
Line 2,246 ⟶ 3,112:
 
 
main()</langsyntaxhighlight>
{{Out}}
<pre>(1^2 = 1 = 1^3)
Line 2,281 ⟶ 3,147:
32 -> 1024
33 -> 1089</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ swap - -1 1 clamp 1+ ] is <=> ( n n --> n )
 
[ dup * ] is squared ( n --> n )
 
[ dup squared * ] is cubed ( n --> n )
 
0 0 []
[ unrot
over squared
over cubed <=>
[ table
1+
[ 1+ dip 1+ ]
[ dip
[ tuck squared
join swap 1+ ] ] ]
do
rot dup size 30 = until ]
dip 2drop
echo</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|Racket}}==
Line 2,286 ⟶ 3,179:
Using a generator it _is_ possible to print the cubes in-line, but I've chosen to show reusing the generator / for / sequence interaction:
 
<langsyntaxhighlight lang="racket">#lang racket
(require racket/generator)
 
Line 2,306 ⟶ 3,199:
(for ((x (in-range 1 4))
((s^2 c) (sequence-filter (λ (s^2 c) c) (in-producer (make-^2-but-not-^3-generator)))))
(printf "~a: ~a is also ~a^3~%" x s^2 c))</langsyntaxhighlight>
 
{{out}}
Line 2,317 ⟶ 3,210:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @square-and-cube = map { .⁶ }, 1..Inf;
 
my @square-but-not-cube = (1..Inf).map({ .² }).grep({ $_ ∉ @square-and-cube[^@square-and-cube.first: * > $_, :k]});
Line 2,323 ⟶ 3,216:
put "First 30 positive integers that are a square but not a cube: \n", @square-but-not-cube[^30];
 
put "\nFirst 15 positive integers that are both a square and a cube: \n", @square-and-cube[^15];</langsyntaxhighlight>
{{out}}
<pre>First 30 positive integers that are a square but not a cube:
Line 2,331 ⟶ 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).
<langsyntaxhighlight lang="rexx">/*REXX pgm shows N ints>0 that are squares and not cubes, & which are squares and cubes.*/
numeric digits 20 /*ensure handling of larger numbers. */
parse arg N . /*obtain optional argument from the CL.*/
Line 2,360 ⟶ 3,277:
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,461 ⟶ 3,378:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Square but not cube
 
Line 2,486 ⟶ 3,403:
next
return 0
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,524 ⟶ 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 ====
<langsyntaxhighlight lang="ruby">#!/usr/bin/env ruby
 
class PowIt
Line 2,578 ⟶ 3,515:
 
puts "Square-and-cubes:"
puts hexponents.join(" ")</langsyntaxhighlight>
{{out}}
<pre>Squares:
Line 2,585 ⟶ 3,522:
1 64 729</pre>
==== Enumerator ====
<langsyntaxhighlight lang="ruby">squares = Enumerator.new {|y| 1.step{|n| y << n*n} }
 
puts "Square cubes: %p
Square non-cubes: %p" % squares.take(33).partition{|sq| Math.cbrt(sq).to_i ** 3 == sq }</langsyntaxhighlight>
{{out}}
<pre>Square cubes: [1, 64, 729]
Line 2,595 ⟶ 3,532:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn main() {
let mut s = 1;
let mut c = 1;
Line 2,614 ⟶ 3,551:
s += 1;
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,656 ⟶ 3,593:
This example uses Spire's SafeLongs for both removing any size limitation and making exponentiation/roots cleaner, at the expense of initializing lists with an iteration vs the simpler .from(n). Both the non-cube-squares and square-cubes are lazily evaluated lists, the former is constructed by making lists of square numbers between each pair of cubes and flattening them into one list, the latter is formed by filtering non-squares out of a list of cubes.
 
<langsyntaxhighlight lang="scala">import spire.math.SafeLong
import spire.implicits._
 
def ncs: LazyList[SafeLong] = LazyList.iterate(SafeLong(1))(_ + 1).flatMap(n => Iterator.iterate(n.pow(3).sqrt + 1)(_ + 1).map(i => i*i).takeWhile(_ < (n + 1).pow(3)))
def scs: LazyList[SafeLong] = LazyList.iterate(SafeLong(1))(_ + 1).map(_.pow(3)).filter(n => n.sqrt.pow(2) == n)</langsyntaxhighlight>
 
{{out}}
Line 2,671 ⟶ 3,608:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var square_and_cube = Enumerator({|f|
1..Inf -> each {|n| f(n**6) }
})
Line 2,683 ⟶ 3,620:
 
say "First 15 positive integers that are both a square and a cube:"
say square_and_cube.first(15).join(' ')</langsyntaxhighlight>
{{out}}
<pre>
Line 2,693 ⟶ 3,630:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">var s = 1, c = 1, cube = 1, n = 0
while n < 30 {
let square = s * s
Line 2,707 ⟶ 3,644:
}
s += 1
}</langsyntaxhighlight>
 
{{out}}
Line 2,746 ⟶ 3,683:
</pre>
 
=={{header|Visual Basic .NETTcl}}==
{{trans|Common Lisp}}
Inspired by the '''F#''' version, but no longer resembles it. Removed the recursion, multiplying (like the '''D''' and '''Pascal''' versions, only addition is used to calculate squares and cubes), '''match''' (Select Case) statement, and hard-coded limit.
<syntaxhighlight lang="tcl">proc squaregen {{i 0}} {
<lang vbnet>Module Module1
proc squaregen "{i [incr i]}" [info body squaregen]
expr $i * $i
}
 
proc is_cube {n} {
' flag / mask explanation:
for {set i 1} {($i * $i * $i) < $n} {incr i} { }
' bit 0 (1) = increment square
'expr ($i bit* 1$i (2* $i) == increment cube$n
}
' bit 2 (4) = has output
 
set cubes {}
' Checks flag against mask, then advances mask.
set noncubes {}
Function ChkFlg(flag As Integer, ByRef mask As Integer) As Boolean
for {set s [squaregen]} {[llength $noncubes] < 30} {set s [squaregen]} {
ChkFlg = (flag And mask) = mask : mask <<= 1
if [is_cube $s] {
End Function
lappend cubes $s
} else {
lappend noncubes $s
}
}
puts "Squares but not cubes:"
puts $noncubes
puts {}
puts "Both squares and cubes:"
puts $cubes</syntaxhighlight>
 
{{Out}}
Sub SwoC(limit As Integer)
<pre>Squares but not cubes:
Dim count, square, delta, cube, d1, d2, flag, mask As Integer, s as string = ""
4 9 16 25 count36 =49 181 :100 square121 =144 1169 :196 delta225 =256 1289 :324 cube361 =400 1441 :484 d1529 =576 1625 :676 d2784 =841 0900 961 1024 1089
While count <= limit
flag = {5, 7, 2}(1 + square.CompareTo(cube))
If flag = 7 Then s = String. Format(" {0} (also cube)", square)
If flag = 5 Then s = String.Format("{0,-2} {1}", count, square) : count += 1
mask = 1 : If ChkFlg(flag, mask) Then delta += 2 : square += delta
If ChkFlg(flag, mask) Then d2 += 6 : d1 += d2 : cube += d1
If ChkFlg(flag, mask) Then Console.WriteLine(s)
End While
End Sub
 
Both squares and cubes:
Sub Main()
1 64 729</pre>
SwoC(30)
End Sub
 
=={{header|UNIX Shell}}==
End Module</lang>
 
{{works with|Korn Shell}}
Ksh has a built-in cube root function, making this a little simpler:
<syntaxhighlight lang="sh"># First 30 positive integers which are squares but not cubes
# also, the first 3 positive integers which are both squares and cubes
######
# main #
######
integer n sq cr cnt=0
for (( n=1; cnt<30; n++ )); do
(( sq = n * n ))
(( cr = cbrt(sq) ))
if (( (cr * cr * cr) != sq )); then
(( cnt++ ))
print ${sq}
else
print "${sq} is square and cube"
fi
done</syntaxhighlight>
 
{{Out}}
<pre>1 is square and cube
4
9
16
25
36
49
64 is square and cube
81
100
121
144
169
196
225
256
289
324
361
400
441
484
529
576
625
676
729 is square and cube
784
841
900
961
1024
1089</pre>
 
{{works with|Bourne Again Shell}}
 
Since Bash lacks a built-in `cbrt` function, here we adopted the BASIC algorithm.
<pre>main() {
local non_cubes=()
local cubes=()
local cr=1 cube=1 i square
for (( i=1; ${#non_cubes[@]} < 30; ++i )); do
(( square = i * i ))
while (( square > cube )); do
(( cr+=1, cube=cr*cr*cr ))
done
if (( square == cube )); then
cubes+=($square)
else
non_cubes+=($square)
fi
done
printf 'Squares but not cubes:\n'
printf ${non_cubes[0]}
printf ', %d' "${non_cubes[@]:1}"
 
printf '\n\nBoth squares and cubes:\n'
printf ${cubes[0]}
printf ', %d' "${cubes[@]:1}"
printf '\n'
}
 
main "$@"</pre>
 
{{works with|Zsh}}
 
The Zsh solution is virtually identical to the Bash one, the main difference being the array syntax and base index.
 
<syntaxhighlight lang="sh">#!/usr/bin/env bash
 
main() {
local non_cubes=()
local cubes=()
local cr=1 cube=1 i square
for (( i=1; $#non_cubes < 30; ++i )); do
(( square = i * i ))
while (( square > cube )); do
(( cr+=1, cube=cr*cr*cr ))
done
if (( square == cube )); then
cubes+=($square)
else
non_cubes+=($square)
fi
done
printf 'Squares but not cubes:\n'
printf $non_cubes[1]
printf ', %d' "${(@)non_cubes[2,-1]}"
 
printf '\n\nBoth squares and cubes:\n'
printf $cubes[1]
printf ', %d' "${(@)cubes[2,-1]}"
printf '\n'
}
 
main "$@"</syntaxhighlight>
 
{{Out}}
Both the bash and zsh versions have the same output:
 
<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|VTL-2}}==
<syntaxhighlight lang="vtl2">10 N=0
20 S=1
30 C=1
40 #=60
50 C=C+1
60 #=C*C*C<(S*S)*50
70 #=C*C*C=(S*S)*110
80 N=N+1
90 ?=S*S
100 $=32
110 S=S+1
120 #=N<30*60</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>
<pre> 1 (also cube)
1 4
2 9
3 16
4 25
5 36
6 49
64 (also cube)
7 81
8 100
9 121
10 144
11 169
12 196
13 225
14 256
15 289
16 324
17 361
18 400
19 441
20 484
21 529
22 576
23 625
24 676
729 (also cube)
25 784
26 841
27 900
28 961
29 1024
30 1089</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<lang ecmascript>import "/math" for Math
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var i = 1
Line 2,822 ⟶ 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)
Line 2,833 ⟶ 3,884:
System.print(sqnc.take(30).toList)
System.print("\nThe first 3 positive integers which are both squares and cubes are:")
System.print(sqcb.take(3).toList)</langsyntaxhighlight>
 
{{out}}
Line 2,842 ⟶ 3,893:
The first 3 positive integers which are both squares and cubes are:
[1, 64, 729]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">int C, N, N2, T;
[C:= 0; N:= 1;
loop [N2:= N*N;
IntOut(0, N2);
T:= fix(Pow(float(N2), 1./3.));
if T*T*T # N2 then
[ChOut(0, ^ );
C:= C+1;
if C >= 30 then quit;
]
else Text(0, "* ");
N:= N+1;
];
Text(0, "^m^j* are both squares and cubes.^m^j");
]</syntaxhighlight>
 
{{out}}
<pre>
1* 4 9 16 25 36 49 64* 81 100 121 144 169 196 225 256 289 324 361 400 441 484 529 576 625 676 729* 784 841 900 961 1024 1089
* are both squares and cubes.
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">println("First 30 positive integers that are a square but not a cube:");
squareButNotCube:=(1).walker(*).tweak(fcn(n){
sq,cr := n*n, sq.toFloat().pow(1.0/3).round(); // cube root(64)<4
Line 2,853 ⟶ 3,927:
 
println("First 15 positive integers that are both a square and a cube:");
println((1).walker(*).tweak((1).pow.unbind().fp1(6)).walk(15));</langsyntaxhighlight>
{{out}}
<pre>
2,094

edits