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.




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