Find solution to pattern

83 Views Asked by At

Ok so I have this pattern

[1]0-50
[2]51-150
[3]151-300
[4]301-500
[5]501-750
[6]751-1050
[7]1051-1400
etc forever

What I need is to be able to take in the second number and get the associated first number.

Examples of answers

in      out
12  ->  1
101 ->  2
841 ->  6

I could do this using recursion(this is for a program), but I'd rather not waste cycles on it, and I'm sure there is a solution, I just don't have the mental maths tools to solve it.

2

There are 2 best solutions below

6
On BEST ANSWER

The upper boundary for group $n$ is $25(n)(n+1)$ so you could check it against that.

\begin{array}{|c|c|c|} \hline \text{Group}& 25\times(n)\times(n+1) & \text{Upper Limit}\\ \hline \text{1} & 25(1)(2) &50 \\ \hline \text{2} & 25(2)(3) &150 \\ \hline \text{3} & 25(3)(4) &300 \\ \hline \text{7} & 25(7)(8) &1400 \\ \hline \end{array}

For example, solve $841=25(n)(n+1)$ and get $n\approx5.32$ and round up to 6.

You an solve with the quadratic equation $25n^2+25n-A=0$ so $${n = \frac{{ - 25 \pm \sqrt {25^2 - 4(25)(-A)} }}{{50}}} \\{n = \frac{{ - 25 + \sqrt {625 - 100(-A)} }}{{50}}}$$

So, whatever your number is, say $A=841$, you can say, $$n=(\sqrt{(A*100)+625}-25)/50\\5.32\approx(\sqrt{(841*100)+625}-25)/50$$

Basically, $n=(\sqrt{(A*100)+625}-25)/50$ is going to be your formula and you will always round up your answer. Or use the ceiling function of it.

Also, the formula given by user2345215 works as well and is simplified.

0
On

The upper bound on the $n$-th row is $$50\cdot(1+2+\ldots+n)=50n(n+1)/2=25(n^2\!+n).$$

So if $x$ is the input, you want to find the lowest $n$ such that $$x\le 25(n^2\!{+}n)=25((n{+}1/2)^2-1/4)\iff x/25+1/4\le(n{+}1/2)^2\iff n\ge\sqrt{x/25{+}1/4}-1/2$$ Therefore the answer is: $$\left\lceil\sqrt{x/25{+}1/4}-1/2\right\rceil$$ or in C++:

ceil (sqrt (x / 25. + 1 / 4.) - 1 / 2.)