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.




package require Tcl 8.5
 
set f {n {expr {$n + floor(0.5 + sqrt($n))}}}
 
for {set x 1} {$x <= 22} {incr x} {
    puts [format "%d\t%s" $x [apply $f $x]]
}
 
puts "looking for a square..."
for {set x 1} {$x <= 1000000} {incr x} {
    set y [apply $f $x]
    set s [expr {sqrt($y)}]
    if {$s == int($s)} {
        error "found a square in the sequence: $x -> $y"
    }
}
puts "done"