-1

Need hints/solution to solve for a in terms of n in the equation:

$$a = \sqrt{n} + \sqrt{a}$$


I'm actually trying to get and solve the recurrence for the following piece of code:

while (n > 1)
{
    n = (long)Math.Sqrt(n);
    // do something
}

I felt that for this piece of code:

$$T(n) = \sqrt{n} + \sqrt{T(n)}$$

and hence arrived at the equation above by writing T(n) = a.

  • 1
    Did you mean $a = \sqrt{n} + \sqrt{a}$? – dxiv Oct 04 '16 at 02:54
  • @dxiv: Exactly. Is my question too bogus? – displayName Oct 04 '16 at 02:55
  • 3
    Hint: let $\sqrt{a} = b >= 0$ then you get a simple quadratic in $b$. P.S. I edited your question to use MathJax which looks better and is more easily understood around here. – dxiv Oct 04 '16 at 02:56
  • What have you tried? Have you tried subtracting $\sqrt a$ from both sides and squaring? Why not? You wind up with an equation with only one square root-that is progress. dxiv has a good idea as well. – Ross Millikan Oct 04 '16 at 02:56
  • @RossMillikan: Updated my question to answer your question. – displayName Oct 04 '16 at 03:01
  • @dxiv: Thanks for the edit. I have updated the question. – displayName Oct 04 '16 at 03:02
  • 1
    What you posted (a) is an equation not a recurrence, and (b) doesn't match the code fragment. – dxiv Oct 04 '16 at 03:05
  • @dxiv: Can you explain (b)? – displayName Oct 04 '16 at 03:06
  • Your edit does not show what you have tried. It is also not correct. Where does the $\sqrt{T(n)}$ come from? – Ross Millikan Oct 04 '16 at 03:06
  • n = (long)Math.Sqrt(n); would be the recurrence $T(n+1) = \lfloor \sqrt{T(n)} \rfloor$ (but of course you don't show what comes after that line of code). I seriously suggest you run the code step by step for a few iterations, understand what it's doing, and figure out what your question really is before posting. – dxiv Oct 04 '16 at 03:08
  • @RossMillikan: This equation has come from my attempt to analyze this code. I see that I'm wrong about the recurrence. I'm able to express it in words, but not in terms of variables. The run time of the code is equal to the number of times the input can be taken square root of, as it reaches 1. – displayName Oct 04 '16 at 03:11
  • @dxiv: What comes in //do something is independent of n. Hence omitted. My exact question is to understand the time complexity of the code above. Unfortunately such a question is not properly answered on SO. So, I came to MSE. – displayName Oct 04 '16 at 03:13
  • @dxiv: If you can write an answer, outlining how my approach is wrong and explaining how to solve the correct recurrence, that would be very kind. – displayName Oct 04 '16 at 03:18
  • What do you do when $\sqrt n$ is not an integer? Do you round down? Or up? I'm sorry for the aggression, but if you (think you) can express it in words but not variables you can't express it precisely. – Ross Millikan Oct 04 '16 at 03:23
  • @RossMillikan: I round up because I have to run the loop one more time. Is my simple english explanation incorrect? – displayName Oct 04 '16 at 03:24
  • @RossMillikan That looks like C notation, in which case the (long) cast truncates (rounds down a la $\lfloor x \rfloor$). P.S. [displayName] The code you posted does not round up. – dxiv Oct 04 '16 at 03:25
  • @dxiv: It's C#. n is long itself. I wasn't sure - so just read - about the cast from Double to long. Usually when calculating time complexity we aren't that strict because constants are ignored. If we are really strict, I agree the code will result in truncation of the precision and round down. – displayName Oct 04 '16 at 03:36

1 Answers1

3

Based on the comments, saying do something is fixed in time, the correct recurrence is $T(n)=1+T(\sqrt n)$ You make one run through with the variable being $\sqrt n$ and then restart. You have not specified what happens when $\sqrt n$ is not an integer. Do you round down, up, or ???. Leaving aside the rounding issues, you should think about what happens to $n=2^k$ where $k$ is a power of $2$

Ross Millikan
  • 374,822
  • Thank you for your response. That brings me to the question - how is such a recurrence solved? Hints should suffice if you mind giving away the answer. Not a homework question - Just trying to solve another question on SO. – displayName Oct 04 '16 at 03:39
  • Ok.. So T(n) = Log(Log(n)). Thanks. – displayName Oct 04 '16 at 03:48