Square but not cube: Difference between revisions

Content added Content deleted
(Add VTL-2)
m (syntax highlighting fixup automation)
Line 10: Line 10:
{{trans|C}}
{{trans|C}}


<lang 11l>V n = 1
<syntaxhighlight lang="11l">V n = 1
V count = 0
V count = 0


Line 21: Line 21:
E
E
print(sq‘ is square and cube’)
print(sq‘ is square and cube’)
n++</lang>
n++</syntaxhighlight>


=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==
<lang asm> cpu 8086
<syntaxhighlight lang="asm"> cpu 8086
org 100h
org 100h
section .text
section .text
Line 70: Line 70:
section .data
section .data
db '*****' ; Placeholder for number
db '*****' ; Placeholder for number
num: db ' $'</lang>
num: db ' $'</syntaxhighlight>


{{out}}
{{out}}
Line 77: Line 77:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>BYTE FUNC IsCube(INT n)
<syntaxhighlight lang="action!">BYTE FUNC IsCube(INT n)
INT i,c
INT i,c


Line 118: Line 118:
n==+1
n==+1
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Square_but_not_cube.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Square_but_not_cube.png Screenshot from Atari 8-bit computer]
Line 130: Line 130:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;


procedure Square_But_Not_Cube is
procedure Square_But_Not_Cube is
Line 164: Line 164:
begin
begin
Show (Limit => 30);
Show (Limit => 30);
end Square_But_Not_Cube;</lang>
end Square_But_Not_Cube;</syntaxhighlight>


{{out}}
{{out}}
Line 172: Line 172:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
Avoids computing cube roots.
Avoids computing cube roots.
<lang algol68>BEGIN
<syntaxhighlight lang="algol68">BEGIN
# list the first 30 numbers that are squares but not cubes and also #
# list the first 30 numbers that are squares but not cubes and also #
# show the numbers that are both squares and cubes #
# show the numbers that are both squares and cubes #
Line 194: Line 194:
print( ( newline ) )
print( ( newline ) )
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 233: Line 233:


=={{header|ALGOL-M}}==
=={{header|ALGOL-M}}==
<lang algolm>begin
<syntaxhighlight lang="algolm">begin
integer function square(x);
integer function square(x);
integer x;
integer x;
Line 256: Line 256:
s := s + 1;
s := s + 1;
end;
end;
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 267: Line 267:


=={{header|APL}}==
=={{header|APL}}==
<lang APL>(×⍨∘⍳ ~ (⊢×⊢×⊢)∘⍳) 33</lang>
<syntaxhighlight lang="apl">(×⍨∘⍳ ~ (⊢×⊢×⊢)∘⍳) 33</syntaxhighlight>


{{out}}
{{out}}
Line 274: Line 274:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>on run
<syntaxhighlight lang="applescript">on run
script listing
script listing
on |λ|(x)
on |λ|(x)
Line 344: Line 344:
set my text item delimiters to dlm
set my text item delimiters to dlm
str
str
end unlines</lang>
end unlines</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1 (also cube)
<pre>1 (also cube)
Line 381: Line 381:


=={{header|Arturo}}==
=={{header|Arturo}}==
<lang rebol>squares: map 1..100 => [&^2]
<syntaxhighlight lang="rebol">squares: map 1..100 => [&^2]
cubes: map 1..100 => [&^3]
cubes: map 1..100 => [&^3]


Line 387: Line 387:
print first.n:30 select squares => [not? in? & cubes]
print first.n:30 select squares => [not? in? & cubes]
print "Square and cube:"
print "Square and cube:"
print first.n:3 select squares => [in? & cubes]</lang>
print first.n:3 select squares => [in? & cubes]</syntaxhighlight>


{{out}}
{{out}}
Line 397: Line 397:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>cube := [], counter:=0
<syntaxhighlight lang="autohotkey">cube := [], counter:=0
while counter<30 {
while counter<30 {
cube[(n := A_Index)**3] := true
cube[(n := A_Index)**3] := true
Line 405: Line 405:
res .= "[" n**2 "] "
res .= "[" n**2 "] "
}
}
MsgBox % Trim(res, " ")</lang>
MsgBox % Trim(res, " ")</syntaxhighlight>
square and cube numbers are denoted by square brackets:
square and cube numbers are denoted by square brackets:
Outputs:<pre>
Outputs:<pre>
Line 411: Line 411:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SQUARE_BUT_NOT_CUBE.AWK
# syntax: GAWK -f SQUARE_BUT_NOT_CUBE.AWK
BEGIN {
BEGIN {
Line 434: Line 434:
return(0)
return(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 473: Line 473:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang BASIC>10 DEFINT C,S,Q,R,N: C=1: S=1: Q=1: R=1: N=1
<syntaxhighlight lang="basic">10 DEFINT C,S,Q,R,N: C=1: S=1: Q=1: R=1: N=1
20 IF N>30 THEN END
20 IF N>30 THEN END
30 S=Q*Q
30 S=Q*Q
Line 479: Line 479:
50 IF S<C THEN N=N+1: PRINT S;
50 IF S<C THEN N=N+1: PRINT S;
60 Q=Q+1
60 Q=Q+1
70 GOTO 20</lang>
70 GOTO 20</syntaxhighlight>


{{out}}
{{out}}
Line 487: Line 487:


=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let square(x) = x * x
let square(x) = x * x
Line 503: Line 503:
s := s + 1
s := s + 1
$)
$)
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre> 4 9 16 25 36
<pre> 4 9 16 25 36
Line 512: Line 512:
841 900 961 1024 1089</pre>
841 900 961 1024 1089</pre>
=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn>∘‿5⥊ ((⊢⋆3˙)((¬∊)/⊢)⊢⋆2˙) ↕34</lang>
<syntaxhighlight lang="bqn">∘‿5⥊ ((⊢⋆3˙)((¬∊)/⊢)⊢⋆2˙) ↕34</syntaxhighlight>
{{out}}
{{out}}
<pre>┌─
<pre>┌─
Line 524: Line 524:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
#include <math.h>


Line 541: Line 541:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 549: Line 549:


=={{header|C sharp}}==
=={{header|C sharp}}==
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using static System.Console;
using static System.Console;
Line 589: Line 589:


}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:30ex;overflow:scroll">
<pre style="height:30ex;overflow:scroll">
Line 628: Line 628:
=={{header|C++}}==
=={{header|C++}}==
{{trans|C}}
{{trans|C}}
<lang cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <cmath>
#include <cmath>


Line 649: Line 649:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 is square and cube
<pre>1 is square and cube
Line 687: Line 687:
=={{header|Clojure}}==
=={{header|Clojure}}==
{{trans|Raku}}
{{trans|Raku}}
<lang clojure>(def squares (map #(* % %) (drop 1 (range))))
<syntaxhighlight lang="clojure">(def squares (map #(* % %) (drop 1 (range))))
(def square-cubes (map #(int (. Math pow % 6)) (drop 1 (range))))
(def square-cubes (map #(int (. Math pow % 6)) (drop 1 (range))))


Line 696: Line 696:
(println "Both squares and cubes:")
(println "Both squares and cubes:")
(println (take 15 square-cubes))
(println (take 15 square-cubes))
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>Squares but not cubes:
<pre>Squares but not cubes:
Line 704: Line 704:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>square_not_cube = iter () yields (int)
<syntaxhighlight lang="clu">square_not_cube = iter () yields (int)
cube_root: int := 1
cube_root: int := 1
square_root: int := 1
square_root: int := 1
Line 730: Line 730:
if n = amount then break end
if n = amount then break end
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre> 4 9 16 25 36 49 81 100 121 144
<pre> 4 9 16 25 36 49 81 100 121 144
Line 737: Line 737:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. SQUARE-NOT-CUBE.
PROGRAM-ID. SQUARE-NOT-CUBE.
Line 765: Line 765:
ADD 1 TO SQ-ROOT.
ADD 1 TO SQ-ROOT.
IF SEEN IS LESS THAN 30 GO TO SQUARE-STEP.
IF SEEN IS LESS THAN 30 GO TO SQUARE-STEP.
STOP RUN.</lang>
STOP RUN.</syntaxhighlight>
{{out}}
{{out}}
<pre style='height:50ex;'> 4
<pre style='height:50ex;'> 4
Line 799: Line 799:


=={{header|Comal}}==
=={{header|Comal}}==
<lang comal>0010 ZONE 5
<syntaxhighlight lang="comal">0010 ZONE 5
0020 cube_n#:=0;square_n#:=0;seen#:=0
0020 cube_n#:=0;square_n#:=0;seen#:=0
0030 WHILE seen#<30 DO
0030 WHILE seen#<30 DO
Line 810: Line 810:
0100 square_n#:+1
0100 square_n#:+1
0110 ENDWHILE
0110 ENDWHILE
0120 END</lang>
0120 END</syntaxhighlight>
{{out}}
{{out}}
<pre>4 9 16 25 36
<pre>4 9 16 25 36
Line 821: Line 821:
=={{header|Commodore BASIC}}==
=={{header|Commodore BASIC}}==
{{trans|BASIC}}
{{trans|BASIC}}
<lang basic>100 DIM SC(2): SC = 0: REM REMEMBER SQUARE CUBES
<syntaxhighlight lang="basic">100 DIM SC(2): SC = 0: REM REMEMBER SQUARE CUBES
110 PRINT "SQUARES BUT NOT CUBES:"
110 PRINT "SQUARES BUT NOT CUBES:"
120 N = 0: REM NUMBER OF NON-CUBE SQUARES FOUND
120 N = 0: REM NUMBER OF NON-CUBE SQUARES FOUND
Line 837: Line 837:
240 PRINT "BOTH SQUARES AND CUBES:"
240 PRINT "BOTH SQUARES AND CUBES:"
250 FOR I=0 TO SC-1: PRINT SC(I),: NEXT I
250 FOR I=0 TO SC-1: PRINT SC(I),: NEXT I
260 PRINT</lang>
260 PRINT</syntaxhighlight>


{{works with|Commodore BASIC|3.5,7.0}}
{{works with|Commodore BASIC|3.5,7.0}}
Line 879: Line 879:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>(defun cubep (n)
<syntaxhighlight lang="lisp">(defun cubep (n)
(loop for i from 1
(loop for i from 1
for c = (* i i i)
for c = (* i i i)
Line 895: Line 895:
finally (return (list noncubes cubes)))
finally (return (list noncubes cubes)))
(format t "Squares but not cubes:~%~A~%~%" noncubes)
(format t "Squares but not cubes:~%~A~%~%" noncubes)
(format t "Both squares and cubes:~%~A~%~%" cubes))</lang>
(format t "Both squares and cubes:~%~A~%~%" cubes))</syntaxhighlight>


{{Out}}
{{Out}}
Line 905: Line 905:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


var cube: uint16 := 1;
var cube: uint16 := 1;
Line 926: Line 926:
nsqr := nsqr + 1;
nsqr := nsqr + 1;
end loop;
end loop;
print_nl();</lang>
print_nl();</syntaxhighlight>


{{out}}
{{out}}
Line 934: Line 934:
=={{header|D}}==
=={{header|D}}==
{{trans|C#}}
{{trans|C#}}
<lang d>import std.algorithm;
<syntaxhighlight lang="d">import std.algorithm;
import std.range;
import std.range;
import std.stdio;
import std.stdio;
Line 1,029: Line 1,029:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 (also cube)
<pre>1 (also cube)
Line 1,067: Line 1,067:
=={{header|dc}}==
=={{header|dc}}==
{{trans|BASIC}}
{{trans|BASIC}}
<syntaxhighlight lang="dc">
<lang dc>
[n = # of non-cube squares found; we stop when it hits 30]sz
[n = # of non-cube squares found; we stop when it hits 30]sz
[b = # found that are both squares and cubes]sz
[b = # found that are both squares and cubes]sz
Line 1,133: Line 1,133:


[initialize i to 0, set f again, and call B to print out the values in l]sz
[initialize i to 0, set f again, and call B to print out the values in l]sz
0 si 1 sf lBx 10P</lang>
0 si 1 sf lBx 10P</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,146: Line 1,146:
{{libheader| System.Math}}
{{libheader| System.Math}}
{{Trans|Go}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Square_but_not_cube;
program Square_but_not_cube;


Line 1,173: Line 1,173:


{$IFNDEF UNIX} readln; {$ENDIF}
{$IFNDEF UNIX} readln; {$ENDIF}
end.</lang>
end.</syntaxhighlight>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight 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)
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)φ
| 0->printfn "%d cube and square"(n*n);fN(n+1)(g+1)φ
| 1->fN n (g+1) φ
| 1->fN n (g+1) φ
fN 1 1 1
fN 1 1 1
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,221: Line 1,221:
=={{header|Factor}}==
=={{header|Factor}}==
{{trans|F#}}
{{trans|F#}}
<lang factor>USING: combinators interpolate io kernel prettyprint math
<syntaxhighlight lang="factor">USING: combinators interpolate io kernel prettyprint math
math.functions math.order pair-rocket ;
math.functions math.order pair-rocket ;
IN: rosetta-code.square-but-not-cube
IN: rosetta-code.square-but-not-cube
Line 1,234: Line 1,234:
] when ;
] when ;


1 1 1 fn 3drop</lang>
1 1 1 fn 3drop</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,273: Line 1,273:


=={{header|FALSE}}==
=={{header|FALSE}}==
<lang false>1 1 1
<syntaxhighlight lang="false">1 1 1
[2O30>~][
[2O30>~][
[$$*2O$$**>][\1+\]#
[$$*2O$$**>][\1+\]#
Line 1,280: Line 1,280:
]#
]#
%%%
%%%
10,</lang>
10,</syntaxhighlight>


{{out}}
{{out}}
Line 1,287: Line 1,287:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<lang FOCAL>01.10 S C=1;S S=1;S Q=1;S R=1;S N=1
<syntaxhighlight lang="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.20 I (N-30)1.3,1.3,1.8
01.30 S S=Q*Q
01.30 S S=Q*Q
Line 1,294: Line 1,294:
01.60 S N=N+1;T %4,S,!
01.60 S N=N+1;T %4,S,!
01.70 S Q=Q+1;G 1.2
01.70 S Q=Q+1;G 1.2
01.80 Q</lang>
01.80 Q</syntaxhighlight>


{{out}}
{{out}}
Line 1,330: Line 1,330:


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>: square dup * ;
<syntaxhighlight lang="forth">: square dup * ;
: cube dup dup * * ;
: cube dup dup * * ;
: 30-non-cube-squares
: 30-non-cube-squares
Line 1,346: Line 1,346:
;
;


30-non-cube-squares cr bye</lang>
30-non-cube-squares cr bye</syntaxhighlight>


{{out}}
{{out}}
Line 1,353: Line 1,353:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>function is_pow(n as integer, q as integer) as boolean
<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
'tests if the number n is the q'th power of some other integer
dim as integer r = int( n^(1.0/q) )
dim as integer r = int( n^(1.0/q) )
Line 1,381: Line 1,381:
n += 1
n += 1
loop until count = 3
loop until count = 3
print</lang>
print</syntaxhighlight>
{{out}}
{{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> 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
Line 1,387: Line 1,387:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,405: Line 1,405:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,445: Line 1,445:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>{-# LANGUAGE TupleSections #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-}


import Control.Monad (join)
import Control.Monad (join)
Line 1,470: Line 1,470:
( ((," (also cube)") <$> take 3 both)
( ((," (also cube)") <$> take 3 both)
<> ((,"") <$> take 30 only)
<> ((,"") <$> take 30 only)
)</lang>
)</syntaxhighlight>


Or simply
Or simply


<lang haskell>import Control.Monad (join)
<syntaxhighlight lang="haskell">import Control.Monad (join)


------------------- SQUARE BUT NOT CUBE ------------------
------------------- SQUARE BUT NOT CUBE ------------------
Line 1,495: Line 1,495:
| isCube x = " (also cube of " <> show (cubeRoot x) <> ")"
| isCube x = " (also cube of " <> show (cubeRoot x) <> ")"
| otherwise = []
| otherwise = []
</syntaxhighlight>
</lang>


Or, if we prefer a finite series to an infinite one
Or, if we prefer a finite series to an infinite one
<lang haskell>isCube :: Int -> Bool
<syntaxhighlight lang="haskell">isCube :: Int -> Bool
isCube =
isCube =
(==)
(==)
Line 1,515: Line 1,515:
label n
label n
| isCube n = " (also cube)"
| isCube n = " (also cube)"
| otherwise = ""</lang>
| otherwise = ""</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1 (also cube)
<pre>1 (also cube)
Line 1,552: Line 1,552:


=={{header|IS-BASIC}}==
=={{header|IS-BASIC}}==
<lang IS-BASIC>100 PROGRAM "Square.bas"
<syntaxhighlight lang="is-basic">100 PROGRAM "Square.bas"
110 LET SQNOTCB,SQANDCB,SQNUM,CBNUM,CBN,SQN,D1=0:LET SQD,D2=1
110 LET SQNOTCB,SQANDCB,SQNUM,CBNUM,CBN,SQN,D1=0:LET SQD,D2=1
120 DO
120 DO
Line 1,567: Line 1,567:
230 END IF
230 END IF
240 LOOP UNTIL SQNOTCB>=30
240 LOOP UNTIL SQNOTCB>=30
250 PRINT SQANDCB;"where numbers are square and cube."</lang>
250 PRINT SQANDCB;"where numbers are square and cube."</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<lang j>isSqrNotCubeofInt=: (*. -.)/@(= <.)@(2 3 %:/ ])
<syntaxhighlight lang="j">isSqrNotCubeofInt=: (*. -.)/@(= <.)@(2 3 %:/ ])
getN_Indicies=: adverb def '[ ({. I.) [ (] , [: u (i.200) + #@])^:(> +/)^:_ u@]'</lang>
getN_Indicies=: adverb def '[ ({. I.) [ (] , [: u (i.200) + #@])^:(> +/)^:_ u@]'</syntaxhighlight>


'''Example Use:'''
'''Example Use:'''
<lang j> I. isSqrNotCubeofInt i.1090 NB. If we know the upper limit required to get first 30
<syntaxhighlight 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
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
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</lang>
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>


'''Alternative Solution:'''
'''Alternative Solution:'''


Breaking up the solution above into smaller chunks with comments...
Breaking up the solution above into smaller chunks with comments...
<lang j>isInt=: = <. NB. are numbers integers?
<syntaxhighlight lang="j">isInt=: = <. NB. are numbers integers?
sqrcube=: 2 3 %:/ ] NB. table of 2nd and 3rd roots of y
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?
isSqrNotCubeofInt=: (*. -.)/@isInt@sqrcube NB. is y the square but not cube of an integer?
Line 1,593: Line 1,593:
while=: conjunction def 'u^:v^:_' NB. repeat u while v is true
while=: conjunction def 'u^:v^:_' NB. repeat u while v is true


process_until_enough=: adverb def 'u process_more while notEnough u'</lang>
process_until_enough=: adverb def 'u process_more while notEnough u'</syntaxhighlight>


'''Example Use:'''
'''Example Use:'''
<lang j> 30 ([ getIdx isSqrNotCubeofInt process_until_enough) 0
<syntaxhighlight 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
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
900 961 1024 1089
</syntaxhighlight>
</lang>


=={{header|Java}}==
=={{header|Java}}==
<lang java>public class SquaresCubes {
<syntaxhighlight lang="java">public class SquaresCubes {
public static boolean isPerfectCube(long n) {
public static boolean isPerfectCube(long n) {
long c = (long)Math.cbrt((double)n);
long c = (long)Math.cbrt((double)n);
Line 1,625: Line 1,625:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,663: Line 1,663:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 1,712: Line 1,712:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1 (cube of 1 and square of 1)
<pre>1 (cube of 1 and square of 1)
Line 1,751: Line 1,751:
{{works with|jq}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
<lang jq>
# Emit an unbounded stream
# Emit an unbounded stream
def squares_not_cubes:
def squares_not_cubes:
Line 1,761: Line 1,761:
limit(30; squares_not_cubes)
limit(30; squares_not_cubes)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,777: Line 1,777:
=={{header|Julia}}==
=={{header|Julia}}==


<syntaxhighlight lang="julia">
<lang Julia>
iscube(n) = n == round(Int, cbrt(n))^3
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)))
println(collect(Iterators.take((n^2 for n in 1:10^6 if !iscube(n^2)), 30)))
</syntaxhighlight>
</lang>
{{Out}}
{{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]
[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}}==
=={{header|Kotlin}}==
<lang scala>// Version 1.2.60
<syntaxhighlight lang="scala">// Version 1.2.60


fun main(args: Array<String>) {
fun main(args: Array<String>) {
Line 1,803: Line 1,803:
n++
n++
}
}
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 1,811: Line 1,811:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>
<syntaxhighlight lang="ksh">
#!/bin/ksh
#!/bin/ksh


Line 1,832: Line 1,832:
print "${sq} is square and cube"
print "${sq} is square and cube"
fi
fi
done</lang>
done</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
1 is square and cube
1 is square and cube
Line 1,870: Line 1,870:
=={{header|LOLCODE}}==
=={{header|LOLCODE}}==
{{trans|Commodore BASIC}}
{{trans|Commodore BASIC}}
<lang lolcode>HAI 1.2
<syntaxhighlight lang="lolcode">HAI 1.2


I HAS A SkwareKyoobs ITZ A BUKKIT
I HAS A SkwareKyoobs ITZ A BUKKIT
Line 1,916: Line 1,916:
VISIBLE ""
VISIBLE ""


KTHXBYE</lang>
KTHXBYE</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,928: Line 1,928:
=={{header|Lua}}==
=={{header|Lua}}==
Calculating cube roots with x^(1/3) caused problems with floating-point 'dust' so the Newton-Raphson method is used instead.
Calculating cube roots with x^(1/3) caused problems with floating-point 'dust' so the Newton-Raphson method is used instead.
<lang Lua>function nthroot (x, n)
<syntaxhighlight lang="lua">function nthroot (x, n)
local r = 1
local r = 1
for i = 1, 16 do
for i = 1, 16 do
Line 1,948: Line 1,948:
count = count + 1
count = count + 1
end
end
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>1 is square and cube
<pre>1 is square and cube
Line 1,985: Line 1,985:


=={{header|MAD}}==
=={{header|MAD}}==
<lang MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
CUBE=1
CUBE=1
Line 2,007: Line 2,007:
VECTOR VALUES FMT = $I4*$
VECTOR VALUES FMT = $I4*$
END OF PROGRAM </lang>
END OF PROGRAM </syntaxhighlight>


{{out}}
{{out}}
Line 2,043: Line 2,043:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>s = Range[50]^2;
<syntaxhighlight lang="mathematica">s = Range[50]^2;
c = Range[1, Ceiling[Surd[Max[s], 3]]]^3;
c = Range[1, Ceiling[Surd[Max[s], 3]]]^3;
Take[Complement[s, c], 30]
Take[Complement[s, c], 30]
Intersection[s, c]</lang>
Intersection[s, c]</syntaxhighlight>
{{out}}
{{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>{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}
Line 2,052: Line 2,052:


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>squares = []
<syntaxhighlight lang="miniscript">squares = []
tris = []
tris = []
both = []
both = []
Line 2,067: Line 2,067:
print squares[:30]
print squares[:30]
print "Both square and cube:"
print "Both square and cube:"
print both[:3]</lang>
print both[:3]</syntaxhighlight>
{{out}}
{{out}}
<pre>Square but not cube:
<pre>Square but not cube:
Line 2,075: Line 2,075:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE SquareNotCube;
<syntaxhighlight lang="modula2">MODULE SquareNotCube;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;


Line 2,105: Line 2,105:
END;
END;
UNTIL Seen = Amount
UNTIL Seen = Amount
END SquareNotCube.</lang>
END SquareNotCube.</syntaxhighlight>
{{out}}
{{out}}
<pre> 4
<pre> 4
Line 2,139: Line 2,139:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>var count = 0
<syntaxhighlight lang="nim">var count = 0
var n, c, c3 = 1
var n, c, c3 = 1


Line 2,152: Line 2,152:
echo sq
echo sq
inc count
inc count
inc n</lang>
inc n</syntaxhighlight>
<pre>
<pre>
1 is square and cube
1 is square and cube
Line 2,191: Line 2,191:
=={{header|OCaml}}==
=={{header|OCaml}}==
{{trans|F#}}
{{trans|F#}}
<lang ocaml>let rec fN n g phi =
<syntaxhighlight lang="ocaml">let rec fN n g phi =
if phi < 31 then
if phi < 31 then
match compare (n*n) (g*g*g) with
match compare (n*n) (g*g*g) with
Line 2,200: Line 2,200:
;;
;;


fN 1 1 1</lang>
fN 1 1 1</syntaxhighlight>


=={{header|Pascal}}==
=={{header|Pascal}}==
Only using addition :-)
Only using addition :-)
<lang pascal>program SquareButNotCube;
<syntaxhighlight lang="pascal">program SquareButNotCube;
var
var
sqN,
sqN,
Line 2,251: Line 2,251:
until CountSqNotCb >= 30;//sqrt(High(NativeUint));
until CountSqNotCb >= 30;//sqrt(High(NativeUint));
writeln(CountSqANDCb,' where numbers are square and cube ');
writeln(CountSqANDCb,' where numbers are square and cube ');
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 1*1 = 1*1*1
<pre> 1 1*1 = 1*1*1
Line 2,295: Line 2,295:
==== Hash ====
==== Hash ====
Use a hash to track state (and avoid floating-point math).
Use a hash to track state (and avoid floating-point math).
<lang perl>while ($cnt < 30) {
<syntaxhighlight lang="perl">while ($cnt < 30) {
$n++;
$n++;
$h{$n**2}++;
$h{$n**2}++;
Line 2,306: Line 2,306:


print "\n\nFirst 3 positive integers that are both a square and a cube:\n";
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;</lang>
print "$_ " for sort { $a <=> $b } grep { $h{$_} == 0 } keys %h;</syntaxhighlight>
{{out}}
{{out}}
<pre>First 30 positive integers that are a square but not a cube:
<pre>First 30 positive integers that are a square but not a cube:
Line 2,320: Line 2,320:
Output is the same as previous.
Output is the same as previous.


<lang perl># return an anonymous subroutine that generates stream of specified powers
<syntaxhighlight lang="perl"># return an anonymous subroutine that generates stream of specified powers
sub gen_pow {
sub gen_pow {
my $m = shift;
my $m = shift;
Line 2,349: Line 2,349:
my $pow6 = gen_pow(6);
my $pow6 = gen_pow(6);
print "\n\nFirst 3 positive integers that are both a square and a cube:\n";
print "\n\nFirst 3 positive integers that are both a square and a cube:\n";
print $pow6->() . ' ' for 1..3;</lang>
print $pow6->() . ' ' for 1..3;</syntaxhighlight>


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="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: #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>
<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,371: Line 2,371:
<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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,413: Line 2,413:


=={{header|PILOT}}==
=={{header|PILOT}}==
<lang pilot>C :sqr=1
<syntaxhighlight lang="pilot">C :sqr=1
C :cbr=1
C :cbr=1
C :sq=1
C :sq=1
Line 2,432: Line 2,432:
C :cb=(cbr*#cbr)*#cbr
C :cb=(cbr*#cbr)*#cbr
J (sq>cb):*cube
J (sq>cb):*cube
E :</lang>
E :</syntaxhighlight>
{{out}}
{{out}}
<pre>4
<pre>4
Line 2,466: Line 2,466:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>squareNotCube: procedure options(main);
<syntaxhighlight lang="pli">squareNotCube: procedure options(main);
square: procedure(n) returns(fixed);
square: procedure(n) returns(fixed);
Line 2,491: Line 2,491:
end;
end;
end;
end;
end squareNotCube;</lang>
end squareNotCube;</syntaxhighlight>
{{out}}
{{out}}
<pre> 4 9 16 25 36 49 81 100 121 144
<pre> 4 9 16 25 36 49 81 100 121 144
Line 2,498: Line 2,498:


=={{header|PL/M}}==
=={{header|PL/M}}==
<lang plm>100H: /* CP/M OUTPUT */
<syntaxhighlight lang="plm">100H: /* CP/M OUTPUT */
BDOS: PROCEDURE (FN, ARG);
BDOS: PROCEDURE (FN, ARG);
DECLARE FN BYTE, ARG ADDRESS;
DECLARE FN BYTE, ARG ADDRESS;
Line 2,541: Line 2,541:


CALL BDOS(0,0);
CALL BDOS(0,0);
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{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>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}}==
=={{header|PureBasic}}==
<lang PureBasic>OpenConsole()
<syntaxhighlight lang="purebasic">OpenConsole()
lv=1
lv=1
Repeat
Repeat
Line 2,561: Line 2,561:
Until c>=30
Until c>=30
PrintN("s² : "+tx2$) : PrintN("s²&s³: "+tx3$)
PrintN("s² : "+tx2$) : PrintN("s²&s³: "+tx3$)
Input()</lang>
Input()</syntaxhighlight>
{{out}}
{{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
<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
Line 2,567: Line 2,567:


=={{header|Python}}==
=={{header|Python}}==
<lang python># nonCubeSquares :: Int -> [(Int, Bool)]
<syntaxhighlight lang="python"># nonCubeSquares :: Int -> [(Int, Bool)]
def nonCubeSquares(n):
def nonCubeSquares(n):
upto = enumFromTo(1)
upto = enumFromTo(1)
Line 2,621: Line 2,621:




main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>(1^2 = 1 = 1^3)
<pre>(1^2 = 1 = 1^3)
Line 2,659: Line 2,659:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ swap - -1 1 clamp 1+ ] is <=> ( n n --> n )
<syntaxhighlight lang="quackery"> [ swap - -1 1 clamp 1+ ] is <=> ( n n --> n )


[ dup * ] is squared ( n --> n )
[ dup * ] is squared ( n --> n )
Line 2,678: Line 2,678:
rot dup size 30 = until ]
rot dup size 30 = until ]
dip 2drop
dip 2drop
echo</lang>
echo</syntaxhighlight>


{{out}}
{{out}}
Line 2,688: Line 2,688:
Using a generator it _is_ possible to print the cubes in-line, but I've chosen to show reusing the generator / for / sequence interaction:
Using a generator it _is_ possible to print the cubes in-line, but I've chosen to show reusing the generator / for / sequence interaction:


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(require racket/generator)
(require racket/generator)


Line 2,708: Line 2,708:
(for ((x (in-range 1 4))
(for ((x (in-range 1 4))
((s^2 c) (sequence-filter (λ (s^2 c) c) (in-producer (make-^2-but-not-^3-generator)))))
((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))</lang>
(printf "~a: ~a is also ~a^3~%" x s^2 c))</syntaxhighlight>


{{out}}
{{out}}
Line 2,719: Line 2,719:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>my @square-and-cube = map { .⁶ }, 1..Inf;
<syntaxhighlight lang="raku" line>my @square-and-cube = map { .⁶ }, 1..Inf;


my @square-but-not-cube = (1..Inf).map({ .² }).grep({ $_ ∉ @square-and-cube[^@square-and-cube.first: * > $_, :k]});
my @square-but-not-cube = (1..Inf).map({ .² }).grep({ $_ ∉ @square-and-cube[^@square-and-cube.first: * > $_, :k]});
Line 2,725: Line 2,725:
put "First 30 positive integers that are a square but not a cube: \n", @square-but-not-cube[^30];
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];</lang>
put "\nFirst 15 positive integers that are both a square and a cube: \n", @square-and-cube[^15];</syntaxhighlight>
{{out}}
{{out}}
<pre>First 30 positive integers that are a square but not a cube:
<pre>First 30 positive integers that are a square but not a cube:
Line 2,735: Line 2,735:
=={{header|REXX}}==
=={{header|REXX}}==
Programming note: &nbsp; extra code was added to support an additional output format &nbsp; (see the 2<sup>nd</sup> '''output''' section).
Programming note: &nbsp; extra code was added to support an additional output format &nbsp; (see the 2<sup>nd</sup> '''output''' section).
<lang rexx>/*REXX pgm shows N ints>0 that are squares and not cubes, & which are squares and cubes.*/
<syntaxhighlight 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. */
numeric digits 20 /*ensure handling of larger numbers. */
parse arg N . /*obtain optional argument from the CL.*/
parse arg N . /*obtain optional argument from the CL.*/
Line 2,762: Line 2,762:
exit 0 /*stick a fork in it, we're all done. */
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 ?</lang>
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 2,863: Line 2,863:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Square but not cube
# Project : Square but not cube


Line 2,888: Line 2,888:
next
next
return 0
return 0
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,928: Line 2,928:
=={{header|Ruby}}==
=={{header|Ruby}}==
==== Class based ====
==== Class based ====
<lang ruby>#!/usr/bin/env ruby
<syntaxhighlight lang="ruby">#!/usr/bin/env ruby


class PowIt
class PowIt
Line 2,980: Line 2,980:


puts "Square-and-cubes:"
puts "Square-and-cubes:"
puts hexponents.join(" ")</lang>
puts hexponents.join(" ")</syntaxhighlight>
{{out}}
{{out}}
<pre>Squares:
<pre>Squares:
Line 2,987: Line 2,987:
1 64 729</pre>
1 64 729</pre>
==== Enumerator ====
==== Enumerator ====
<lang ruby>squares = Enumerator.new {|y| 1.step{|n| y << n*n} }
<syntaxhighlight lang="ruby">squares = Enumerator.new {|y| 1.step{|n| y << n*n} }


puts "Square cubes: %p
puts "Square cubes: %p
Square non-cubes: %p" % squares.take(33).partition{|sq| Math.cbrt(sq).to_i ** 3 == sq }</lang>
Square non-cubes: %p" % squares.take(33).partition{|sq| Math.cbrt(sq).to_i ** 3 == sq }</syntaxhighlight>
{{out}}
{{out}}
<pre>Square cubes: [1, 64, 729]
<pre>Square cubes: [1, 64, 729]
Line 2,997: Line 2,997:


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn main() {
<syntaxhighlight lang="rust">fn main() {
let mut s = 1;
let mut s = 1;
let mut c = 1;
let mut c = 1;
Line 3,016: Line 3,016:
s += 1;
s += 1;
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,058: Line 3,058:
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.
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.


<lang scala>import spire.math.SafeLong
<syntaxhighlight lang="scala">import spire.math.SafeLong
import spire.implicits._
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 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)</lang>
def scs: LazyList[SafeLong] = LazyList.iterate(SafeLong(1))(_ + 1).map(_.pow(3)).filter(n => n.sqrt.pow(2) == n)</syntaxhighlight>


{{out}}
{{out}}
Line 3,073: Line 3,073:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>var square_and_cube = Enumerator({|f|
<syntaxhighlight lang="ruby">var square_and_cube = Enumerator({|f|
1..Inf -> each {|n| f(n**6) }
1..Inf -> each {|n| f(n**6) }
})
})
Line 3,085: Line 3,085:


say "First 15 positive integers that are both a square and a cube:"
say "First 15 positive integers that are both a square and a cube:"
say square_and_cube.first(15).join(' ')</lang>
say square_and_cube.first(15).join(' ')</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,095: Line 3,095:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>var s = 1, c = 1, cube = 1, n = 0
<syntaxhighlight lang="swift">var s = 1, c = 1, cube = 1, n = 0
while n < 30 {
while n < 30 {
let square = s * s
let square = s * s
Line 3,109: Line 3,109:
}
}
s += 1
s += 1
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,150: Line 3,150:
=={{header|Tcl}}==
=={{header|Tcl}}==
{{trans|Common Lisp}}
{{trans|Common Lisp}}
<lang tcl>proc squaregen {{i 0}} {
<syntaxhighlight lang="tcl">proc squaregen {{i 0}} {
proc squaregen "{i [incr i]}" [info body squaregen]
proc squaregen "{i [incr i]}" [info body squaregen]
expr $i * $i
expr $i * $i
Line 3,173: Line 3,173:
puts {}
puts {}
puts "Both squares and cubes:"
puts "Both squares and cubes:"
puts $cubes</lang>
puts $cubes</syntaxhighlight>


{{Out}}
{{Out}}
Line 3,186: Line 3,186:
{{works with|Korn Shell}}
{{works with|Korn Shell}}
Ksh has a built-in cube root function, making this a little simpler:
Ksh has a built-in cube root function, making this a little simpler:
<lang sh># First 30 positive integers which are squares but not cubes
<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
# also, the first 3 positive integers which are both squares and cubes
Line 3,204: Line 3,204:
print "${sq} is square and cube"
print "${sq} is square and cube"
fi
fi
done</lang>
done</syntaxhighlight>


{{Out}}
{{Out}}
Line 3,275: Line 3,275:
The Zsh solution is virtually identical to the Bash one, the main difference being the array syntax and base index.
The Zsh solution is virtually identical to the Bash one, the main difference being the array syntax and base index.


<lang sh>#!/usr/bin/env bash
<syntaxhighlight lang="sh">#!/usr/bin/env bash


main() {
main() {
Line 3,302: Line 3,302:
}
}


main "$@"</lang>
main "$@"</syntaxhighlight>


{{Out}}
{{Out}}
Line 3,315: Line 3,315:
=={{header|Visual Basic .NET}}==
=={{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.
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.
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


' flag / mask explanation:
' flag / mask explanation:
Line 3,344: Line 3,344:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre> 1 (also cube)
<pre> 1 (also cube)
Line 3,381: Line 3,381:


=={{header|VTL-2}}==
=={{header|VTL-2}}==
<lang VTL2>10 N=0
<syntaxhighlight lang="vtl2">10 N=0
20 S=1
20 S=1
30 C=1
30 C=1
Line 3,392: Line 3,392:
100 $=32
100 $=32
110 S=S+1
110 S=S+1
120 #=N<30*60</lang>
120 #=N<30*60</syntaxhighlight>
{{out}}
{{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>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}}==
=={{header|Wren}}==
<lang ecmascript>import "/math" for Math
<syntaxhighlight lang="ecmascript">import "/math" for Math
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 3,416: Line 3,416:
System.print(sqnc.take(30).toList)
System.print(sqnc.take(30).toList)
System.print("\nThe first 3 positive integers which are both squares and cubes are:")
System.print("\nThe first 3 positive integers which are both squares and cubes are:")
System.print(sqcb.take(3).toList)</lang>
System.print(sqcb.take(3).toList)</syntaxhighlight>


{{out}}
{{out}}
Line 3,428: Line 3,428:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>int C, N, N2, T;
<syntaxhighlight lang="xpl0">int C, N, N2, T;
[C:= 0; N:= 1;
[C:= 0; N:= 1;
loop [N2:= N*N;
loop [N2:= N*N;
Line 3,442: Line 3,442:
];
];
Text(0, "^m^j* are both squares and cubes.^m^j");
Text(0, "^m^j* are both squares and cubes.^m^j");
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 3,451: Line 3,451:


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>println("First 30 positive integers that are a square but not a cube:");
<syntaxhighlight lang="zkl">println("First 30 positive integers that are a square but not a cube:");
squareButNotCube:=(1).walker(*).tweak(fcn(n){
squareButNotCube:=(1).walker(*).tweak(fcn(n){
sq,cr := n*n, sq.toFloat().pow(1.0/3).round(); // cube root(64)<4
sq,cr := n*n, sq.toFloat().pow(1.0/3).round(); // cube root(64)<4
Line 3,459: Line 3,459:


println("First 15 positive integers that are both a square and a cube:");
println("First 15 positive integers that are both a square and a cube:");
println((1).walker(*).tweak((1).pow.unbind().fp1(6)).walk(15));</lang>
println((1).walker(*).tweak((1).pow.unbind().fp1(6)).walk(15));</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>