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.




non_square = proc (n: int) returns (int)
    return(n + real$r2i(0.5 + real$i2r(n)**0.5))
end non_square
 
is_square = proc (n: int) returns (bool)
    return(n = real$r2i(real$i2r(n)**0.5))
end is_square
 
start_up = proc()
    po: stream := stream$primary_output()
 
    for n: int in int$from_to(1, 22) do
        stream$puts(po, int$unparse(non_square(n)) || " ")
    end 
    stream$putl(po, "")
 
    begin
        for n: int in int$from_to(1, 1000000) do
            if is_square(non_square(n)) then exit square(n) end
        end
        stream$putl(po, "No squares found up to 1000000.")
    end
    except when square(n: int):
        stream$putl(po, "Found square " || int$unparse(non_square(n))
                    || " at n = " || int$unparse(n))
    end
end start_up 