Funnily familiar

The output of hilarious can be approximated as n a \sqrt{ na } , where n n is a positive integer . What is a a ?

1
2
3
4
5
6
7
8
9
def hilarious(n):
    j = 1
    i = 0
    s = 0
    while i < n:
        i += j
        s += 1
        j += 1
    return s


The answer is 2.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

Kunal Gupta
Aug 26, 2016

By the Code, we see that i i increases as the n t h nth triangular number i.e. n ( n + 1 ) 2 \frac{n(n+1)}{2} .
Hence, we need to find approximately the number of times the while loop will run that'll give the output i.e. 's'.
i.e to find a k k such that: k ( k + 1 ) 2 = n \dfrac{k(k+1)}{2}=n Solving this Quadratic Eqn. will give: k = 0.5 + 0.25 + 2 n k=-0.5+\sqrt{0.25+2n} 2 n \approx \sqrt{2n} giving a = 2 a=2

Vlad Vasilescu
Jul 8, 2018

By extending the range for i and r the result tends to 2 \boxed{2}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...