Square but not cube: Difference between revisions
Add MACRO-11
(→{{header|Tcl}}: Add implementation.) |
Not a robot (talk | contribs) (Add MACRO-11) |
||
(22 intermediate revisions by 11 users not shown) | |||
Line 10:
{{trans|C}}
<
V count = 0
Line 21:
E
print(sq‘ is square and cube’)
n++</
=={{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}}==
<
org 100h
section .text
Line 70 ⟶ 193:
section .data
db '*****' ; Placeholder for number
num: db ' $'</
{{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}}==
<
procedure Square_But_Not_Cube is
Line 111 ⟶ 331:
begin
Show (Limit => 30);
end Square_But_Not_Cube;</
{{out}}
Line 119 ⟶ 339:
=={{header|ALGOL 68}}==
Avoids computing cube roots.
<
# 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</
{{out}}
<pre>
Line 180 ⟶ 400:
=={{header|ALGOL-M}}==
<
integer function square(x);
integer x;
Line 203 ⟶ 423:
s := s + 1;
end;
end</
{{out}}
Line 214 ⟶ 434:
=={{header|APL}}==
<
{{out}}
Line 221 ⟶ 441:
=={{header|AppleScript}}==
<
script listing
on |λ|(x)
Line 291 ⟶ 511:
set my text item delimiters to dlm
str
end unlines</
{{Out}}
<pre>1 (also cube)
Line 328 ⟶ 548:
=={{header|Arturo}}==
<
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]</
{{out}}
Line 344 ⟶ 564:
=={{header|AutoHotkey}}==
<
while counter<30 {
cube[(n := A_Index)**3] := true
Line 352 ⟶ 572:
res .= "[" n**2 "] "
}
MsgBox % Trim(res, " ")</
square and cube numbers are denoted by square brackets:
Outputs:<pre>
Line 358 ⟶ 578:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SQUARE_BUT_NOT_CUBE.AWK
BEGIN {
Line 381 ⟶ 601:
return(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 420 ⟶ 640:
=={{header|BASIC}}==
<
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</
{{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}}==
<
let square(x) = x * x
Line 450 ⟶ 937:
s := s + 1
$)
$)</
{{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}}==
<
#include <math.h>
Line 476 ⟶ 975:
}
return 0;
}</
{{out}}
Line 484 ⟶ 983:
=={{header|C sharp}}==
<
using System.Collections.Generic;
using static System.Console;
Line 524 ⟶ 1,023:
}
}</
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 563 ⟶ 1,062:
=={{header|C++}}==
{{trans|C}}
<
#include <cmath>
Line 584 ⟶ 1,083:
return 0;
}</
{{out}}
<pre>1 is square and cube
Line 622 ⟶ 1,121:
=={{header|Clojure}}==
{{trans|Raku}}
<
(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>
{{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}}==
<
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.</
{{out}}
<pre style='height:50ex;'> 4
Line 700 ⟶ 1,232:
1089</pre>
=={{header|
<syntaxhighlight lang="comal">0010 ZONE 5
0020 cube_n#:=0;square_n#:=0;seen#:=0
0030 WHILE seen#<30 DO
0040 WHILE cube_n#^3<square_n#^2 DO cube_n#:+1
0050 IF cube_n#^3<>square_n#^2 THEN
0060 PRINT square_n#^2,
0070 seen#:+1
0090 ENDIF
0100 square_n#:+1
0110 ENDWHILE
0120 END</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|Common Lisp}}==
<
(loop for i from 1
for c = (* i i i)
Line 776 ⟶ 1,270:
finally (return (list noncubes cubes)))
(format t "Squares but not cubes:~%~A~%~%" noncubes)
(format t "Both squares and cubes:~%~A~%~%" cubes))</
{{Out}}
Line 786 ⟶ 1,280:
=={{header|Cowgol}}==
<
var cube: uint16 := 1;
Line 807 ⟶ 1,301:
nsqr := nsqr + 1;
end loop;
print_nl();</
{{out}}
Line 815 ⟶ 1,309:
=={{header|D}}==
{{trans|C#}}
<
import std.range;
import std.stdio;
Line 910 ⟶ 1,404:
}
}
}</
{{out}}
<pre>1 (also cube)
Line 948 ⟶ 1,442:
=={{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
Line 1,014 ⟶ 1,508:
[initialize i to 0, set f again, and call B to print out the values in l]sz
0 si 1 sf lBx 10P</
{{Out}}
Line 1,027 ⟶ 1,521:
{{libheader| System.Math}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Square_but_not_cube;
Line 1,054 ⟶ 1,548:
{$IFNDEF UNIX} readln; {$ENDIF}
end.</
=={{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#}}==
<
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>
{{out}}
<pre>
Line 1,102 ⟶ 1,650:
=={{header|Factor}}==
{{trans|F#}}
<
math.functions math.order pair-rocket ;
IN: rosetta-code.square-but-not-cube
Line 1,115 ⟶ 1,663:
] when ;
1 1 1 fn 3drop</
{{out}}
<pre>
Line 1,154 ⟶ 1,702:
=={{header|FALSE}}==
<
[2O30>~][
[$$*2O$$**>][\1+\]#
Line 1,161 ⟶ 1,709:
]#
%%%
10,</
{{out}}
Line 1,168 ⟶ 1,716:
=={{header|FOCAL}}==
<
01.20 I (N-30)1.3,1.3,1.8
01.30 S S=Q*Q
Line 1,175 ⟶ 1,723:
01.60 S N=N+1;T %4,S,!
01.70 S Q=Q+1;G 1.2
01.80 Q</
{{out}}
Line 1,211 ⟶ 1,759:
=={{header|Forth}}==
<
: cube dup dup * * ;
: 30-non-cube-squares
Line 1,227 ⟶ 1,775:
;
30-non-cube-squares cr bye</
{{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|Go}}==
<
import (
Line 1,286 ⟶ 1,800:
}
}
}</
{{out}}
Line 1,326 ⟶ 1,840:
=={{header|Haskell}}==
<
import Control.Monad (join)
Line 1,351 ⟶ 1,865:
( ((," (also cube)") <$> take 3 both)
<> ((,"") <$> take 30 only)
)</
Or simply
<
------------------- SQUARE BUT NOT CUBE ------------------
Line 1,376 ⟶ 1,890:
| isCube x = " (also cube of " <> show (cubeRoot x) <> ")"
| otherwise = []
</syntaxhighlight>
Or, if we prefer a finite series to an infinite one
<
isCube =
(==)
Line 1,396 ⟶ 1,910:
label n
| isCube n = " (also cube)"
| otherwise = ""</
{{Out}}
<pre>1 (also cube)
Line 1,431 ⟶ 1,945:
1024
1089</pre>
=={{header|J}}==
'''Solution:'''
<
getN_Indicies=: adverb def '[ ({. I.) [ (] , [: u (i.200) + #@])^:(> +/)^:_ u@]'</
'''Example Use:'''
<
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</
'''Alternative Solution:'''
Breaking up the solution above into smaller chunks with comments...
<
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,474 ⟶ 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'</
'''Example Use:'''
<
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>
=={{header|Java}}==
<
public static boolean isPerfectCube(long n) {
long c = (long)Math.cbrt((double)n);
Line 1,506 ⟶ 2,002:
}
}
}</
{{out}}
<pre>
Line 1,544 ⟶ 2,040:
=={{header|JavaScript}}==
<
'use strict';
Line 1,593 ⟶ 2,089:
// MAIN ---
return main();
})();</
{{Out}}
<pre>1 (cube of 1 and square of 1)
Line 1,632 ⟶ 2,128:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
# Emit an unbounded stream
def squares_not_cubes:
Line 1,642 ⟶ 2,138:
limit(30; squares_not_cubes)
</syntaxhighlight>
{{out}}
<pre>
Line 1,658 ⟶ 2,154:
=={{header|Julia}}==
<syntaxhighlight 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>
{{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}}==
<
fun main(args: Array<String>) {
Line 1,684 ⟶ 2,180:
n++
}
}</
{{output}}
Line 1,690 ⟶ 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}}
<
I HAS A SkwareKyoobs ITZ A BUKKIT
Line 1,739 ⟶ 2,293:
VISIBLE ""
KTHXBYE</
{{Out}}
Line 1,751 ⟶ 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.
<
local r = 1
for i = 1, 16 do
Line 1,771 ⟶ 2,325:
count = count + 1
end
end</
{{out}}
<pre>1 is square and cube
Line 1,800 ⟶ 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,808 ⟶ 2,448:
=={{header|MAD}}==
<
CUBE=1
Line 1,830 ⟶ 2,470:
VECTOR VALUES FMT = $I4*$
END OF PROGRAM </
{{out}}
Line 1,866 ⟶ 2,506:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
c = Range[1, Ceiling[Surd[Max[s], 3]]]^3;
Take[Complement[s, c], 30]
Intersection[s, c]</
{{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}}==
<
tris = []
both = []
Line 1,890 ⟶ 2,579:
print squares[:30]
print "Both square and cube:"
print both[:3]</
{{out}}
<pre>Square but not cube:
Line 1,896 ⟶ 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}}==
<
var n, c, c3 = 1
Line 1,911 ⟶ 2,664:
echo sq
inc count
inc n</
<pre>
1 is square and cube
Line 1,950 ⟶ 2,703:
=={{header|OCaml}}==
{{trans|F#}}
<
if phi < 31 then
match compare (n*n) (g*g*g) with
Line 1,959 ⟶ 2,712:
;;
fN 1 1 1</
=={{header|Pascal}}==
Only using addition :-)
<
var
sqN,
Line 2,010 ⟶ 2,763:
until CountSqNotCb >= 30;//sqrt(High(NativeUint));
writeln(CountSqANDCb,' where numbers are square and cube ');
end.</
{{out}}
<pre> 1 1*1 = 1*1*1
Line 2,054 ⟶ 2,807:
==== Hash ====
Use a hash to track state (and avoid floating-point math).
<
$n++;
$h{$n**2}++;
Line 2,065 ⟶ 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;</
{{out}}
<pre>First 30 positive integers that are a square but not a cube:
Line 2,079 ⟶ 2,832:
Output is the same as previous.
<
sub gen_pow {
my $m = shift;
Line 2,108 ⟶ 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;</
=={{header|Phix}}==
<!--<
<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,130 ⟶ 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>
<!--</
{{out}}
<pre>
Line 2,170 ⟶ 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}}==
<
square: procedure(n) returns(fixed);
Line 2,197 ⟶ 3,003:
end;
end;
end squareNotCube;</
{{out}}
<pre> 4 9 16 25 36 49 81 100 121 144
Line 2,204 ⟶ 3,010:
=={{header|PL/M}}==
<
BDOS: PROCEDURE (FN, ARG);
DECLARE FN BYTE, ARG ADDRESS;
Line 2,247 ⟶ 3,053:
CALL BDOS(0,0);
EOF</
{{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|Python}}==
<
def nonCubeSquares(n):
upto = enumFromTo(1)
Line 2,327 ⟶ 3,112:
main()</
{{Out}}
<pre>(1^2 = 1 = 1^3)
Line 2,362 ⟶ 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,367 ⟶ 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:
<
(require racket/generator)
Line 2,387 ⟶ 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))</
{{out}}
Line 2,398 ⟶ 3,210:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my @square-but-not-cube = (1..Inf).map({ .² }).grep({ $_ ∉ @square-and-cube[^@square-and-cube.first: * > $_, :k]});
Line 2,404 ⟶ 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];</
{{out}}
<pre>First 30 positive integers that are a square but not a cube:
Line 2,412 ⟶ 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: extra code was added to support an additional output format (see the 2<sup>nd</sup> '''output''' section).
<
numeric digits 20 /*ensure handling of larger numbers. */
parse arg N . /*obtain optional argument from the CL.*/
Line 2,441 ⟶ 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 ?</
{{out|output|text= when using the default input:}}
<pre>
Line 2,542 ⟶ 3,378:
=={{header|Ring}}==
<
# Project : Square but not cube
Line 2,567 ⟶ 3,403:
next
return 0
</syntaxhighlight>
Output:
<pre>
Line 2,605 ⟶ 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 ====
<
class PowIt
Line 2,659 ⟶ 3,515:
puts "Square-and-cubes:"
puts hexponents.join(" ")</
{{out}}
<pre>Squares:
Line 2,666 ⟶ 3,522:
1 64 729</pre>
==== Enumerator ====
<
puts "Square cubes: %p
Square non-cubes: %p" % squares.take(33).partition{|sq| Math.cbrt(sq).to_i ** 3 == sq }</
{{out}}
<pre>Square cubes: [1, 64, 729]
Line 2,676 ⟶ 3,532:
=={{header|Rust}}==
<
let mut s = 1;
let mut c = 1;
Line 2,695 ⟶ 3,551:
s += 1;
}
}</
{{out}}
Line 2,737 ⟶ 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.
<
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)</
{{out}}
Line 2,752 ⟶ 3,608:
=={{header|Sidef}}==
{{trans|Raku}}
<
1..Inf -> each {|n| f(n**6) }
})
Line 2,764 ⟶ 3,620:
say "First 15 positive integers that are both a square and a cube:"
say square_and_cube.first(15).join(' ')</
{{out}}
<pre>
Line 2,774 ⟶ 3,630:
=={{header|Swift}}==
<
while n < 30 {
let square = s * s
Line 2,788 ⟶ 3,644:
}
s += 1
}</
{{out}}
Line 2,829 ⟶ 3,685:
=={{header|Tcl}}==
{{trans|Common Lisp}}
<
proc squaregen "{i [incr i]}" [info body squaregen]
expr $i * $i
Line 2,852 ⟶ 3,708:
puts {}
puts "Both squares and cubes:"
puts $cubes</
{{Out}}
Line 2,865 ⟶ 3,721:
{{works with|Korn Shell}}
Ksh has a built-in cube root function, making this a little simpler:
<
# also, the first 3 positive integers which are both squares and cubes
Line 2,883 ⟶ 3,739:
print "${sq} is square and cube"
fi
done</
{{Out}}
Line 2,954 ⟶ 3,810:
The Zsh solution is virtually identical to the Bash one, the main difference being the array syntax and base index.
<
main() {
Line 2,981 ⟶ 3,837:
}
main "$@"</
{{Out}}
Line 2,992 ⟶ 3,848:
1, 64, 729</pre>
=={{header|
<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>
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
var i = 1
Line 3,068 ⟶ 3,873:
while (sqnc.count < 30 || sqcb.count < 3) {
var sq = i * i
var cb =
if (cb*cb*cb != sq) {
sqnc.add(sq)
Line 3,079 ⟶ 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)</
{{out}}
Line 3,088 ⟶ 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}}==
<
squareButNotCube:=(1).walker(*).tweak(fcn(n){
sq,cr := n*n, sq.toFloat().pow(1.0/3).round(); // cube root(64)<4
Line 3,099 ⟶ 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));</
{{out}}
<pre>
|