Task

Show that the following remarkable formula gives the sequence of non-square natural numbers:

            n + floor(1/2 + sqrt(n)) 

 Print out the values for    n    in the range   1   to   22
 Show that no squares occur for    n    less than one million


This is sequence   A000037   in the OEIS database.




import math, strutils
 
func nosqr(n: int): seq[int] =
  result = newSeq[int](n)
  for i in 1..n:
    result[i - 1] = i + i.float.sqrt.toInt
 
func issqr(n: int): bool =
  sqrt(float(n)).splitDecimal().floatpart < 1e-7
 
 
echo "Sequence for n = 22:"
echo nosqr(22).join(" ")
 
for i in nosqr(1_000_000 - 1):
  assert not issqr(i)
echo "\nNo squares were found for n less than 1_000_000."