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.




MODULE NonSquare EXPORTS Main;
 
IMPORT IO, Fmt, Math;
 
VAR i: INTEGER;
 
PROCEDURE NonSquare(n: INTEGER): INTEGER =
  BEGIN
    RETURN n + FLOOR(0.5D0 + Math.sqrt(FLOAT(n, LONGREAL)));
  END NonSquare;
 
BEGIN
  FOR n := 1 TO 22 DO
    IO.Put(Fmt.Int(NonSquare(n)) & " ");
  END;
  IO.Put("\n");
  FOR n := 1 TO 1000000 DO
    i := NonSquare(n);
    IF i = FLOOR(Math.sqrt(FLOAT(i, LONGREAL))) THEN
      IO.Put("Found square: " & Fmt.Int(n) & "\n");
    END;
  END;
END NonSquare.