Sequence of non-squares: Difference between revisions

Content added Content deleted
(Added 11l)
(Replaced "round.int" by "toInt". Simplified "issqr". Replaced "proc" by "func". Improved output.)
Line 1,609: Line 1,609:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>import math
<lang nim>import math, strutils

proc nosqr(n: int): seq[int] =
func nosqr(n: int): seq[int] =
result = newSeq[int] n
result = newSeq[int](n)
for i in 1..n:
for i in 1..n:
result[i - 1] = i + i.float.sqrt.round.int
result[i - 1] = i + i.float.sqrt.toInt
func 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


echo nosqr(22)
for i in nosqr(1_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."</lang>
assert(not issqr(i))</lang>
{{out}}
{{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>
<pre>@[2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 26, 27]</pre>