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 std.stdio, std.math, std.algorithm, std.range;
 
int nonSquare(in int n) pure nothrow @safe @nogc {
    return n + cast(int)(0.5 + real(n).sqrt);
}
 
void main() {
    iota(1, 23).map!nonSquare.writeln;
 
    foreach (immutable i; 1 .. 1_000_000) {
        immutable ns = i.nonSquare;
        assert(ns != (cast(int)real(ns).sqrt) ^^ 2);
    }
}