Sequence of non-squares: Difference between revisions

Replaced "round.int" by "toInt". Simplified "issqr". Replaced "proc" by "func". Improved output.
(Added 11l)
(Replaced "round.int" by "toInt". Simplified "issqr". Replaced "proc" by "func". Improved output.)
Line 1,609:
 
=={{header|Nim}}==
<lang nim>import math, strutils
 
procfunc nosqr(n: int): seq[int] =
result = newSeq[int] (n)
for i in 1..n:
result[i - 1] = i + i.float.sqrt.round.inttoInt
procfunc issqr(n: int): bool =
sqrt(float(n)).splitDecimal().floatpart < 1e-7
 
proc issqr(n: int): 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))</lang>
for i in nosqr(1_000_000):
echo "\nNo squares were found for n less than 1_000_000."</lang>
assert(not issqr(i))</lang>
{{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>
Anonymous user