Sequence of non-squares: Difference between revisions

Add ABC
(rearranges in order of the language.)
(Add ABC)
 
(99 intermediate revisions by 40 users not shown)
Line 1:
{{task|Arithmetic operations}}
 
;Task:
Show that the following remarkable formula gives the [http://www.research.att.com/~njas/sequences/A000037 sequence] of non-square [[wp:Natural_number|natural numbers]]:
<big> n + floor(1/2 + sqrt(n)) </big>
* Print out the values for &nbsp; <big> n </big> &nbsp; in the range &nbsp; '''1''' &nbsp; to &nbsp; '''22'''
* Show that no squares occur for &nbsp; <big> n </big> &nbsp; less than one million
 
* This sequence is also known as [http://oeis.org/A000037 A000037].
 
This is sequence &nbsp; [[oeis:A000037|A000037]] &nbsp; in the '''OEIS''' database.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F non_square(Int n)
R n + Int(floor(1/2 + sqrt(n)))
 
print_elements((1..22).map(non_square))
 
F is_square(n)
R fract(sqrt(n)) == 0
 
L(i) 1 .< 10 ^ 6
I is_square(non_square(i))
print(‘Square found ’i)
L.break
L.was_no_break
print(‘No squares found’)</syntaxhighlight>
 
{{out}}
<pre>
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
No squares found
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN non.square n:
RETURN n + floor (1/2 + root n)
HOW TO REPORT square n:
REPORT n = (floor root n)**2
 
FOR n IN {1..22}: WRITE non.square n
WRITE /
 
IF NO n IN {1..1000000} HAS square non.square n:
WRITE "No squares occur for n < 1.000.000"</syntaxhighlight>
{{out}}
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
No squares occur for n < 1.000.000</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">with Ada.Numerics.Long_Elementary_Functions;
with Ada.Text_IO; use Ada.Text_IO;
 
Line 30 ⟶ 75:
end if;
end loop;
end Sequence_Of_Non_Squares_Test;</langsyntaxhighlight>
{{out}}
<pre>
Line 42 ⟶ 87:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386}}
<langsyntaxhighlight lang="algol68">PROC non square = (INT n)INT: n + ENTIER(0.5 + sqrt(n));
 
main: (
Line 60 ⟶ 105:
FI
OD
)</langsyntaxhighlight>
{{out}}
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
% check values of the function: f(n) = n + floor(1/2 + sqrt(n)) %
% are not squares %
 
integer procedure f ( integer value n ) ;
begin
n + entier( 0.5 + sqrt( n ) )
end f ;
 
logical noSquares;
 
% first 22 values of f %
for n := 1 until 22 do writeon( i_w := 1, f( n ) );
 
% check f(n) does not produce a square for n in 1..1 000 000 %
noSquares := true;
for n := 1 until 1000000 do begin
integer fn, rn;
fn := f( n );
rn := round( sqrt( fn ) );
if ( rn * rn ) = fn then begin
write( "Found square at: ", n );
noSquares := false
end if_fn_is_a_square
end for_n ;
 
if noSquares then write( "f(n) did not produce a square in 1 .. 1 000 000" )
else write( "f(n) produced a square" )
 
end.</syntaxhighlight>
{{out}}
<pre>
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
f(n) did not produce a square in 1 .. 1 000 000
</pre>
 
=={{header|APL}}==
Generate the first 22 numbers:
<syntaxhighlight lang="apl"> NONSQUARE←{(⍳⍵)+⌊0.5+(⍳⍵)*0.5}
NONSQUARE 22
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27</syntaxhighlight>
Show there are no squares in the first million:
<syntaxhighlight lang="apl"> HOWMANYSQUARES←{+⌿⍵=(⌊⍵*0.5)*2}
HOWMANYSQUARES NONSQUARE 1000000
0</syntaxhighlight>
 
=={{header|AppleScript}}==
 
<syntaxhighlight lang="applescript">on task()
set values to {}
set squareCount to 0
repeat with n from 1 to (1000000 - 1)
set v to n + (0.5 + n ^ 0.5) div 1
if (n ≤ 22) then set end of values to v
set sqrt to v ^ 0.5
if (sqrt = sqrt as integer) then set squareCount to squareCount + 1
end repeat
return "Values (n = 1 to 22): " & join(values, ", ") & (linefeed & ¬
"Number of squares (n < 1000000): " & squareCount)
end task
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
task() </syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"Values (n = 1 to 22): 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27
Number of squares (n < 1000000): 0"</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">f: function [n]->
n + floor 0.5 + sqrt n
 
loop 1..22 'i ->
print [i "->" f i]
 
i: new 1, nonSquares: new []
while [i<1000000][ 'nonSquares ++ f i, inc 'i]
squares: map 1..1001 'x -> x ^ 2
 
if? empty? intersection squares nonSquares -> print "Didn't find any squares!"
else -> print "Ooops! Something went wrong!"</syntaxhighlight>
 
{{out}}
 
<pre>1 -> 2
2 -> 3
3 -> 5
4 -> 6
5 -> 7
6 -> 8
7 -> 10
8 -> 11
9 -> 12
10 -> 13
11 -> 14
12 -> 15
13 -> 17
14 -> 18
15 -> 19
16 -> 20
17 -> 21
18 -> 22
19 -> 23
20 -> 24
21 -> 26
22 -> 27
Didn't find any squares!</pre>
 
=={{header|AutoHotkey}}==
ahk forum: [http://www.autohotkey.com/forum/post-276683.html#276683 discussion]
<langsyntaxhighlight AutoHotkeylang="autohotkey">Loop 22
t .= (A_Index + floor(0.5 + sqrt(A_Index))) " "
MsgBox %t%
Line 73 ⟶ 235:
Loop 1000000
x := A_Index + floor(0.5 + sqrt(A_Index)), s += x = round(sqrt(x))**2
Msgbox Number of bad squares = %s% ; 0</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">$ awk 'func f(n){return(n+int(.5+sqrt(n)))}BEGIN{for(i=1;i<=22;i++)print i,f(i)}'
1 2
2 3
Line 101 ⟶ 263:
 
$ awk 'func f(n){return(n+int(.5+sqrt(n)))}BEGIN{for(i=1;i<100000;i++){n=f(i);r=int(sqrt(n));if(r*r==n)print n"is square"}}'
$</langsyntaxhighlight>
 
=={{header|BASIC}}==
{{works with|FreeBASIC}}
{{works with|RapidQ}}
<langsyntaxhighlight lang="freebasic">DIM i AS Integer
DIM j AS Double
DIM found AS Integer
Line 130 ⟶ 292:
END IF
NEXT i
IF found=0 THEN PRINT "No squares found"</langsyntaxhighlight>
 
=={{header|BASIC256}}==
<syntaxhighlight lang="freebasic"># Display first 22 values
print "The first 22 numbers generated by the sequence are : "
for i = 1 to 22
print nonSquare(i); " ";
next i
print
 
# Check for squares up to one million
found = false
for i = 1 to 1e6
j = sqrt(nonSquare(i))
if j = int(j) then
found = true
print i, " square numbers found"
exit for
end if
next i
if not found then print "No squares found"
end
 
function nonSquare (n)
return n + int(0.5 + sqrt(n))
end function</syntaxhighlight>
 
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> FOR N% = 1 TO 22
S% = N% + SQR(N%) + 0.5
PRINT S%
Line 144 ⟶ 332:
IF S%/R% = R% STOP
NEXT
PRINT "No squares occur for n < 1000000"</langsyntaxhighlight>
{{out}}
<pre>
Line 178 ⟶ 366:
Since BC is an arbitrary precision calculator, there are no issues in sqrt (it is enough to increase the scale variable upto the desired ''precision''), nor there are limits (but time) to how many non-squares we can compute.
 
<langsyntaxhighlight lang="bc">#! /usr/bin/bc
 
scale = 20
Line 223 ⟶ 411:
}
 
quit</langsyntaxhighlight>
 
The functions int, round, floor, ceil are taken from [http://www.pixelbeat.org/scripts/bc here] (int is slightly modified) ([http://www.pixelbeat.org/scripts/ Here] he states the license is GPL).
 
=={{header|BurlesqueBQN}}==
<syntaxhighlight lang="bqn"> NonSquare ← +⟜(⌊0.5+√)
IsSquare ← =⟜⌊√
 
NonSquare 1+↕22
<lang burlesque>
⟨ 2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27 ⟩
+´ IsSquare NonSquare 1+↕1e6
0</syntaxhighlight>
 
=={{header|Burlesque}}==
<syntaxhighlight lang="burlesque">
1 22r@{?s0.5?+av?+}[m
</syntaxhighlight>
</lang>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <assert.h>
Line 257 ⟶ 453:
}
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Diagnostics;
 
Line 284 ⟶ 480:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <algorithm>
#include <vector>
Line 320 ⟶ 516:
}
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 327 ⟶ 523:
</pre>
 
Alternatively, without using an external library
=={{header|Clojure}}==
<syntaxhighlight lang="cpp">
#include <cmath>
#include <cstdint>
#include <iostream>
 
uint32_t non_square(const uint32_t& n) {
<lang clojure>;; provides floor and sqrt, but we use Java's sqrt as it's faster
return n + static_cast<uint32_t>(0.5 + sqrt(n));
}
 
int main() {
std::cout << "The first 22 non-square numbers:" << std::endl;
for ( uint32_t i = 1; i <= 22; ++i ) {
std::cout << non_square(i) << " ";
}
std::cout << std::endl << std::endl;
 
uint32_t count = 0;
for ( uint32_t i = 1; i < 1'000'000; ++i ) {
double square_root = sqrt(non_square(i));
if ( square_root == floor(square_root) ) {
count++;
}
}
std::cout << "Number of squares less than 1'000'000 produced by the formula: " << count << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>The first 22 non-square numbers:
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
Number of squares less than 1'000'000 produced by the formula: 0</pre>
 
=={{header|Chipmunk Basic}}==
{{trans|BASIC256}}
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="basic">10 rem Sequence of non-squares
20 cls
30 ' Display first 22 values
40 for i = 1 to 22
50 print nonsqr(i) " ";
60 next i
70 print
80 ' Check for squares up to one million
90 found = 0
100 for i = 1 to 1000000
110 j = sqr(nonsqr(i))
120 if j = int(j) then
130 found = 1
140 print "Found square: " i
150 exit for
160 endif
170 next i
180 if found = 0 then print "No squares occur for n < 1000000"
190 end
200 sub nonsqr(n)
210 nonsqr = n+int(0.5+sqr(n))
220 return</syntaxhighlight>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">;; provides floor and sqrt, but we use Java's sqrt as it's faster
;; (Clojure's is more exact)
(use 'clojure.contrib.math)
Line 341 ⟶ 595:
(doseq [n (range 1 23)] (printf "%s -> %s\n" n (nonsqr n)))
 
(defn verify [] (not-any? square? (map nonsqr (range 1 1000000))) )</langsyntaxhighlight>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">non_square = proc (n: int) returns (int)
return(n + real$r2i(0.5 + real$i2r(n)**0.5))
end non_square
 
is_square = proc (n: int) returns (bool)
return(n = real$r2i(real$i2r(n)**0.5))
end is_square
 
start_up = proc()
po: stream := stream$primary_output()
for n: int in int$from_to(1, 22) do
stream$puts(po, int$unparse(non_square(n)) || " ")
end
stream$putl(po, "")
begin
for n: int in int$from_to(1, 1000000) do
if is_square(non_square(n)) then exit square(n) end
end
stream$putl(po, "No squares found up to 1000000.")
end
except when square(n: int):
stream$putl(po, "Found square " || int$unparse(non_square(n))
|| " at n = " || int$unparse(n))
end
end start_up </syntaxhighlight>
{{out}}
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
No squares found up to 1000000.</pre>
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. NONSQR.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NEWTON.
03 SQR-INP PIC 9(7)V9(5).
03 SQUARE-ROOT PIC 9(7)V9(5).
03 FILLER REDEFINES SQUARE-ROOT.
05 FILLER PIC 9(7).
05 FILLER PIC 9(5).
88 SQUARE VALUE ZERO.
03 SQR-TEMP PIC 9(7)V9(5).
01 SEQUENCE-VARS.
03 N PIC 9(7).
03 SEQ PIC 9(7).
01 SMALL-FMT.
03 N-O PIC Z9.
03 FILLER PIC XX VALUE ": ".
03 SEQ-O PIC Z9.
PROCEDURE DIVISION.
BEGIN.
DISPLAY "Sequence of non-squares from 1 to 22:"
PERFORM SMALL-NUMS VARYING N FROM 1 BY 1
UNTIL N IS GREATER THAN 22.
DISPLAY SPACES.
DISPLAY "Checking items up to 1 million..."
PERFORM CHECK-NONSQUARE VARYING N FROM 1 BY 1
UNTIL SQUARE OR N IS GREATER THAN 1000000.
IF SQUARE, DISPLAY "Square found at N = " N,
ELSE, DISPLAY "No squares found up to 1 million.".
STOP RUN.
SMALL-NUMS.
PERFORM NONSQUARE.
MOVE N TO N-O.
MOVE SEQ TO SEQ-O.
DISPLAY SMALL-FMT.
CHECK-NONSQUARE.
PERFORM NONSQUARE.
MOVE SEQ TO SQR-INP.
PERFORM SQRT.
NONSQUARE.
MOVE N TO SQR-INP.
PERFORM SQRT.
ADD 0.5, SQUARE-ROOT GIVING SEQ.
ADD N TO SEQ.
SQRT.
MOVE SQR-INP TO SQUARE-ROOT.
COMPUTE SQR-TEMP =
(SQUARE-ROOT + SQR-INP / SQUARE-ROOT) / 2.
PERFORM SQRT-LOOP UNTIL SQUARE-ROOT IS EQUAL TO SQR-TEMP.
SQRT-LOOP.
MOVE SQR-TEMP TO SQUARE-ROOT.
COMPUTE SQR-TEMP =
(SQUARE-ROOT + SQR-INP / SQUARE-ROOT) / 2.</syntaxhighlight>
{{out}}
<pre> 1: 2
2: 3
3: 5
4: 6
5: 7
6: 8
7: 10
8: 11
9: 12
10: 13
11: 14
12: 15
13: 17
14: 18
15: 19
16: 20
17: 21
18: 22
19: 23
20: 24
21: 26
22: 27
 
Checking items up to 1 million...
No squares found up to 1 million.</pre>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">
non_square = (n) -> n + Math.floor(1/2 + Math.sqrt(n))
 
Line 364 ⟶ 740:
 
console.log "success"
</syntaxhighlight>
</lang>
 
{{out}}
Line 377 ⟶ 753:
{{works with|CCL}}
 
<langsyntaxhighlight lang="lisp">(defun non-square-sequence ()
(flet ((non-square (n)
"Compute the N-th number of the non-square sequence"
Line 392 ⟶ 768:
:when (squarep (non-square n))
:do (format t "Found a square: ~D -> ~D~%"
n (non-square n)))))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.math, std.algorithm, std.range;
 
int nonSquare(in int n) pure nothrow @safe @nogc {
Line 408 ⟶ 784:
assert(ns != (cast(int)real(ns).sqrt) ^^ 2);
}
}</langsyntaxhighlight>
{{out}}
<pre>[2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27]</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.Math}}
{{Trans|C sharp}}
Small variation of C#
<syntaxhighlight lang="delphi">
program Sequence_of_non_squares;
 
uses
System.SysUtils, System.Math;
 
function nonsqr(i: Integer): Integer;
begin
Result := Trunc(i + Floor(0.5 + Sqrt(i)));
end;
 
var
i: Integer;
j: Double;
 
begin
 
for i := 1 to 22 do
write(nonsqr(i), ' ');
Writeln;
 
for i := 1 to 999999 do
begin
j := Sqrt(nonsqr(i));
if (j = Floor(j)) then
Writeln(i, 'Is Square');
end;
end.</syntaxhighlight>
{{out}}
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
func nonSqu n .
return n + floor (0.5 + sqrt n)
.
for i = 1 to 22
print nonSqu i
.
for i = 1 to 1e6
j = sqrt nonSqu i
if j = floor j
found = 1
.
.
if found = 0
print "No squares found"
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'sequences)
 
Line 423 ⟶ 854:
(filter square? (take A000037 1000000))
→ null
</syntaxhighlight>
</lang>
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
class
APPLICATION
Line 471 ⟶ 902:
end
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 506 ⟶ 937:
1001000
There are no squares for n equal to 1000000.
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">f = fn n -> n + trunc(0.5 + :math.sqrt(n)) end
 
IO.inspect for n <- 1..22, do: f.(n)
 
n = 1_000_000
non_squares = for i <- 1..n, do: f.(i)
m = :math.sqrt(f.(n)) |> Float.ceil |> trunc
squares = for i <- 1..m, do: i*i
case Enum.find_value(squares, fn i -> i in non_squares end) do
nil -> IO.puts "No squares found below #{n}"
val -> IO.puts "Error: number is a square: #{val}"
end</syntaxhighlight>
 
{{out}}
<pre>
[2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26,
27]
No squares found below 1000000
</pre>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">% Implemented by Arjun Sunel
-module(non_squares).
-export([main/0]).
Line 518 ⟶ 970:
non_square(N) ->
N+trunc(1/2+ math:sqrt(N)).
</syntaxhighlight>
</lang>
 
=={{header|Euphoria}}==
This is based on the [[BASIC]] and [[Go]] examples.
<langsyntaxhighlight Euphorialang="euphoria">function nonsqr( atom n)
return n + floor( 0.5 + sqrt( n ) )
end function
Line 545 ⟶ 997:
if found = 0 then
puts( 1, "No squares found\n" )
end if</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System
 
let SequenceOfNonSquares =
Line 557 ⟶ 1,009:
|> Seq.map(fun f -> (f, nonsqr f))
|> Seq.filter(fun f -> IsSquare(snd f))
;;</langsyntaxhighlight>
 
Executing the code gives:<langsyntaxhighlight lang="fsharp">
> SequenceOfNonSquares;;
val it : seq<int * int> = seq []</langsyntaxhighlight>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: kernel math math.functions math.ranges prettyprint
sequences ;
 
: non-sq ( n -- m ) dup sqrt 1/2 + floor + >integer ;
 
: print-first22 ( -- ) 22 [1,b] [ non-sq ] map . ;
 
: check-for-sq ( -- ) 1,000,000 [1,b)
[ non-sq sqrt dup floor = [ "Square found." throw ] when ]
each ;
 
print-first22 check-for-sq</syntaxhighlight>
{{out}}
<pre>
{ 2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27 }
</pre>
 
=={{header|Fantom}}==
 
<langsyntaxhighlight lang="fantom">
class Main
{
Line 587 ⟶ 1,057:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: u>f 0 d>f ;
: f>u f>d drop ;
 
Line 599 ⟶ 1,069:
: square? ( n -- ? ) u>f fsqrt fdup fround f- f0= ;
: test ( n -- ) 1 do i fn square? if cr i . ." fn was square" then loop ;
1000000 test \ ok</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">PROGRAM NONSQUARES
 
IMPLICIT NONE
Line 622 ⟶ 1,092:
END DO
 
END PROGRAM NONSQUARES</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Function nonSquare (n As UInteger) As UInteger
Return CUInt(n + Int(0.5 + Sqr(n)))
End Function
 
Function isSquare (n As UInteger) As Boolean
Dim As UInteger r = CUInt(Sqr(n))
Return n = r * r
End Function
 
Print "The first 22 numbers generated by the sequence are :"
For i As Integer = 1 To 22
Print nonSquare(i); " ";
Next
 
Print : Print
 
' Test numbers generated for n less than a million to see if they're squares
 
For i As UInteger = 1 To 999999
If isSquare(nonSquare(i)) Then
Print "The number generated by the sequence for n ="; i; " is square!"
Goto finish
End If
Next
 
Print "None of the numbers generated by the sequence for n < 1000000 are square"
 
finish:
Print
Print "Press any key to quit"
Sleep</syntaxhighlight>
 
{{out}}
<pre>
The first 22 numbers generated by the sequence are :
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
None of the numbers generated by the sequence for n < 1000000 are square
</pre>
 
=={{header|GAP}}==
<syntaxhighlight lang="text"># Here we use generators : the given formula doesn't need one, but the alternate
# non-squares function is better done with a generator.
 
Line 669 ⟶ 1,182:
 
ForAll([1 .. 1000000], i -> a() = b());
# true</langsyntaxhighlight>
 
=={{header|Go}}==
I assume it's obvious that the function monotonically increases, thus it's enough to just watch for the next possible square. If a square is found, the panic will cause an ugly stack trace.
<langsyntaxhighlight lang="go">package main
 
import (
Line 709 ⟶ 1,222:
}
fmt.Println("No squares occur for n <", limit)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 751 ⟶ 1,264:
 
Solution:
<langsyntaxhighlight lang="groovy"> def nonSquare = { long n -> n + ((1/2 + n**0.5) as long) }</langsyntaxhighlight>
 
Test Program:
<langsyntaxhighlight lang="groovy">(1..22).each { println nonSquare(it) }
(1..1000000).each { assert ((nonSquare(it)**0.5 as long)**2) != nonSquare(it) }</langsyntaxhighlight>
 
{{out}}
Line 782 ⟶ 1,295:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">nonsqr :: Integral a => a -> a
nonsqr n = n + round (sqrt (fromIntegral n))</langsyntaxhighlight>
 
> map nonsqr [1..22]
Line 790 ⟶ 1,303:
> any (\j -> j == fromIntegral (floor j)) $ map (sqrt . fromIntegral . nonsqr) [1..1000000]
False
 
 
Or, in a point-free variation, defining a 'main' for the compiler (rather than interpreter)
 
<syntaxhighlight lang="haskell">import Control.Monad (join)
 
----------------------- NON SQUARES ----------------------
 
notSquare :: Int -> Bool
notSquare = (/=) <*> (join (*) . floor . root)
 
nonSqr :: Int -> Int
nonSqr = (+) <*> (round . root)
 
root :: Int -> Float
root = sqrt . fromIntegral
 
 
-------------------------- TESTS -------------------------
main :: IO ()
main =
mapM_
putStrLn
[ "First 22 members of the series:",
unwords $ show . nonSqr <$> [1 .. 22],
"",
"All first 10E6 members non square:",
(show . and) $
notSquare . nonSqr <$> [1 .. 1000000]
]</syntaxhighlight>
{{Out}}
<pre>First 22 members of the series:
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
All first 10E6 members non square:
True</pre>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">REAL :: n=22, nonSqr(n)
 
nonSqr = $ + FLOOR(0.5 + $^0.5)
Line 804 ⟶ 1,353:
ENDDO
WRITE(Name) squares_found
END</langsyntaxhighlight>
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
squares_found=0; </pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">link numbers
 
procedure main()
Line 823 ⟶ 1,372:
procedure nsq(n) # return non-squares
return n + floor(0.5 + sqrt(n))
end</langsyntaxhighlight>
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor]
 
=={{header|IDL}}==
<langsyntaxhighlight IDLlang="idl">n = lindgen(1000000)+1 ; Take a million numbers
f = n+floor(.5+sqrt(n)) ; Apply formula
print,f[0:21] ; Output first 22
print,where(sqrt(f) eq fix(sqrt(f))) ; Test for squares</langsyntaxhighlight>
 
{{out}}
Line 843 ⟶ 1,392:
 
=={{header|J}}==
<langsyntaxhighlight lang="j"> rf=: + 0.5 <.@+ %: NB. Remarkable formula
 
rf 1+i.22 NB. Results from 1 to 22
Line 849 ⟶ 1,398:
 
+/ (rf e. *:) 1+i.1e6 NB. Number of square RFs <= 1e6
0</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class SeqNonSquares {
public static int nonsqr(int n) {
return n + (int)Math.round(Math.sqrt(n));
Line 869 ⟶ 1,418:
}
}
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
 
<lang javascript>var a = [];
===ES5===
 
Iterative
 
<syntaxhighlight lang="javascript">var a = [];
for (var i = 1; i < 23; i++) a[i] = i + Math.floor(1/2 + Math.sqrt(i));
console.log(a);
Line 878 ⟶ 1,432:
for (i = 1; i < 1000000; i++) if (Number.isInteger(i + Math.floor(1/2 + Math.sqrt(i))) === false) {
console.log("The ",i,"th element of the sequence is a square");
}</langsyntaxhighlight>
 
===ES6===
 
By functional composition
<syntaxhighlight lang="javascript">(() => {
'use strict';
 
// ------------------ OEIS A000037 -------------------
 
// nonSquare :: Int -> Int
const nonSquare = n =>
// Nth term in the OEIS A000037 series.
n + Math.floor(1 / 2 + Math.sqrt(n));
 
 
// isPerfectSquare :: Int -> Bool
const isPerfectSquare = n => {
const root = Math.sqrt(n);
return root === Math.floor(root);
};
 
// ---------------------- TEST -----------------------
const main = () =>
// First 22 terms, and test of first million.
[
Tuple('First 22 terms:')(
take(22)(
fmapGen(nonSquare)(
enumFrom(1)
)
)
),
Tuple(
'Any perfect squares in 1st 1E6 terms ?'
)(
Array.from({
length: 1E6
})
.map(nonSquare)
.some(isPerfectSquare)
)
]
.map(kv => `${fst(kv)} -> ${snd(kv)}`)
.join('\n\n');
 
 
// --------------------- GENERAL ---------------------
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
b => ({
type: 'Tuple',
'0': a,
'1': b,
length: 2
});
 
// enumFrom :: Enum a => a -> [a]
function* enumFrom(x) {
// A non-finite succession of enumerable
// values, starting with the value x.
let v = x;
while (true) {
yield v;
v = 1 + v;
}
}
 
// fmapGen <$> :: (a -> b) -> Gen [a] -> Gen [b]
const fmapGen = f =>
function* (gen) {
let v = take(1)(gen);
while (0 < v.length) {
yield(f(v[0]));
v = take(1)(gen);
}
};
 
// fst :: (a, b) -> a
const fst = tpl =>
// First member of a pair.
tpl[0];
 
 
// snd :: (a, b) -> b
const snd = tpl =>
// Second member of a pair.
tpl[1];
 
 
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = n =>
// The first n elements of a list,
// string of characters, or stream.
xs => 'GeneratorFunction' !== xs
.constructor.constructor.name ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
length: n
}, () => {
const x = xs.next();
return x.done ? [] : [x.value];
}));
 
return main()
})();</syntaxhighlight>
{{Out}}
<pre>First 22 terms: -> 2,3,5,6,7,8,10,11,12,13,14,15,17,18,19,20,21,22,23,24,26,27
 
Any perfect squares in 1st 1E6 terms ? -> false</pre>
 
=={{header|jq}}==
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq">def A000037: . + (0.5 + sqrt | floor);
 
def is_square: sqrt | . == floor;
Line 889 ⟶ 1,554:
(range(1;23) | A000037),
"Check for squares for n up to 1e6:",
(range(1;1e6+1) | A000037 | select( is_square ))</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -n -r -f sequence_of_non-squares.jq
For n up to and including 22:
2
Line 916 ⟶ 1,581:
27
Check for squares for n up to 1e6:
$</langsyntaxhighlight>
 
=={{header|Julia}}==
 
<syntaxhighlight lang="julia">nonsquare(n::Real) = n + floor(typeof(n), 0.5 + sqrt(n))
<lang Julia>[n + ifloor(1/2 + sqrt(n)) for n = 1:22]
@show nonsquare.(1:1_000_000) ∩ collect(1:1000) .^ 2</syntaxhighlight>
 
{{out}}
julia> intersect([1:1000].^2, [n + ifloor(1/2 + sqrt(n)) for n = 1:1000000])
<pre>nonsquare.(1:1000000) ∩ collect(1:1000) .^ 2 = Int64[]</pre>
0-element Array{Int64,1}
</lang>
So the set of squares of integers between 1 and 1000 and the first 1000000 terms of the given sequence is empty. Note that the given sequence is increasing and that its last term has a square root slightly less than 1000.5.
 
=={{header|K}}==
<syntaxhighlight lang="k"> nonsquare:{x+_.5+%x}
nonsquare[1_!23]</syntaxhighlight>
{{out}}
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27</pre>
<syntaxhighlight lang="k"> issquare:{(%x)=_%x}
+/issquare[nonsquare[1_!1000001]] / Number of squares in first million results</syntaxhighlight>
{{out}}
<pre>0</pre>
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1
 
fun f(n: Int) = n + Math.floor(0.5 + Math.sqrt(n.toDouble())).toInt()
 
fun main(args: Array<String>) {
println(" n f")
val squares = mutableListOf<Int>()
for (n in 1 until 1000000) {
val v1 = f(n)
val v2 = Math.sqrt(v1.toDouble()).toInt()
if (v1 == v2 * v2) squares.add(n)
if (n < 23) println("${"%2d".format(n)} : $v1")
}
println()
if (squares.size == 0) println("There are no squares for n less than one million")
else println("Squares are generated for the following values of n: $squares")
}</syntaxhighlight>
 
{{out}}
<pre>
n f
1 : 2
2 : 3
3 : 5
4 : 6
5 : 7
6 : 8
7 : 10
8 : 11
9 : 12
10 : 13
11 : 14
12 : 15
13 : 17
14 : 18
15 : 19
16 : 20
17 : 21
18 : 22
19 : 23
20 : 24
21 : 26
22 : 27
 
There are no squares for n less than one million
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def nosquare {lambda {:n} {+ :n {floor {+ 0.5 {sqrt :n}}}}}}
-> nosquare
{def issquare {lambda {:n} {= {sqrt :n} {round {sqrt :n}}}}}
-> issquare
 
{S.map nosquare {S.serie 1 22}}
-> 2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
{S.replace false by in
{S.map issquare _
{S.map nosquare
{S.serie 1 1000000}}}}
-> true
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
<lang lb>
for i = 1 to 22
print nonsqr( i); " ";
Line 950 ⟶ 1,689:
nonsqr = n +int( 0.5 +n^0.5)
end function
</syntaxhighlight>
</lang>
<pre>
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
Line 957 ⟶ 1,696:
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">repeat 22 [print sum # round sqrt #]</langsyntaxhighlight>
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">function nonSquare (n)
<lang lua>for i = 1, 22 do print(i + math.round(i^.5)) end</lang>
return n + math.floor(1/2 + math.sqrt(n))
end
 
for n = 1, 22 do
io.write(nonSquare(n) .. " ")
end
print()
local sr
for n = 1, 10^6 do
sr = math.sqrt(nonSquare(n))
if sr == math.floor(sr) then
print("Result for n = " .. n .. " is square!")
os.exit()
end
end
print("No squares found")</syntaxhighlight>
{{out}}
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
No squares found</pre>
 
=={{header|MAD}}==
 
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
BOOLEAN FOUND
FOUND = 0B
R SEQUENCE OF NON-SQUARES FORMULA
R FLOOR IS AUTOMATIC DUE TO INTEGER MATH
INTERNAL FUNCTION NONSQR.(N) = N+(.5+SQRT.(N))
R PRINT VALUES FOR 1..N..22
THROUGH SHOW, FOR N=1, 1, N.G.22
SHOW PRINT FORMAT OUTFMT,N,NONSQR.(N)
VECTOR VALUES OUTFMT = $I2,2H: ,I2*$
 
R CHECK FOR NO SQUARES UP TO ONE MILLION
THROUGH CHECK, FOR N=1, 1, N.GE.1000000
X=NONSQR.(N)
Y=SQRT.(X)
WHENEVER Y*Y.E.X
PRINT FORMAT FINDSQ,N,X
FOUND = 1B
CHECK END OF CONDITIONAL
WHENEVER .NOT. FOUND, PRINT FORMAT NOSQ
VECTOR VALUES FINDSQ = $5HELEM ,I5,2H, ,I5,11H, IS SQUARE*$
VECTOR VALUES NOSQ = $16HNO SQUARES FOUND*$
END OF PROGRAM</syntaxhighlight>
 
{{out}}
 
<pre> 1: 2
2: 3
3: 5
4: 6
5: 7
6: 8
7: 10
8: 11
9: 12
10: 13
11: 14
12: 15
13: 17
14: 18
15: 19
16: 20
17: 21
18: 22
19: 23
20: 24
21: 26
22: 27
NO SQUARES FOUND</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">
with(NumberTheory):
 
nonSquareSequence := proc(n::integer)
return n + floor(1 / 2 + sqrt(n));
end proc:
 
seq(nonSquareSequence(i), i = 1..22);
 
for number from 1 to 10^6 while not issqr(nonSquareSequence(number)) do end;
 
number;
</syntaxhighlight>
 
{{out}}<pre>
2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26,
 
27
 
1000001
 
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">nonsq = (# + Floor[0.5 + Sqrt[#]]) &;
nonsq@Range[22]
If[! Or @@ (IntegerQ /@ Sqrt /@ nonsq@Range[10^6]),
Print["No squares for n <= ", 10^6]
]</langsyntaxhighlight>
{{out}}
<pre>{2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27}
Line 973 ⟶ 1,810:
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function nonSquares(i)
 
for n = (1:i)
Line 991 ⟶ 1,828:
fprintf('\nNo square numbers were generated for n <= %d\n',i);
end</langsyntaxhighlight>
Solution:
<langsyntaxhighlight MATLABlang="matlab">>> nonSquares(1000000)
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
No square numbers were generated for n <= 1000000</langsyntaxhighlight>
 
No loops
<syntaxhighlight lang="matlab">
sum(ismember((1:1:sqrt(1e6-1)).^2,(1:1e6-1) + floor(1/2 + sqrt((1:1e6-1)))))
</syntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">nonsquare(n) := n + quotient(isqrt(100 * n) + 5, 10);
makelist(nonsquare(n), n, 1, 20);
[2,3,5,6,7,8,10,11,12,13,14,15,17,18,19,20,21,22,23,24]
Line 1,007 ⟶ 1,849:
u: makelist(i, i, 1, m)$
is(sublist(u, not_square) = sublist(map(nonsquare, u), lambda([x], x <= m)));
true</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<syntaxhighlight lang="min">(dup sqrt 0.5 + int +) :non-sq
(sqrt dup floor - 0 ==) :sq?
(:n =q 1 'dup q concat 'succ concat n times pop) :upto
 
(non-sq print! " " print!) 22 upto newline
"Squares for n below one million:" puts!
(non-sq 'sq? 'puts when pop) 999999 upto</syntaxhighlight>
{{out}}
<pre>
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
Squares for n below one million:
</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay [first22, hassquare])]
 
first22 :: [char]
first22 = show (take 22 nonsqrseq)
 
hassquare :: [char]
hassquare = "Square found", if or [issquare n | n<-take 1000000 nonsqrseq]
= "No square found", otherwise
 
issquare :: num->bool
issquare n = n == (entier (sqrt n))^2
 
nonsqrseq :: [num]
nonsqrseq = map nonsqr [1..]
 
nonsqr :: num->num
nonsqr n = n + entier (0.5 + sqrt n)</syntaxhighlight>
{{out}}
<pre>[2,3,5,6,7,8,10,11,12,13,14,15,17,18,19,20,21,22,23,24,26,27]
No square found</pre>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">1 П4 ИП4 0 , 5 ИП4 КвКор + [x]
+ С/П КИП4 БП 02</langsyntaxhighlight>
 
=={{header|MMIX}}==
<langsyntaxhighlight lang="mmix"> LOC Data_Segment
GREG @
buf OCTA 0,0
Line 1,100 ⟶ 1,980:
LDA $255,NL
TRAP 0,Fputs,StdOut
TRAP 0,Halt,0 % }</langsyntaxhighlight>
{{out}}
<pre>~/MIX/MMIX/Rosetta> mmix SoNS
Line 1,107 ⟶ 1,987:
 
=={{header|Modula-3}}==
<langsyntaxhighlight lang="modula3">MODULE NonSquare EXPORTS Main;
 
IMPORT IO, Fmt, Math;
Line 1,129 ⟶ 2,009:
END;
END;
END NonSquare.</langsyntaxhighlight>
{{out}}
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import math, strutils
 
procfunc nosqr(n: int): seq[int] =
result = @newSeq[int](n)
for i in 1..n:
result.add([i - 1] = i + round(sqrt(float(i)))).float.sqrt.toInt
func issqr(n: int): bool =
sqrt(float(n)).splitDecimal().floatpart < 1e-7
 
proc issqr(n): bool =
echo "Sequence for n = 22:"
let sqr = sqrt(float(n))
echo nosqr(22).join(" ")
let err = abs(sqr - float(round(sqr)))
err < 1e-7
 
echofor i in nosqr(221_000_000 - 1):
assert not issqr(i)
for i in nosqr(1_000_000):
echo "\nNo squares were found for n less than 1_000_000."</syntaxhighlight>
assert(not issqr(i))
</lang>
{{out}}
<pre>Sequence for n = 22:
<pre>@[2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27]</pre>
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
No squares were found for n less than 1_000_000.</pre>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml"># let nonsqr n = n + truncate (0.5 +. sqrt (float n));;
val nonsqr : int -> int = <fun>
# (* first 22 values (as a list) has no squares: *)
Line 1,168 ⟶ 2,052:
assert (j <> floor j)
done;;
- : unit = ()</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">22 seq map(#[ dup sqrt 0.5 + floor + ]) println
 
1000000 seq map(#[ dup sqrt 0.5 + floor + ]) conform(#[ sqrt dup floor <>]) println</langsyntaxhighlight>
 
{{out}}
Line 1,181 ⟶ 2,065:
1
</pre>
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
(import (lib math))
 
(print
; sequence for 1 .. 22
(map (lambda (n)
(+ n (floor (+ 1/2 (exact (sqrt n))))))
(iota 22 1)))
; ==> (2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27)
 
(print
; filter out non squares
(filter
(lambda (x)
(let ((s (floor (exact (sqrt x)))))
(= (* s s) x)))
(map (lambda (n)
(+ n (floor (+ 1/2 (exact (sqrt n))))))
(iota 1000000 1))))
; ==> ()
 
</syntaxhighlight>
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
fun {NonSqr N}
N + {Float.toInt {Floor 0.5 + {Sqrt {Int.toFloat N}}}}
Line 1,199 ⟶ 2,107:
in
{Show {List.take Ns 22}}
{Show {Some Ns IsSquare}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">[vector(22,n,n + floor(1/2 + sqrt(n))), sum(n=1,1e6,issquare(n + floor(1/2 + sqrt(n))))]</langsyntaxhighlight>
 
=={{header|Pascal}}==
{{libheader|Math}}
<langsyntaxhighlight lang="pascal">Program SequenceOfNonSquares(output);
 
uses
Line 1,229 ⟶ 2,137:
writeln('square found for n = ', n);
end;
end.</langsyntaxhighlight>
{{out}}
<pre>:> ./SequenceOfNonSquares
Line 1,237 ⟶ 2,145:
a little speedup in testing upto 1 billion.
5 secs instead of 21 secs using fpc2.6.4
<langsyntaxhighlight lang="pascal">program seqNonSq;
//sequence of non-squares
//n = i + floor(1/2 + sqrt(i))
Line 1,286 ⟶ 2,194:
First22;
Test(1000*1000*1000);
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub nonsqr { my $n = shift; $n + int(0.5 + sqrt( $n)) }
 
print join(' ', map nonsqr($_), 1..22), "\n";
 
foreach my $i (1..1_000_000) {
my $jroot = sqrt( nonsqr($i));
$jdie !="Oops, intnonsqr($ji) or die "Foundis a square!" inif the$root sequence:== int $i"root;
}</langsyntaxhighlight>
 
{{out}}
=={{header|Perl 6}}==
<pre>2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27</pre>
{{works with|Rakudo Star|2010.08}}
 
<lang perl6>sub nth_term (Int $n) { $n + round sqrt $n }
 
say nth_term $_ for 1 .. 22;
 
loop (my $i = 1; $i <= 1_000_000; $i++) {
$i.&nth_term.sqrt %% 1 and say "nth_term($i) is square.";
}</lang>
With a recent Rakudo using MoarVM the last test takes under a minute, but older Rakudos may take much longer.
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence s = repeat(0,22)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
for n=1 to length(s) do
s[n] = n + floor(1/2 + sqrt(n))
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">22</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
?s
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
integer nxt = 2, snxt = nxt*nxt, k
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
for n=1 to 1000000 do
<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;">"%V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
k = n + floor(1/2 + sqrt(n))
<span style="color: #004080;">integer</span> <span style="color: #000000;">nxt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">snxt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nxt</span><span style="color: #0000FF;">*</span><span style="color: #000000;">nxt</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">k</span>
if k>snxt then
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1000000</span> <span style="color: #008080;">do</span>
-- printf(1,"%d didn't occur\n",snxt)
<span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))</span>
nxt += 1
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">></span><span style="color: #000000;">snxt</span> <span style="color: #008080;">then</span>
snxt = nxt*nxt
<span style="color: #000080;font-style:italic;">-- printf(1,"%d didn't occur\n",snxt)</span>
end if
<span style="color: #000000;">nxt</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if k=snxt then
<span style="color: #000000;">snxt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nxt</span><span style="color: #0000FF;">*</span><span style="color: #000000;">nxt</span>
puts(1,"error!!\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">snxt</span> <span style="color: #008080;">then</span>
end for
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"error!!\n"</span><span style="color: #0000FF;">)</span>
puts(1,"none found ")
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
?{nxt,snxt}</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"none found "</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?{</span><span style="color: #000000;">nxt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">snxt</span><span style="color: #0000FF;">}</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,333 ⟶ 2,238:
none found {1001,1002001}
</pre>
 
=={{header|Phixmonti}}==
<syntaxhighlight lang="Phixmonti">include ..\Utilitys.pmt
 
def non-sq dup sqrt 0.5 + int + enddef
 
22 for dup print ", " print non-sq ? endfor
 
1000000 for
non-sq sqrt dup int == if "Square found." ? exitfor endif
endfor</syntaxhighlight>
{{out}}
<pre>1, 2
2, 3
3, 5
4, 6
5, 7
6, 8
7, 10
8, 11
9, 12
10, 13
11, 14
12, 15
13, 17
14, 18
15, 19
16, 20
17, 21
18, 22
19, 23
20, 24
21, 26
22, 27
 
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
//First Task
for($i=1;$i<=22;$i++){
Line 1,355 ⟶ 2,296:
echo("Up to 1000000, found no square number in the sequence!");
}
?></langsyntaxhighlight>
{{Out}}
<pre>>php nsqrt.php
Line 1,383 ⟶ 2,324:
Up to 1000000, found no square number in the sequence!
></pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
println([f(I) : I in 1..22]),
nl,
check(1_000_000),
nl.
 
% The formula
f(N) = N + floor(1/2 + sqrt(N)).
 
check(Limit) =>
Squares = new_map([I*I=1:I in 1..sqrt(Limit)]),
Check = [[I,T] : I in 1..Limit-1, T=f(I), Squares.has_key(T)],
println(check=Check.len).</syntaxhighlight>
 
{{out}}
<pre>[2,3,5,6,7,8,10,11,12,13,14,15,17,18,19,20,21,22,23,24,26,27]
 
check = 0</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de sqfun (N)
(+ N (sqrt N T)) ) # 'sqrt' rounds when called with 'T'
 
Line 1,394 ⟶ 2,356:
(let (N (sqfun I) R (sqrt N))
(when (= N (* R R))
(prinl N " is square") ) ) )</langsyntaxhighlight>
{{out}}
<pre>1 2
Line 1,420 ⟶ 2,382:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
put skip edit ((n, n + floor(sqrt(n) + 0.5) do n = 1 to n))
(skip, 2 f(5));
</syntaxhighlight>
</lang>
 
Results:
 
<syntaxhighlight lang="text">
1 2
2 3
Line 1,449 ⟶ 2,411:
20 24
21 26
</syntaxhighlight>
</lang>
 
Test 1,000,000 values:
 
<syntaxhighlight lang="text">
test: proc options (main);
declare n fixed (15);
Line 1,471 ⟶ 2,433:
 
end test;
</syntaxhighlight>
</lang>
 
=={{header|PostScript}}==
<syntaxhighlight lang="text">/nonsquare { dup sqrt .5 add floor add } def
/issquare { dup sqrt floor dup mul eq } def
 
Line 1,484 ⟶ 2,446:
} if pop
} for
</syntaxhighlight>
</lang>
{{out}} (lack of error message shows none below 1000 produced a square)
<pre>
Line 1,513 ⟶ 2,475:
=={{header|PowerShell}}==
Implemented as a filter here, which can be used directly on the pipeline.
<langsyntaxhighlight lang="powershell">filter Get-NonSquare {
return $_ + [Math]::Floor(1/2 + [Math]::Sqrt($_))
}</langsyntaxhighlight>
Printing out the first 22 values is straightforward, then:
<syntaxhighlight lang ="powershell">1..22 | Get-NonSquare</langsyntaxhighlight>
If there were any squares for ''n'' up to one million, they would be printed with the following, but there is no output:
<langsyntaxhighlight lang="powershell">1..1000000 `
| Get-NonSquare `
| Where-Object {
$r = [Math]::Sqrt($_)
[Math]::Truncate($r) -eq $r
}</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">OpenConsole()
For a = 1 To 22
; Integer, so no floor needed
Line 1,553 ⟶ 2,515:
EndIf
; Wait for enter
Input()</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> from math import floor, sqrt
>>> def non_square(n):
>>> # (Using the fact that round(X) is equivalent to floor(0.5+X) for our range of X)
>>> def nonsqr(n): return n + int(roundfloor(1/2 + sqrt(n)))
 
>>> # first 22 values has no squares:
>>> print(*map(non_square, range(1, 23)))
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
>>> # first 22 values (as a list) has no squares:
>>> [nonsqr(i) for i in xrange(1,23)]
[2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27]
>>> # The following check shows no squares up to one million:
>>> fordef i in xrangeis_square(1,1000000n):
j = return sqrt(nonsqr(in).is_integer()
 
assert j != int(j), "Found a square in the sequence: %i" % i
>>> non_squares = map(non_square, range(1, 10 ** 6))
>>> next(filter(is_square, non_squares))
StopIteration Traceback (most recent call last)
<ipython-input-45-f32645fc1c0a> in <module>()
1 non_squares = map(non_square, range(1, 10 ** 6))
----> 2 next(filter(is_square, non_squares))
 
StopIteration: </syntaxhighlight>
 
 
Or, defining OEIS A000037 as a non-finite series:
 
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''Sequence of non-squares'''
 
from itertools import count, islice
from math import floor, sqrt
 
 
# A000037 :: [Int]
def A000037():
'''A non-finite series of integers.'''
return map(nonSquare, count(1))
 
 
# nonSquare :: Int -> Int
def nonSquare(n):
'''Nth term in the OEIS A000037 series.'''
return n + floor(1 / 2 + sqrt(n))
 
 
# --------------------------TEST---------------------------
# main :: IO ()
def main():
'''OEIS A000037'''
 
def first22():
'''First 22 terms'''
return take(22)(A000037())
 
def squareInFirstMillion():
'''True if any of the first 10^6 terms are perfect squares'''
return any(map(
isPerfectSquare,
take(10 ** 6)(A000037())
))
 
print(
fTable(main.__doc__)(
lambda f: '\n' + f.__doc__
)(lambda x: ' ' + showList(x))(
lambda f: f()
)([first22, squareInFirstMillion])
)
 
 
# -------------------------DISPLAY-------------------------
 
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
return s + '\n' + '\n'.join(map(
lambda x, y: y + ':\n' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
 
 
# -------------------------GENERAL-------------------------
 
# isPerfectSquare :: Int -> Bool
def isPerfectSquare(n):
'''True if n is a perfect square.'''
return sqrt(n).is_integer()
 
 
# showList :: [a] -> String
def showList(xs):
'''Compact stringification of any list value.'''
return '[' + ','.join(repr(x) for x in xs) + ']' if (
isinstance(xs, list)
) else repr(xs)
 
 
# take :: Int -> [a] -> [a]
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.
'''
return lambda xs: list(islice(xs, n))
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>OEIS A000037
 
First 22 terms:
[2,3,5,6,7,8,10,11,12,13,14,15,17,18,19,20,21,22,23,24,26,27]
 
True if any of the first 10^6 terms are perfect squares:
False</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> $ "bigrat.qky" loadfile
 
[ dup n->v 2 vsqrt
drop 1 2 v+ / + ] is nonsquare ( n --> n )
 
[ sqrt nip 0 = ] is squarenum ( n --> b )
 
say "Non-squares: "
22 times [ i^ 1+ nonsquare echo sp ]
cr cr
0
999999 times
[ i^ 1+ nonsquare
squarenum if 1+ ]
echo say " square numbers found"</syntaxhighlight>
 
{{out}}
 
<pre>Non-squares: 2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
0 square numbers found
</pre>
 
>>></lang>
 
=={{header|R}}==
Printing the first 22 nonsquares.
<langsyntaxhighlight Rlang="r">nonsqr <- function(n) n + floor(1/2 + sqrt(n))
nonsqr(1:22)</langsyntaxhighlight>
[1] 2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
Testing the first million nonsquares.
<langsyntaxhighlight Rlang="r">is.square <- function(x)
{
sqrx <- sqrt(x)
Line 1,584 ⟶ 2,680:
err < 100*.Machine$double.eps
}
any(is.square(nonsqr(1:1e6)))</langsyntaxhighlight>
[1] FALSE
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 1,601 ⟶ 2,697:
(for/or ([n (in-range 1 1000001)])
(square? (non-square n)))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,607 ⟶ 2,703:
'(2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27)
#f
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
{{works with|Rakudo|2016.07}}
<syntaxhighlight lang="raku" line>sub nth-term (Int $n) { $n + round sqrt $n }
 
# Print the first 22 values of the sequence
say (nth-term $_ for 1 .. 22);
 
# Check that the first million values of the sequence are indeed non-square
for 1 .. 1_000_000 -> $i {
say "Oops, nth-term($i) is square!" if (sqrt nth-term $i) %% 1;
}</syntaxhighlight>
 
{{out}}
<pre>(2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27)</pre>
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">Red ["Sequence of non-squares"]
 
repeat i 999'999 [
n: i + round/floor 0.5 + sqrt i
if i < 23 [prin [to-integer n ""]]
if equal? round/floor n sqrt n [
print "Square found!"
break
]
]</syntaxhighlight>
{{out}}
<pre>
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
</pre>
 
=={{header|REXX}}==
REXX has no native support for &nbsp; '''floor''' &nbsp; or &nbsp; '''sqrt''', &nbsp; so these subroutines (functionsa) are written in REXX and are included below.
 
<br><br>The &nbsp; '''isqrt''' &nbsp; is a special integer square root function, it returns the integer square root.
The &nbsp; '''iSqrt''' &nbsp; is a special integer square root function, it returns the &nbsp; ''integer'' &nbsp; root &nbsp; (and uses no floating point).
:::* &nbsp; 7 &nbsp; = &nbsp; isqrt(63)
:::* &nbsp; 87 &nbsp; = &nbsp; isqrtiSqrt(6463)
:::* &nbsp; 8 &nbsp; = &nbsp; isqrtiSqrt(6564)
:::* &nbsp; 8 &nbsp; = &nbsp; iSqrt(65)
<lang rexx>/*REXX program displays some non─square numbers (with a validation check). */
<syntaxhighlight lang="rexx">/*REXX pgm displays some non─square numbers, & also displays a validation check up to 1M*/
do j=1 for 22
parse arg N M . /*obtain optional arguments from the CL*/
say right(j, 6) right(j + floor(1/2 + sqrt(j)), 7)
if N=='' | N=="," then N= 22 /*Not specified? Then use the default.*/
if M=='' | M=="," then M= 1000000 /* " " " " " " */
say 'The first ' N " non─square numbers:" /*display a header of what's to come. */
say /* [↑] default for M is one million.*/
say center('index', 20) center("non─square numbers", 20)
say center('' , 20, "═") center('' , 20, "═")
do j=1 for N
say center(j, 20) center(j +floor(1/2 +sqrt(j)), 20)
end /*j*/
oops#= 0
do k=1 for 1000000-1M /*have it step through a million of 'em*/
n$= k + floor(.5+ sqrt(k) + .5 ) /*use the specified formula (algorithm)*/
iRoot= iSqrt($) /*··· and also use the ISQRT function.*/
iroot=isqrt(n)
if irootiRoot *iroot iRoot ==n $ then oops#=oops # + 1 /*have we found a mistook? (sic) */
end /*k*/
say; if #==0 then #= 'no' /*use gooder English for display below.*/
say
say oops'Using the formula: floor[ 1/2 + sqrt(n) ], ' # " squares found up to' " k-1 M'.'
exit /*stick a fork in it, we're all done. /* [↑] display (possible) error count.*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
floor: procedure; parse arg x; return trunc(x- (x<0) )
floor: parse arg floor_; return trunc( floor_ - (floor_ < 0) )
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
isqrt: procedure; parse arg x; x=trunc(x); r=0; q=1
iSqrt: procedure; parse arg x; #=1; r= 0; do while q# <= x; q #=q #*4; end
do while q#>1; q#=q#%4; _=x-r-q#; r=r%2; if _>=<0 then doiterate; x=_; r=r+q#; end;end return r
/*──────────────────────────────────────────────────────────────────────────────────────*/
return r /*return the integer square root of X.*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
/*────────────────────────────────────────────────────────────────────────────*/
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_%2
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
numeric digits do j=0 while h>9; numeric form; h m.j=d+6 h; if x<0 then do; x=-x; i h='i' h % 2 + 1; end /*j*/
parse value format(x,2,1,,0) 'E0'do k=j+5 with to g0 'E' _by .-1; numeric digits m.k; g= (g+x/g)*.5'e'_%2; end /*k*/; return g</syntaxhighlight>
{{out|output}}
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return (g/1)i /*make complex if X < 0.*/</lang>
'''output'''
<pre>
The first 22 non─square numbers:
1 2
2 3
3 5
4 6
5 7
6 8
7 10
8 11
9 12
10 13
11 14
12 15
13 17
14 18
15 19
16 20
17 21
18 22
19 23
20 24
21 26
22 27
 
index non─square numbers
0 squares found up to 999999
════════════════════ ════════════════════
1 2
2 3
3 5
4 6
5 7
6 8
7 10
8 11
9 12
10 13
11 14
12 15
13 17
14 18
15 19
16 20
17 21
18 22
19 23
20 24
21 26
22 27
 
Using the formula: floor[ 1/2 + sqrt(n) ], no squares found up to 1000000.
</pre>
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
for n=1 to 22
x = n + floor(1/2 + sqrt(n))
see "" + x + " "
next
see nl
</syntaxhighlight>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
≪ DUP √ 0.5 + FLOOR + ≫ ‘'''A0037'''’ STO
≪ 0 ROT ROT '''FOR''' n
'''IF''' n '''A0037''' √ FP NOT '''THEN''' 1 + '''END'''
'''NEXT''' →STR " square(s) found" +
≫ ‘'''TEST'''’ STO
2 runs were necessary to test one million numbers without waking emulator's timedog up.
≪ 1 22 '''FOR''' n n '''A0037''' + '''NEXT''' ≫ EVAL
1 500000 '''TEST'''
500001 1000000 '''TEST'''
{{out}}
<pre>
3: { 2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27 }
2: “0 square(s) found“
1: “0 square(s) found“
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang ="ruby">def f(n); n + (0.5 + Math.sqrt(n)).floor; end
n + (0.5 + Math.sqrt(n)).floor
end
 
(1..22).each { |n| pputs "#{n} #{f(n)}" }
 
non_squares = (1..1_000_000).map { |n| f(n) }
squares = (1..1001).map { |n| n**2 } # Note: 1001*1001 = 1_002_001 > 1_001_000 = f(1_000_000)
(squares & non_squares).each do |n|
failputs "Oops, found a square f(#{non_squares.index(n)}) = #{n}"
end</langsyntaxhighlight>
 
=={{header|Rust}}==
{{works with|Rust|1.1}}
<langsyntaxhighlight lang="rust">
fn f(n: i64) -> i64 {
n + (0.5 + (n as f64).sqrt()) as i64
Line 1,698 ⟶ 2,866:
println!("{} unexpected squares found", count);
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def nonsqr(n:Int)=n+math.round(math.sqrt(n)).toInt
for(n<-1 to 22) println(n + " "+ nonsqr(n))
Line 1,709 ⟶ 2,877:
j==math.floor(j)
}
println("squares up to one million="+test)</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define non-squares
(lambda (index)
(+ index (inexact->exact (floor (+ (/ 1 2) (sqrt index)))))))
Line 1,742 ⟶ 2,910:
 
(display ((any? square?) (((sequence non-squares) 1) 999999)))
(newline)</langsyntaxhighlight>
{{out}}
(2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27)
Line 1,748 ⟶ 2,916:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
include "math.s7i";
Line 1,773 ⟶ 2,941:
end if;
end for;
end func;</langsyntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program sequence_of_non_squares;
print([nonsquare n : n in [1..22]]);
 
if exists n in [1..1000000] | is_square nonsquare n then
print("Found square", nonsquare n, "at", n);
else
print("No squares found up to 1 million");
end if;
 
op is_square(n);
return (floor sqrt n)**2 = n;
end op;
 
op nonsquare(n);
return n + floor(0.5 + sqrt n);
end op;
end program;</syntaxhighlight>
{{out}}
<pre>[2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27]
No squares found up to 1 million</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func nonsqr(n) { 0.5 + n.sqrt -> floor + n };
1..22 -> map {|i| nonsqr(i) }.map(1..22).join(' ').say;
 
{ |i|
if (nonsqr(i).sqrtis_sqr) -> is_int && ({
die "Found a square in the sequence: #{i}".die;
);}
} *<< 1..1e6;</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
<syntaxhighlight lang="smalltalk">| nonSquare isSquare squaresFound |
nonSquare := [:n |
n + (n sqrt) rounded
].
isSquare := [:n |
n = (((n sqrt) asInteger) raisedTo: 2)
].
Transcript show: 'The first few non-squares:'; cr.
1 to: 22 do: [:n |
Transcript show: (nonSquare value: n) asString; cr
].
squaresFound := 0.
1 to: 1000000 do: [:n |
(isSquare value: (nonSquare value: n)) ifTrue: [
squaresFound := squaresFound + 1
]
].
Transcript show: 'Squares found for values up to 1,000,000: ';
show: squaresFound asString; cr</syntaxhighlight>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "nonsquares" );
pragma annotate( description, "Show that the following remarkable formula gives the" );
pragma annotate( description, "sequence of non-square natural numbers: n +" );
pragma annotate( description, "floor(1/2 + sqrt(n)). Print out the values for n in" );
pragma annotate( description, "the range 1 to 22. Show that no squares occur for n" );
pragma annotate( description, "less than one million." );
pragma annotate( see_also, "http://rosettacode.org/wiki/Sequence_of_non-squares" );
pragma annotate( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure nonsquares is
function is_non_square (n : positive) return positive is
begin
return n + positive (numerics.rounding(numerics.sqrt (long_float (n))));
end is_non_square;
i : positive;
begin
for n in 1..22 loop -- First 22 non-squares
put (strings.image (is_non_square (n)));
end loop;
new_line;
for n in 1..1_000_000 loop -- Check first million of
i := is_non_square (n);
if i = positive (numerics.rounding(numerics.sqrt (long_float (i))))**2 then
put_line ("Found a square:" & strings.image (n));
end if;
end loop;
end nonsquares;</syntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">- fun nonsqr n = n + round (Math.sqrt (real n));
val nonsqr = fn : int -> int
- List.tabulate (23, nonsqr);
Line 1,797 ⟶ 3,044:
loop 1
end;
val it = true : bool</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
set f {n {expr {$n + floor(0.5 + sqrt($n))}}}
Line 1,816 ⟶ 3,063:
}
}
puts "done"</langsyntaxhighlight>
{{out}}
<pre>1 2.0
Line 1,847 ⟶ 3,094:
Definition and 1 to 22, interactively:
 
<langsyntaxhighlight lang="ti89b">■ n+floor(1/2+√(n)) → f(n)
Done
■ seq(f(n),n,1,22)
{2,3,5,6,7,8,10,11,12,13,14,15,17,18,19,20,21,22,23,24,26,27}</langsyntaxhighlight>
 
Program testing up to one million:
 
<langsyntaxhighlight lang="ti89b">test()
Prgm
Local i, ns
Line 1,864 ⟶ 3,111:
EndFor
Disp "Done"
EndPrgm</langsyntaxhighlight>
 
(This program has not been run to completion.)
 
=={{header|Transd}}==
<syntaxhighlight lang="scheme">#lang transd
 
MainModule: {
nonsqr: (λ i Int()
(ret (+ i (to-Int (floor (+ 0.5 (sqrt i))))))),
 
_start: (lambda locals: d Double()
(for i in Range(1 23) do
(textout (nonsqr i) " "))
 
(for i in Range(1 1000001) do
(= d (sqrt (nonsqr i)))
(if (eq d (floor d))
(throw String("Square: " i))))
 
(textout "\n\nUp to 1 000 000 - no squares found.")
)
}</syntaxhighlight>
{{out}}
<pre>
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
 
Up to 1 000 000 - no squares found.
</pre>
 
=={{header|True BASIC}}==
<syntaxhighlight lang="qbasic">FUNCTION nonSquare (n)
LET nonSquare = n + INT(0.5 + SQR(n))
END FUNCTION
 
! Display first 22 values
PRINT "The first 22 numbers generated by the sequence are : "
FOR i = 1 TO 22
PRINT nonSquare(i); " ";
NEXT i
PRINT
 
! Check FOR squares up TO one million
LET found = 0
FOR i = 1 TO 1e6
LET j = SQR(nonSquare(i))
IF j = INT(j) THEN
LET found = 1
PRINT i, " square numbers found"
EXIT FOR
END IF
NEXT i
IF found = 0 THEN PRINT "No squares found"
END</syntaxhighlight>
 
 
=={{header|Ursala}}==
<langsyntaxhighlight Ursalalang="ursala">#import nat
#import flo
 
Line 1,878 ⟶ 3,177:
 
examples = %neALP ^(~&,nth_non_square)*t iota23
check = (is_square*~+nth_non_square*t; ~&i&& %eLP)||-[no squares found]-! iota 1000000</langsyntaxhighlight>
{{out}}
<pre>
Line 1,906 ⟶ 3,205:
no squares found
</pre>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Sub Main()
Dim i&, c&, j#, s$
Const N& = 1000000
s = "values for n in the range 1 to 22 : "
For i = 1 To 22
s = s & ns(i) & ", "
Next
For i = 1 To N
j = Sqr(ns(i))
If j = CInt(j) Then c = c + 1
Next
Debug.Print s
Debug.Print c & " squares less than " & N
End Sub
 
Private Function ns(l As Long) As Long
ns = l + Int(1 / 2 + Sqr(l))
End Function</syntaxhighlight>
{{out}}
<pre>values for n in the range 1 to 22 : 2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27,
0 squares less than 1000000</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
System.print("The first 22 numbers in the sequence are:")
System.print(" n term")
for (n in 1...1e6) {
var s = n + (0.5 + n.sqrt).floor
var ss = s.sqrt.round
if (ss * ss == s) {
Fmt.print("The $r number in the sequence $d = $d x $d is a square.", n, s, ss, ss)
return
}
if (n <= 22) Fmt.print(" $2d $2d", n, s)
}
System.print("\nNo squares were found in the first 999,999 terms.")</syntaxhighlight>
 
{{out}}
<pre>
The first 22 numbers in the sequence are:
n term
1 2
2 3
3 5
4 6
5 7
6 8
7 10
8 11
9 12
10 13
11 14
12 15
13 17
14 18
15 19
16 20
17 21
18 22
19 23
20 24
21 26
22 27
 
No squares were found in the first 999,999 terms.
</pre>
 
=={{header|XLISP}}==
<syntaxhighlight lang="lisp">(defun non-square (n)
(+ n (floor (+ 0.5 (sqrt n)))))
 
(defun range (x y)
(if (< x y)
(cons x (range (+ x 1) y))))
 
(defun squarep (x)
(= x (expt (floor (sqrt x)) 2)))
 
(defun count-squares (x y)
(define squares 0)
(if (squarep (non-square x))
(define squares (+ squares 1)))
(if (= x y)
squares
(count-squares (+ x 1) y)))
 
(print (mapcar non-square (range 1 23)))
 
(print `(number of squares for values less than 1000000 = ,(count-squares 1 1000000)))</syntaxhighlight>
{{out}}
<pre>(2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27)
(NUMBER OF SQUARES FOR VALUES LESS THAN 1000000 = 0)</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
func real Floor(X); \Truncate X toward - infinity
Line 1,930 ⟶ 3,327:
M0:= M;
];
]</langsyntaxhighlight>
 
{{out}}
Line 1,936 ⟶ 3,333:
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
</pre>
 
 
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">// Display first 22 values
print "The first 22 numbers generated by the sequence are : "
for i = 1 to 22
print nonSquare(i), " ";
next i
print
 
// Check for squares up to one million
found = false
for i = 1 to 1e6
j = sqrt(nonSquare(i))
if j = int(j) then
found = true
print i, " square numbers found" //print "Found square: ", i
break
end if
next i
if not found print "No squares found"
end
 
sub nonSquare (n)
return n + int(0.5 + sqrt(n))
end sub</syntaxhighlight>
 
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn seq(n){n + (0.5+n.toFloat().sqrt()).floor()}
[1..22].apply(seq).toString(*).println();
 
Line 1,944 ⟶ 3,368:
isSquare(25) //-->True
isSquare(26) //-->False
[2..0d1_000_000].filter(fcn(n){isSquare(seq(n))}).println();</langsyntaxhighlight>
modf returns the integer and fractional parts of a float
{{out}}
2,114

edits