2

I have this equation

a(n) = 2^(n - 1) n

for the series

1,4,12,32,80,192,448,.....

So when n = 4, a(n) = 32

What I am looking for is to get n for a(n), but a(n) is not always in the series Example

Get n for 50?

50 is between a(4) and a(5)

In this case it will be the smaller one even if we are getting n for 447, which is between a(6) and a(7), but clearly it is much closer to a(7), I will require n =6

I will be using the equation in a computer program, so if the convertion equasion returns a fraction/decimal, that is fine, as converting it into integer will do the trick.

So basically, I need the above equation converted into

n = .....

Sorry, I don't know what tag best suits the question, please amend if you don't mind.

Mocas
  • 143

3 Answers3

1

Here is an analytic solution.

This problem is essentially asking for the inverse function for $y=x\cdot 2^{x-1},$ as if for an integer $n,$ if $x$ is such that $n=x2^{x-1},$ then the number you are looking for is the floor of $x.$

This can be written explicitly using the Lambert $W$ function, as the following: $x=W((2\ln2 )\cdot y)/\ln 2.$ How to best calculate the Lambert $W$ function is answered here.

Kenta S
  • 16,151
  • 15
  • 26
  • 53
  • Not sure what the Lambert does, does it do the flooring? I am not sure if it is implementing in c-sharp. If the reset of the equation returns the decimal, I can do the flooring – Mocas Feb 12 '20 at 21:36
  • $W((2\ln2 )n)/\ln 2$ should return a decimal, and the floor of that should be the integer you are looking for. – Kenta S Feb 12 '20 at 21:37
  • Thank you. Will see if it is implemented in c-sharp – Mocas Feb 12 '20 at 21:39
0

The 2^n part of your equation will dominate what's going on. (I broke off the -1 part to be division by 2). Taking the log base 2 of a(n) will get you pretty close. If you don't wish to muss with differential equations, you can use approximation to get what you want. Start with pretty close and then calculate the a(n) it gives you. If it's too low, try a(n+1) and if it's too high, try a(n-1). Once you have answers that are too high and too low, you can trap the correct answer between them. Try n in the middle of them like a(n+.5). Depending on if that's too high or too low, use it as the new boundary for where n really is. You can keep cutting the distance between the possible answers by half every time until you're happy with how close you are.

user78090
  • 555
0

Unless you need a large range of $n$ values, a simple solution is to tabulate $n2^{n-1}$ and find the desired number by dichotomic search.

The values that fit in a $32$ bits unsigned are

$$\begin{align}n&\to n2^n&\text{bits}\\ 0 &\to 0\\ 1 &\to 1 &\to 1 \\ 2 &\to 4 &\to 3 \\ 3 &\to 12 &\to 5 \\ 4 &\to 32 &\to 6 \\ 5 &\to 80 &\to 8 \\ 6 &\to 192 &\to 9 \\ 7 &\to 448 &\to 10 \\ 8 &\to 1024 &\to 11 \\ 9 &\to 2304 &\to 13 \\ 10 &\to 5120 &\to 14 \\ 11 &\to 11264 &\to 15 \\ 12 &\to 24576 &\to 16 \\ 13 &\to 53248 &\to 17 \\ 14 &\to 114688 &\to 18 \\ 15 &\to 245760 &\to 19 \\ 16 &\to 524288 &\to 20 \\ 17 &\to 1114112 &\to 22 \\ 18 &\to 2359296 &\to 23 \\ 19 &\to 4980736 &\to 24 \\ 20 &\to 10485760 &\to 25 \\ 21 &\to 22020096 &\to 26 \\ 22 &\to 46137344 &\to 27 \\ 23 &\to 96468992 &\to 28 \\ 24 &\to 201326592 &\to 29 \\ 25 &\to 419430400 &\to 30 \\ 26 &\to 872415232 &\to 31 \\ 27 &\to 1811939328 &\to 32 \\\end{align}$$

CAUTION: the $\text{bits}$ column is not correct.

You can even do better by relying on the number of significant bits of your number, which tell you quickly where in the table you will land. For a given number of bits, you have the choice between only two values.

Use an array indexed with the number of bits of the value and in every slot store the values of $n2^n$ that achieves at least this length, and corresponding $n$ (some of the $n$ will be repeated).