Sequence of non-squares: Difference between revisions
Content deleted Content added
MaiconSoft (talk | contribs) Added Delphi example |
Not a robot (talk | contribs) Add ABC |
||
(17 intermediate revisions by 13 users not shown) | |||
Line 14:
{{trans|Python}}
<
R n + Int(floor(1/2 + sqrt(n)))
Line 27:
L.break
L.was_no_break
print(‘No squares found’)</
{{out}}
Line 34:
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}}==
<
with Ada.Text_IO; use Ada.Text_IO;
Line 59 ⟶ 75:
end if;
end loop;
end Sequence_Of_Non_Squares_Test;</
{{out}}
<pre>
Line 71 ⟶ 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}}
<
main: (
Line 89 ⟶ 105:
FI
OD
)</
{{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}}==
<
% check values of the function: f(n) = n + floor(1/2 + sqrt(n)) %
% are not squares %
Line 123 ⟶ 139:
else write( "f(n) produced a square" )
end.</
{{out}}
<pre>
Line 132 ⟶ 148:
=={{header|APL}}==
Generate the first 22 numbers:
<
NONSQUARE 22
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27</
Show there are no squares in the first million:
<
HOWMANYSQUARES NONSQUARE 1000000
0</
=={{header|AppleScript}}==
<
set values to {}
set squareCount to 0
Line 163 ⟶ 179:
end join
task() </
{{output}}
<
Number of squares (n < 1000000): 0"</
=={{header|Arturo}}==
<
n + floor 0.5 + sqrt n
Line 182 ⟶ 198:
if? empty? intersection squares nonSquares -> print "Didn't find any squares!"
else -> print "Ooops! Something went wrong!"</
{{out}}
Line 212 ⟶ 228:
=={{header|AutoHotkey}}==
ahk forum: [http://www.autohotkey.com/forum/post-276683.html#276683 discussion]
<
t .= (A_Index + floor(0.5 + sqrt(A_Index))) " "
MsgBox %t%
Line 219 ⟶ 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</
=={{header|AWK}}==
<
1 2
2 3
Line 247 ⟶ 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"}}'
$</
=={{header|BASIC}}==
{{works with|FreeBASIC}}
{{works with|RapidQ}}
<
DIM j AS Double
DIM found AS Integer
Line 276 ⟶ 292:
END IF
NEXT i
IF found=0 THEN PRINT "No squares found"</
=={{header|BASIC256}}==
<
print "The first 22 numbers generated by the sequence are : "
for i = 1 to 22
Line 301 ⟶ 317:
function nonSquare (n)
return n + int(0.5 + sqrt(n))
end function</
=={{header|BBC BASIC}}==
<
S% = N% + SQR(N%) + 0.5
PRINT S%
Line 316 ⟶ 332:
IF S%/R% = R% STOP
NEXT
PRINT "No squares occur for n < 1000000"</
{{out}}
<pre>
Line 350 ⟶ 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.
<
scale = 20
Line 395 ⟶ 411:
}
quit</
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|
<syntaxhighlight lang="bqn"> NonSquare ← +⟜(⌊0.5+√)
IsSquare ← =⟜⌊√
NonSquare 1+↕22
⟨ 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>
=={{header|C}}==
<
#include <stdio.h>
#include <assert.h>
Line 429 ⟶ 453:
}
return 0;
}</
=={{header|C sharp|C#}}==
<
using System.Diagnostics;
Line 456 ⟶ 480:
}
}
}</
=={{header|C++}}==
<
#include <algorithm>
#include <vector>
Line 492 ⟶ 516:
}
return 0 ;
}</
{{out}}
<pre>
Line 499 ⟶ 523:
</pre>
Alternatively, without using an external library
<syntaxhighlight lang="cpp">
#include <cmath>
#include <cstdint>
#include <iostream>
uint32_t non_square(const uint32_t& n) {
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 513 ⟶ 595:
(doseq [n (range 1 23)] (printf "%s -> %s\n" n (nonsqr n)))
(defn verify [] (not-any? square? (map nonsqr (range 1 1000000))) )</
=={{header|CLU}}==
<
return(n + real$r2i(0.5 + real$i2r(n)**0.5))
end non_square
Line 542 ⟶ 624:
|| " at n = " || int$unparse(n))
end
end start_up </
{{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 548 ⟶ 630:
=={{header|COBOL}}==
<
PROGRAM-ID. NONSQR.
Line 609 ⟶ 691:
MOVE SQR-TEMP TO SQUARE-ROOT.
COMPUTE SQR-TEMP =
(SQUARE-ROOT + SQR-INP / SQUARE-ROOT) / 2.</
{{out}}
<pre> 1: 2
Line 638 ⟶ 720:
=={{header|CoffeeScript}}==
<
non_square = (n) -> n + Math.floor(1/2 + Math.sqrt(n))
Line 658 ⟶ 740:
console.log "success"
</syntaxhighlight>
{{out}}
Line 671 ⟶ 753:
{{works with|CCL}}
<
(flet ((non-square (n)
"Compute the N-th number of the non-square sequence"
Line 686 ⟶ 768:
:when (squarep (non-square n))
:do (format t "Found a square: ~D -> ~D~%"
n (non-square n)))))</
=={{header|D}}==
<
int nonSquare(in int n) pure nothrow @safe @nogc {
Line 702 ⟶ 784:
assert(ns != (cast(int)real(ns).sqrt) ^^ 2);
}
}</
{{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>
Line 711 ⟶ 793:
{{Trans|C sharp}}
Small variation of C#
<syntaxhighlight lang="delphi">
program Sequence_of_non_squares;
Line 738 ⟶ 820:
Writeln(i, 'Is Square');
end;
end.</
{{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}}==
<
(lib 'sequences)
Line 753 ⟶ 854:
(filter square? (take A000037 1000000))
→ null
</syntaxhighlight>
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
class
APPLICATION
Line 801 ⟶ 902:
end
</syntaxhighlight>
{{out}}
<pre>
Line 839 ⟶ 940:
=={{header|Elixir}}==
<
IO.inspect for n <- 1..22, do: f.(n)
Line 850 ⟶ 951:
nil -> IO.puts "No squares found below #{n}"
val -> IO.puts "Error: number is a square: #{val}"
end</
{{out}}
Line 860 ⟶ 961:
=={{header|Erlang}}==
<
-module(non_squares).
-export([main/0]).
Line 869 ⟶ 970:
non_square(N) ->
N+trunc(1/2+ math:sqrt(N)).
</syntaxhighlight>
=={{header|Euphoria}}==
This is based on the [[BASIC]] and [[Go]] examples.
<
return n + floor( 0.5 + sqrt( n ) )
end function
Line 896 ⟶ 997:
if found = 0 then
puts( 1, "No squares found\n" )
end if</
=={{header|F_Sharp|F#}}==
<
let SequenceOfNonSquares =
Line 908 ⟶ 1,009:
|> Seq.map(fun f -> (f, nonsqr f))
|> Seq.filter(fun f -> IsSquare(snd f))
;;</
Executing the code gives:<
> SequenceOfNonSquares;;
val it : seq<int * int> = seq []</
=={{header|Factor}}==
<
sequences ;
Line 926 ⟶ 1,027:
each ;
print-first22 check-for-sq</
{{out}}
<pre>
Line 934 ⟶ 1,035:
=={{header|Fantom}}==
<
class Main
{
Line 956 ⟶ 1,057:
}
}
</syntaxhighlight>
=={{header|Forth}}==
<
: f>u f>d drop ;
Line 968 ⟶ 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</
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
IMPLICIT NONE
Line 991 ⟶ 1,092:
END DO
END PROGRAM NONSQUARES</
=={{header|FreeBASIC}}==
<
Function nonSquare (n As UInteger) As UInteger
Line 1,026 ⟶ 1,127:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,037 ⟶ 1,138:
=={{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 1,081 ⟶ 1,182:
ForAll([1 .. 1000000], i -> a() = b());
# true</
=={{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.
<
import (
Line 1,121 ⟶ 1,222:
}
fmt.Println("No squares occur for n <", limit)
}</
{{out}}
<pre>
Line 1,163 ⟶ 1,264:
Solution:
<
Test Program:
<
(1..1000000).each { assert ((nonSquare(it)**0.5 as long)**2) != nonSquare(it) }</
{{out}}
Line 1,194 ⟶ 1,295:
=={{header|Haskell}}==
<
nonsqr n = n + round (sqrt (fromIntegral n))</
> map nonsqr [1..22]
Line 1,206 ⟶ 1,307:
Or, in a point-free variation, defining a 'main' for the compiler (rather than interpreter)
<
----------------------- NON SQUARES ----------------------
Line 1,231 ⟶ 1,332:
(show . and) $
notSquare . nonSqr <$> [1 .. 1000000]
]</
{{Out}}
<pre>First 22 members of the series:
Line 1,240 ⟶ 1,341:
=={{header|HicEst}}==
<
nonSqr = $ + FLOOR(0.5 + $^0.5)
Line 1,252 ⟶ 1,353:
ENDDO
WRITE(Name) squares_found
END</
<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}}==
<
procedure main()
Line 1,271 ⟶ 1,372:
procedure nsq(n) # return non-squares
return n + floor(0.5 + sqrt(n))
end</
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/numbers.icn numbers provides floor]
=={{header|IDL}}==
<
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</
{{out}}
Line 1,291 ⟶ 1,392:
=={{header|J}}==
<
rf 1+i.22 NB. Results from 1 to 22
Line 1,297 ⟶ 1,398:
+/ (rf e. *:) 1+i.1e6 NB. Number of square RFs <= 1e6
0</
=={{header|Java}}==
<
public static int nonsqr(int n) {
return n + (int)Math.round(Math.sqrt(n));
Line 1,317 ⟶ 1,418:
}
}
}</
=={{header|JavaScript}}==
Line 1,325 ⟶ 1,426:
Iterative
<
for (var i = 1; i < 23; i++) a[i] = i + Math.floor(1/2 + Math.sqrt(i));
console.log(a);
Line 1,331 ⟶ 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");
}</
===ES6===
By functional composition
<
'use strict';
Line 1,438 ⟶ 1,539:
return main()
})();</
{{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
Line 1,446 ⟶ 1,547:
=={{header|jq}}==
{{works with|jq|1.4}}
<
def is_square: sqrt | . == floor;
Line 1,453 ⟶ 1,554:
(range(1;23) | A000037),
"Check for squares for n up to 1e6:",
(range(1;1e6+1) | A000037 | select( is_square ))</
{{out}}
<
For n up to and including 22:
2
Line 1,480 ⟶ 1,581:
27
Check for squares for n up to 1e6:
$</
=={{header|Julia}}==
<
@show nonsquare.(1:1_000_000) ∩ collect(1:1000) .^ 2</
{{out}}
<pre>nonsquare.(1:1000000) ∩ collect(1:1000) .^ 2 = Int64[]</pre>
Line 1,491 ⟶ 1,592:
=={{header|K}}==
<
nonsquare[1_!23]</
{{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>
<
+/issquare[nonsquare[1_!1000001]] / Number of squares in first million results</
{{out}}
<pre>0</pre>
=={{header|Kotlin}}==
<
fun f(n: Int) = n + Math.floor(0.5 + Math.sqrt(n.toDouble())).toInt()
Line 1,517 ⟶ 1,618:
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")
}</
{{out}}
Line 1,549 ⟶ 1,650:
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def nosquare {lambda {:n} {+ :n {floor {+ 0.5 {sqrt :n}}}}}}
-> nosquare
Line 1,563 ⟶ 1,664:
{S.serie 1 1000000}}}}
-> true
</syntaxhighlight>
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
for i = 1 to 22
print nonsqr( i); " ";
Line 1,588 ⟶ 1,689:
nonsqr = n +int( 0.5 +n^0.5)
end function
</syntaxhighlight>
<pre>
2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27
Line 1,595 ⟶ 1,696:
=={{header|Logo}}==
<
=={{header|Lua}}==
<
return n + math.floor(1/2 + math.sqrt(n))
end
Line 1,614 ⟶ 1,715:
end
end
print("No squares found")</
{{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 1,621 ⟶ 1,722:
=={{header|MAD}}==
<
BOOLEAN FOUND
FOUND = 0B
Line 1,646 ⟶ 1,747:
VECTOR VALUES FINDSQ = $5HELEM ,I5,2H, ,I5,11H, IS SQUARE*$
VECTOR VALUES NOSQ = $16HNO SQUARES FOUND*$
END OF PROGRAM</
{{out}}
Line 1,675 ⟶ 1,776:
=={{header|Maple}}==
<syntaxhighlight lang="maple">
with(NumberTheory):
Line 1,687 ⟶ 1,788:
number;
</syntaxhighlight>
{{out}}<pre>
Line 1,699 ⟶ 1,800:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
nonsq@Range[22]
If[! Or @@ (IntegerQ /@ Sqrt /@ nonsq@Range[10^6]),
Print["No squares for n <= ", 10^6]
]</
{{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 1,709 ⟶ 1,810:
=={{header|MATLAB}}==
<
for n = (1:i)
Line 1,727 ⟶ 1,828:
fprintf('\nNo square numbers were generated for n <= %d\n',i);
end</
Solution:
<
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</
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}}==
<
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,743 ⟶ 1,849:
u: makelist(i, i, 1, m)$
is(sublist(u, not_square) = sublist(map(nonsquare, u), lambda([x], x <= m)));
true</
=={{header|min}}==
{{works with|min|0.19.3}}
<
(sqrt dup floor - 0 ==) :sq?
(:n =q 1 'dup q concat 'succ concat n times pop) :upto
Line 1,753 ⟶ 1,859:
(non-sq print! " " print!) 22 upto newline
"Squares for n below one million:" puts!
(non-sq 'sq? 'puts when pop) 999999 upto</
{{out}}
<pre>
Line 1,759 ⟶ 1,865:
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</
=={{header|MMIX}}==
<
GREG @
buf OCTA 0,0
Line 1,851 ⟶ 1,980:
LDA $255,NL
TRAP 0,Fputs,StdOut
TRAP 0,Halt,0 % }</
{{out}}
<pre>~/MIX/MMIX/Rosetta> mmix SoNS
Line 1,858 ⟶ 1,987:
=={{header|Modula-3}}==
<
IMPORT IO, Fmt, Math;
Line 1,880 ⟶ 2,009:
END;
END;
END NonSquare.</
{{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}}==
<
func nosqr(n: int): seq[int] =
Line 1,901 ⟶ 2,030:
for i in nosqr(1_000_000 - 1):
assert not issqr(i)
echo "\nNo squares were found for n less than 1_000_000."</
{{out}}
<pre>Sequence for n = 22:
Line 1,909 ⟶ 2,038:
=={{header|OCaml}}==
<
val nonsqr : int -> int = <fun>
# (* first 22 values (as a list) has no squares: *)
Line 1,923 ⟶ 2,052:
assert (j <> floor j)
done;;
- : unit = ()</
=={{header|Oforth}}==
<
1000000 seq map(#[ dup sqrt 0.5 + floor + ]) conform(#[ sqrt dup floor <>]) println</
{{out}}
Line 1,938 ⟶ 2,067:
=={{header|Ol}}==
<
(import (lib math))
Line 1,959 ⟶ 2,088:
; ==> ()
</syntaxhighlight>
=={{header|Oz}}==
<
fun {NonSqr N}
N + {Float.toInt {Floor 0.5 + {Sqrt {Int.toFloat N}}}}
Line 1,978 ⟶ 2,107:
in
{Show {List.take Ns 22}}
{Show {Some Ns IsSquare}}</
=={{header|PARI/GP}}==
<
=={{header|Pascal}}==
{{libheader|Math}}
<
uses
Line 2,008 ⟶ 2,137:
writeln('square found for n = ', n);
end;
end.</
{{out}}
<pre>:> ./SequenceOfNonSquares
Line 2,016 ⟶ 2,145:
a little speedup in testing upto 1 billion.
5 secs instead of 21 secs using fpc2.6.4
<
//sequence of non-squares
//n = i + floor(1/2 + sqrt(i))
Line 2,065 ⟶ 2,194:
First22;
Test(1000*1000*1000);
end.</
=={{header|Perl}}==
<
print join(' ', map nonsqr($_), 1..22), "\n";
Line 2,075 ⟶ 2,204:
my $root = sqrt nonsqr($i);
die "Oops, nonsqr($i) is a square!" if $root == int $root;
}</
{{out}}
Line 2,081 ⟶ 2,210:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 2,103 ⟶ 2,232:
<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>
<!--</
{{out}}
<pre>
Line 2,109 ⟶ 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}}==
<
//First Task
for($i=1;$i<=22;$i++){
Line 2,131 ⟶ 2,296:
echo("Up to 1000000, found no square number in the sequence!");
}
?></
{{Out}}
<pre>>php nsqrt.php
Line 2,159 ⟶ 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}}==
<
(+ N (sqrt N T)) ) # 'sqrt' rounds when called with 'T'
Line 2,170 ⟶ 2,356:
(let (N (sqfun I) R (sqrt N))
(when (= N (* R R))
(prinl N " is square") ) ) )</
{{out}}
<pre>1 2
Line 2,196 ⟶ 2,382:
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
put skip edit ((n, n + floor(sqrt(n) + 0.5) do n = 1 to n))
(skip, 2 f(5));
</syntaxhighlight>
Results:
<syntaxhighlight lang="text">
1 2
2 3
Line 2,225 ⟶ 2,411:
20 24
21 26
</syntaxhighlight>
Test 1,000,000 values:
<syntaxhighlight lang="text">
test: proc options (main);
declare n fixed (15);
Line 2,247 ⟶ 2,433:
end test;
</syntaxhighlight>
=={{header|PostScript}}==
<syntaxhighlight lang="text">/nonsquare { dup sqrt .5 add floor add } def
/issquare { dup sqrt floor dup mul eq } def
Line 2,260 ⟶ 2,446:
} if pop
} for
</syntaxhighlight>
{{out}} (lack of error message shows none below 1000 produced a square)
<pre>
Line 2,289 ⟶ 2,475:
=={{header|PowerShell}}==
Implemented as a filter here, which can be used directly on the pipeline.
<
return $_ + [Math]::Floor(1/2 + [Math]::Sqrt($_))
}</
Printing out the first 22 values is straightforward, then:
<syntaxhighlight lang
If there were any squares for ''n'' up to one million, they would be printed with the following, but there is no output:
<
| Get-NonSquare `
| Where-Object {
$r = [Math]::Sqrt($_)
[Math]::Truncate($r) -eq $r
}</
=={{header|PureBasic}}==
<
For a = 1 To 22
; Integer, so no floor needed
Line 2,329 ⟶ 2,515:
EndIf
; Wait for enter
Input()</
=={{header|Python}}==
<
>>> def non_square(n):
return n + floor(1/2 + sqrt(n))
Line 2,351 ⟶ 2,537:
----> 2 next(filter(is_square, non_squares))
StopIteration: </
Line 2,357 ⟶ 2,543:
{{Works with|Python|3.7}}
<
from itertools import count, islice
Line 2,445 ⟶ 2,631:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>OEIS A000037
Line 2,457 ⟶ 2,643:
=={{header|Quackery}}==
<
[ dup n->v 2 vsqrt
Line 2,471 ⟶ 2,657:
[ i^ 1+ nonsquare
squarenum if 1+ ]
echo say " square numbers found"</
{{out}}
Line 2,483 ⟶ 2,669:
=={{header|R}}==
Printing the first 22 nonsquares.
<
nonsqr(1:22)</
[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.
<
{
sqrx <- sqrt(x)
Line 2,494 ⟶ 2,680:
err < 100*.Machine$double.eps
}
any(is.square(nonsqr(1:1e6)))</
[1] FALSE
=={{header|Racket}}==
<
#lang racket
Line 2,511 ⟶ 2,697:
(for/or ([n (in-range 1 1000001)])
(square? (non-square n)))
</syntaxhighlight>
{{out}}
Line 2,523 ⟶ 2,709:
{{works with|Rakudo|2016.07}}
<syntaxhighlight lang="raku"
# Print the first 22 values of the sequence
Line 2,531 ⟶ 2,717:
for 1 .. 1_000_000 -> $i {
say "Oops, nth-term($i) is square!" if (sqrt nth-term $i) %% 1;
}</
{{out}}
Line 2,537 ⟶ 2,723:
=={{header|Red}}==
<
repeat i 999'999 [
Line 2,546 ⟶ 2,732:
break
]
]</
{{out}}
<pre>
Line 2,559 ⟶ 2,745:
:::* 8 = iSqrt(64)
:::* 8 = iSqrt(65)
<
parse arg N M . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 22 /*Not specified? Then use the default.*/
Line 2,589 ⟶ 2,775:
numeric digits; parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_%2
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*/; return g</
{{out|output}}
<pre>
Line 2,623 ⟶ 2,809:
=={{header|Ring}}==
<
for n=1 to 22
x = n + floor(1/2 + sqrt(n))
Line 2,629 ⟶ 2,815:
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}}==
<
n + (0.5 + Math.sqrt(n)).floor
end
Line 2,642 ⟶ 2,847:
(squares & non_squares).each do |n|
puts "Oops, found a square f(#{non_squares.index(n)}) = #{n}"
end</
=={{header|Rust}}==
{{works with|Rust|1.1}}
<
fn f(n: i64) -> i64 {
n + (0.5 + (n as f64).sqrt()) as i64
Line 2,661 ⟶ 2,866:
println!("{} unexpected squares found", count);
}
</syntaxhighlight>
=={{header|Scala}}==
<
for(n<-1 to 22) println(n + " "+ nonsqr(n))
Line 2,672 ⟶ 2,877:
j==math.floor(j)
}
println("squares up to one million="+test)</
=={{header|Scheme}}==
<
(lambda (index)
(+ index (inexact->exact (floor (+ (/ 1 2) (sqrt index)))))))
Line 2,705 ⟶ 2,910:
(display ((any? square?) (((sequence non-squares) 1) 999999)))
(newline)</
{{out}}
(2 3 5 6 7 8 10 11 12 13 14 15 17 18 19 20 21 22 23 24 26 27)
Line 2,711 ⟶ 2,916:
=={{header|Seed7}}==
<
include "float.s7i";
include "math.s7i";
Line 2,736 ⟶ 2,941:
end if;
end for;
end func;</
=={{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}}==
<
{|i| nonsqr(i) }.map(1..22).join(' ').say
Line 2,746 ⟶ 2,973:
die "Found a square in the sequence: #{i}"
}
} << 1..1e6</
=={{header|Smalltalk}}==
<
nonSquare := [:n |
n + (n sqrt) rounded
Line 2,767 ⟶ 2,994:
].
Transcript show: 'Squares found for values up to 1,000,000: ';
show: squaresFound asString; cr</
=={{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}}==
<
val nonsqr = fn : int -> int
- List.tabulate (23, nonsqr);
Line 2,781 ⟶ 3,044:
loop 1
end;
val it = true : bool</
=={{header|Tcl}}==
<
set f {n {expr {$n + floor(0.5 + sqrt($n))}}}
Line 2,800 ⟶ 3,063:
}
}
puts "done"</
{{out}}
<pre>1 2.0
Line 2,831 ⟶ 3,094:
Definition and 1 to 22, interactively:
<
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}</
Program testing up to one million:
<
Prgm
Local i, ns
Line 2,848 ⟶ 3,111:
EndFor
Disp "Done"
EndPrgm</
(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}}==
<
LET nonSquare = n + INT(0.5 + SQR(n))
END FUNCTION
Line 2,876 ⟶ 3,164:
NEXT i
IF found = 0 THEN PRINT "No squares found"
END</
=={{header|Ursala}}==
<
#import flo
Line 2,889 ⟶ 3,177:
examples = %neALP ^(~&,nth_non_square)*t iota23
check = (is_square*~+nth_non_square*t; ~&i&& %eLP)||-[no squares found]-! iota 1000000</
{{out}}
<pre>
Line 2,919 ⟶ 3,207:
=={{header|VBA}}==
<syntaxhighlight lang="vb">
Sub Main()
Dim i&, c&, j#, s$
Line 2,938 ⟶ 3,226:
Private Function ns(l As Long) As Long
ns = l + Int(1 / 2 + Sqr(l))
End Function</
{{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,
Line 2,945 ⟶ 3,233:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
System.print("The first 22 numbers in the sequence are:")
Line 2,958 ⟶ 3,246:
if (n <= 22) Fmt.print(" $2d $2d", n, s)
}
System.print("\nNo squares were found in the first 999,999 terms.")</
{{out}}
Line 2,991 ⟶ 3,279:
=={{header|XLISP}}==
<
(+ n (floor (+ 0.5 (sqrt n)))))
Line 3,011 ⟶ 3,299:
(print (mapcar non-square (range 1 23)))
(print `(number of squares for values less than 1000000 = ,(count-squares 1 1000000)))</
{{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 3,017 ⟶ 3,305:
=={{header|XPL0}}==
<
func real Floor(X); \Truncate X toward - infinity
Line 3,039 ⟶ 3,327:
M0:= M;
];
]</
{{out}}
Line 3,048 ⟶ 3,336:
=={{header|Yabasic}}==
<
print "The first 22 numbers generated by the sequence are : "
for i = 1 to 22
Line 3,070 ⟶ 3,358:
sub nonSquare (n)
return n + int(0.5 + sqrt(n))
end sub</
=={{header|zkl}}==
<
[1..22].apply(seq).toString(*).println();
Line 3,080 ⟶ 3,368:
isSquare(25) //-->True
isSquare(26) //-->False
[2..0d1_000_000].filter(fcn(n){isSquare(seq(n))}).println();</
modf returns the integer and fractional parts of a float
{{out}}
|