A053188 Distance from n to nearest square.
0, 0, 1, 1, 0, 1, 2, 2, 1, 0, 1, 2, 3, 3, 2, 1, 0, 1, 2, 3, 4, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4
Offset: 0
Examples
a(7)=2 since 9 is the closest square to 7 and |9-7| = 2.
Links
Programs
-
Haskell
a053188 0 = 0 a053188 n = min (n - last xs) (head ys - n) where (xs,ys) = span (< n) a000290_list -- Reinhard Zumkeller, Nov 28 2011
-
Mathematica
Flatten[Table[Abs[Nearest[Range[0,25]^2,n]-n],{n,0,120}]] (* Harvey P. Dale, Mar 14 2011 *)
-
PARI
a(n)=abs(((sqrtint(4*n) + 1)\2)^2 - n) \\ Charles R Greathouse IV, Nov 16 2022
-
Python
from math import isqrt def A053188(n): return abs(((m:=isqrt(n))+int(n-m*(m+1)>=1))**2-n) # Chai Wah Wu, Aug 03 2022
Formula
a(n) = |floor(sqrt(n) + 1/2)^2 - n|. - Ridouane Oudra, May 01 2019
a(n) <= sqrt(n). - Charles R Greathouse IV, Nov 16 2022
Comments