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.




#! /usr/bin/bc
 
scale = 20
 
define ceil(x) {
    auto intx
    intx=int(x)
    if (intx<x) intx+=1
    return intx
}
 
define floor(x) {
    return -ceil(-x)
}
 
define int(x) {
    auto old_scale, ret
    old_scale=scale
    scale=0
    ret=x/1
    scale=old_scale
    return ret
}
 
define round(x) {
    if (x<0) x-=.5 else x+=.5
    return int(x)
}
 
 
define nonsqr(n) {
  return n + round(sqrt(n))
}
 
for(i=1; i < 23; i++) {
   print nonsqr(i), "\n"
}
 
for(i=1; i < 1000000; i++) {
  j = sqrt(nonsqr(i))
  if ( j == floor(j) ) {
    print i, " square in the seq\n"
  }
}
 
quit