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.




include c:\cxpl\codes;          \intrinsic 'code' declarations
 
func real Floor(X);             \Truncate X toward - infinity
real X;
return float(fix(X-0.5));
 
func PerfectSq(N);              \Return 'true' if N is a perfect square
int N;
return sqrt(N)*sqrt(N) = N;
 
int N, M, M0;
[for N:= 1 to 22 do
        [IntOut(0, fix(float(N) + Floor(0.5 + sqrt(float(N)))));  ChOut(0,^ )];
CrLf(0);
M0:= 1;
for N:= 1 to 999_999 do
        [M:= fix(float(N) + Floor(0.5 + sqrt(float(N))));
        if PerfectSq(M) then [IntOut(0, M);  Crlf(0)];  \error: have square
        if M#M0+1 and not PerfectSq(M0+1) then          \error: not sequential
                [IntOut(0, M);  Crlf(0)];
        M0:= M;
        ];
]