3
$\begingroup$

I'm writing a function and per my experimenting I've found that my output needs to increment by 1 every time the input's distance to 1 is cut in half.

INPUT    OUTPUT
0.0      1
0.5      2
0.75     3
0.875    4
0.9375   5

So on and so forth. I know this can't be too complicated and I figure it has to be log or sqrt but it's just not clicking.

How can I solve this little math riddle ?

** EDIT **

Many good comments and answers so thanks all. The working c# came out like so

(int)((1-VAR_1) - (Mathf.Log(1-VAR_2))/Math.Log(2));
$\endgroup$
2
  • $\begingroup$ Hi, hint, try taking 1-input and playing with logs. Cheers $\endgroup$ Jan 28 at 19:11
  • 1
    $\begingroup$ Start thinking thinking of the OUTPUT as the INPUT (and fer god sake use fractions one of the biggest mistakes people make is thinking decimals are easier and less ambiguous than fractions). So $1 \to 0; 2\to \frac 12; 3\to \frac 34; 4\to \frac 78; 5\to \frac{15}{16}$. Is anything popping out at you? $\endgroup$
    – fleablood
    Jan 28 at 19:17

3 Answers 3

2
$\begingroup$

Just notice that if you reverse the roles of input and output you can see that

$$n\to f(n)=1-\frac{1}{2^{n-1}}$$ Now to switch back the inputs-outputs you need to invert this function, so the function you would be seeking is

$$m\to g(m)=1-\frac{\log(1-m)}{\log 2} $$

$\endgroup$
1
$\begingroup$

$$\text{input}=1-\left(\frac12\right)^{\text{output}-1}$$

$$\text{output}=1-\frac{\ln(1-\text{input})}{\ln(2)}$$

$\endgroup$
1
$\begingroup$

If you were to swap input/outputs you you'd have

$1\to 0$
$2 \to \frac 12$
$3\to \frac 34$
$4\to \frac 78$

And for any $k\to m$ we'd have $k+1$ to $1 - \frac {1-m}2$.

It's easy to so the pattern that the denominator is always $2^{k-1}$ and the numerator is $ ({2^{k-1} -1})$. and we have $k \to \frac {2^{k-1}-1}{2^{k-1}} = 1 - \frac 1{2^{k-1}}$.

So $k$ is our output and $1-\frac 1{2^{k-1}}$ is our input we have

So we have input expressed in terms of output. We want output expressed in terms of input.

Let $k$ be the output and $j$ be the input.

We have $j= 1-\frac 1{2^{k-1}}$. We need to reverse that to get $k = \text{something to do with }j$.

So.... solve for $k$.

$j = 1-\frac 1{2^{k-1}}$

$j-1 = -\frac 1{2^{k-1}}$

$\frac 1{2^{k-1}}=1-j$

$2^{k-1} = \frac 1{1-j}$

$\log_2 2^{k-1} = \log_2 \frac 1{1-j}$

$k-1 = \log_2\frac 1{1-j}$

$k = 1+ \log_2\frac 1{1-j} = 1-\log_2 (1-j)$.

Not that the requires $0 \le j < 1$.

If To do an example if $j = \frac {31}{32}$ we get

$k = 1-\log_2 (1-\frac {31}{32})=1-\log_2(1-\frac {31}{32})=1-\log_2(\frac 1{32})=1-\log_2 2^{-5} = 1-(-5) = 6$. Just as we expected!

And if we have an arbitrary value. So $j=\frac 23$ (meaning we'd expect $j$ to be between $2$ and $3$ [as $\frac 12 < \frac 23 < \frac 34$]) we have

$k = 1-\log_2(1-\frac 23)=1-\log_2 \frac 13 = 1-\log_2 3^{-1} = 1+\log_2 3 = 1+ \frac {\log_{10} 3}{\log_{10}2} = 1+\frac {0.47712125471966243729502790325512}{0.47712125471966243729502790325512} = 1+ 1.5849625007211561814537389439478=2.5849625007211561814537389439478$

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.