2

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 Answers2

3

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.

turkeyhundt
  • 7,733
  • Yeah I'm not sure I follow, 25 * n * (n + 1) ? Also what do you mean by Upper Boundary for group? This should work indefinatly for any input number. Oh shoot I guess you're saying that that's the reverse of what I'm looking for, but I need to reverse that equation to solve for N? I guess it's been awhile so I'm not 100% I could pull that off. – Kelly Elton Dec 19 '14 at 22:22
  • I'll elaborate in answer with an edit. – turkeyhundt Dec 19 '14 at 22:23
  • Yes, you will have to reverse the equation to solve for N. I guess you need this in a program, right? – turkeyhundt Dec 19 '14 at 22:29
  • Yeah, I'm trying...the part that trips me up is moving n+1 to the other side of the equation, or more exact the +1 – Kelly Elton Dec 19 '14 at 22:30
  • Right. It's a quadratic equation in the form of $25n^2+25n-A=0$ and you will plug in your amount for the A and solve for n. I'll add this and the quadratic equation to my answer. – turkeyhundt Dec 19 '14 at 22:33
  • Cool thanks for the hand holding /w this one. – Kelly Elton Dec 19 '14 at 22:36
2

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.)
user2345215
  • 16,422