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.




function nonSquares(i)
 
    for n = (1:i)
 
        generatedNumber = n + floor(1/2 + sqrt(n));
 
        if mod(sqrt(generatedNumber),1)==0 %Check to see if the sqrt of the generated number is an integer
            fprintf('\n%d generates a square number: %d\n', [n,generatedNumber]);
            return
        else %If it isn't then the generated number is a square number
            if n<=22
                fprintf('%d ',generatedNumber);
            end
        end
    end
 
    fprintf('\nNo square numbers were generated for n <= %d\n',i);
 
end