Need help developing a function

50 Views Asked by At

I am a computer programmer and I have gotten to the point in this problem where I need to engage a mathematician to help me solve it.

I have spiral like this:

1 

222
212
222

33333
32223
32123
32223
33333

I need a function so that given f(x) it will return the length of a side of the matrix I will need.

Expected outputs:

f(1)  = 1
f(2)  = 3
f(3)  = 3
f(4)  = 3
...
f(9)  = 3
f(10) = 5

n should always be less than the result squared. I would like it to be as close as possible, meaning that the side that fits n would be the smallest possible side that will fit n.

I am sorry if that is confusing this is also my first time posting here and asking for this type of help.

Edit Also the output must be odd. Thanks.

thanks!

1

There are 1 best solutions below

2
On BEST ANSWER

I believe the function you are looking for is $$ f(n) = 2\left\lceil\frac{\sqrt{n} + 1}{2}\right\rceil - 1 $$ for $n$ a positive integer. The $k$th frame of the same numbers may be indexed by the integers $1$ for $k = 1$ and $(2k-3)^2 + 1,\ldots,(2k-1)^2$ for $k \geq 2$.