How to get the upper bound of the following equation, thanks!
2log(mn/2) + 4log(mn/4) + ... + mlog(mn/m)
This sum works out to Θ(m log n).
Let's start by rewriting
2k log (mn / 2k) = 2k(log mn - k)
Now, we have the sum
Σk=1log m 2k (log mn - k)
= Σk=1log m (2k log mn - k 2k)
= log mn Σk=1log m 2k - Σk=1log m k 2k
That first sum is the sum of a geometric series. It simplifies to 21 + log m - 2 = 2m - 2. That means that we're left with
2m log mn - 2log mn - Σk=1log m k 2k
That leaves us with the task of simplifying the sum k2k over some range. This is an arithmetico-geometric sum. If we imagine this sum ranging from 2 (inclusive) to some upper bound q, then the sum works out to q2q+1.
(q+1)2q+1 - 2 + 2(2 - 2q+1)
= (q+1)2q+1 - 2 + 4 - 2·2q+1
= (q - 1)2q+1 + 2
You can check that this formula is correct by plugging in different values of q.
In our case, q = log m, so the sum we want works out to
(log m - 1)21 + log m + 2
= (log m - 1)(2m) + 2
= 2m log m - 2m + 2
So our overall sum works out to
2m log mn - 2log mn - Σk=1log mn k 2k
= 2m log mn - 2log mn - (2m log m - 2m + 2)
= 2m log mn - 2log mn - 2m log m + 2m - 2
= 2m (log mn - log m + 1) - 2log mn - 2
= 2m (log n + 1) - 2 log mn - 2
Θ(m log n).
Hope this helps!
Related
For n binary digits with base x, the maximum value will be:
x^(n-1) + x^(n-2) + ... + x^1 + x^0
By using geometric progression,
r=1/x
Using formula for sum of n finite numbers i get:
(x^n - 1) / (x - 1).
But my answer should have been x^n - 1.(formula: M= x^n - 1)
You are correct that the sum of the geometric series x0 + x1 + x2 + ... + xn-1 is indeed (xn - 1) / (x - 1). For example, if we pick x = 10 (base 10) and n = 3 (a three-digit base 10 number), we get back
1 + 10 + 100 = (1000 - 1) / 9 = 999 / 9 = 111.
However, the largest three-digit number is 999. And by looking at the above sum, you might get a sense of why we're off by a factor of 9. When writing out numbers in base 10, we'd maximize our number by having each digit be 9, not 1. And more generally, in base x, we'd maximize our value by having each digit be x - 1. That means that the maximum value is
(x - 1)(x0 + x1 + x2 + ... + xn-1) = (x - 1)(xn - 1)/(x - 1) = xn - 1.
Here's another, easier way to see this. What is the smallest number you can make with n+1 digits? That would be xn. Since that's the smallest (n+1)-digit number, the largest n-digit number must be that minus one, giving xn - 1 without needing to discuss geometric series.
Hi I was looking up solutions to a leetcode problem, I know how to solve the problem but someone else submitted this solution and I don't understand how this works.
The question is how many number of stacks can you form with n number of coins where k-th row has k number of coins. https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3377/
Returning the above formula works, can anyone explain it to me?
The sum of the first N natural numbers (1 + 2 + 3 + ... + N) is known to be equal to N(N+1)2
The game says that if you have 6 coins you have to stack them in this way:
x (1)
x x (2)
x x x (3)
and 6 is equal to 1 + 2 + 3.
If you are given K coins and you know that N(N+1)/2 = K then you know that you can have N rows. Now the question is, given K, how can you find N?
Let's do the math:
N(N+1)/2 = K
N^2 + N = 2*K
N^2 + N -2*K = 0
N = (-1 + sqrt(1 + 8K))/2
N = -1/2 + sqrt(1/4 + 2K)
N = sqrt(2*K + 0.25) - 0.5
Recurrence relations can be directly derived from a recursive algorithm, but
they are in a form that does not allow us to quickly determine how efficient
the algorithm is.
Please how can I solve this
T(n) = 6T(n/6) + 2n + 3 for n a power of 6 T(1) = 1 solution ?
This recurrence could be solved with rsolve from SymPy, Python's symbolic math library.
from sympy import Function, rsolve
from sympy.abc import k, n
f = Function('f')
g = Function('g')
# T(n) = 6T(n/6) + 2n + 3 for n a power of 6 T(1) = 1
T = f(n) - 6*f(n/6) - 2*n - 3
Tk = T.subs({n: 6**k, f(n): g(k), f(n/6):g(k-1)})
s = rsolve(Tk, g(k), {g(0): 1})
print ("solution for k:", s.cancel())
for k in range(0,11):
print(f"k={k}, n={6**k}, T(n)={2*6**k*k + (8*6**k - 3)//5}")
This gives:
Tk(k) = 2*6**k*k + 8*6**k/5 - 3/5 or Tk(k) = ((10k+8)6k - 3)/5
T(n) = 2*n*log(n)/log(6) + 8*n/5 - 3/5 or T(n) = ((n(10log6(n)+8) - 3)/5
First 11 values:
k=0, n=1, T(n)=1
k=1, n=6, T(n)=21
k=2, n=36, T(n)=201
k=3, n=216, T(n)=1641
k=4, n=1296, T(n)=12441
k=5, n=7776, T(n)=90201
k=6, n=46656, T(n)=634521
k=7, n=279936, T(n)=4367001
k=8, n=1679616, T(n)=29561241
k=9, n=10077696, T(n)=197522841
k=10, n=60466176, T(n)=1306069401
We can check the formulas via the recursive formulation:
def recursive_t(n):
if n == 1:
res = 1
else:
t_ndiv6 = recursive_t(n//6)
res = 6 * t_ndiv6 + 2 * n + 3
print(f"T({n})={res}")
return res
recursive_t(6**10)
This prints out the same values for the same n.
I have a Computer Science Midterm tomorrow and I need help determining the complexity of a particular recursive function as below, which is much complicated than the stuffs I've already worked on: it has two variables
T(n) = 3 + mT(n-m)
In simpler cases where m is a constant, the formula can be easily obtained by writing unpacking the relation; however, in this case, unpacking doesn't make the life easier as follows (let's say T(0) = c):
T(n) = 3 + mT(n-m)
T(n-1) = 3 + mT(n-m-1)
T(n-2) = 3 + mT(n-m-2)
...
Obviously, there's no straightforward elimination according to these inequalities. So, I'm wondering whether or not I should use another technique for such cases.
Don't worry about m - this is just a constant parameter. However you're unrolling your recursion incorrectly. Each step of unrolling involves three operations:
Taking value of T with argument value, which is m less
Multiplying it by m
Adding constant 3
So, it will look like this:
T(n) = m * T(n - m) + 3 = (Step 1)
= m * (m * T(n - 2*m) + 3) + 3 = (Step 2)
= m * (m * (m * T(n - 3*m) + 3) + 3) + 3 = ... (Step 3)
and so on. Unrolling T(n) up to step k will be given by following formula:
T(n) = m^k * T(n - k*m) + 3 * (1 + m + m^2 + m^3 + ... + m^(k-1))
Now you set n - k*m = 0 to use the initial condition T(0) and get:
k = n / m
Now you need to use a formula for the sum of geometric progression - and finally you'll get a closed formula for the T(n) (I'm leaving that final step to you).
Please verify my logic to see if what I'm attempting is valid or shady.
W(n) = W(n/2) + nlg(n)
W(1) = 1
n =2^k
By trying the pattern
line 1 : W (2^k) = W(2^k-1) + nlgn
line 2 : = W(2^k-2) + nlgn + nlgn
...
line i : = W(2^k-i) + i*nlgn
and then solve the rest for i.
I just want to make sure it's cool if I substitute in 2k in one place (on line 1) but not in the other for the n lg n.
I find by subbing in 2^k for 2^k lg(2^k) gets really greasy.
Any thoughts are welcome (specifically if I should be subbing in 2^k, and if I should then how would you suggest the solution)
It's fine to switch back and forth between n and 2k as needed because you're assuming that n = 2k. However, that doesn't mean that what you have above is correct. Remember that as n decreases, the value of n log n keeps decreasing as well, so it isn't the case that the statement
line i = W(2k-i) + i * n lg n
is true.
Let's use the iteration method one more time:
W(n) = W(n / 2) + n log n
= (W(n / 4) + (n/2) log (n/2)) + n log n
= W(n / 4) + (n/2) (log n - 1) + n log n
= W(n / 4) + n log n / 2 - n / 2 + n log n
= W(n / 4) + (1 + 1/2) n log n - n / 2
= (W(n / 8) + (n / 4) log(n/4)) + (1 + 1/2) n log n - n / 2
= W(n / 8) + (n / 4) (log n - 2) + (1 + 1/2) n log n - n / 2
= W(n / 8) + n log n / 4 - n / 2 + (1 + 1/2) log n - n / 2
= W(n / 8) + (1 + 1/2 + 1/4) n log n - n
= (W(n / 16) + (n / 8) log(n/8)) + (1 + 1/2 + 1/4) n log n - n
= W(n / 16) + (n / 8) (log n - 3)) + (1 + 1/2 + 1/4) n log n - n
= W(n / 16) + n log n / 8 - 3n / 8 + (1 + 1/2 + 1/4) n log n - n
= W(n / 16) + (1 + 1/2 + 1/4 + 1/8) n log n - n - 3/8n
We can look at this to see if we spot a pattern. First, notice that the n log n term has a coefficient of (1 + 1/2 + 1/4 + 1/8 + ...) as we keep expanding outward. This series converges to 2, so when the iteration stops that term will be between n log n and 2n log n. Next, look at the coefficient of the -n term. If you look closely, you'll notice that this coefficient is -1 times
(1 / 2 + 2 / 4 + 3 / 8 + 4 / 16 + 5 / 32 + ... )
This is the sum of i / 2i, which converges to 2. Therefore, if we iterate this process, we'll find at each step that the value is Θ(n log n) - Θ(n), so the overall recurrence solves to Θ(n log n).
Hope this helps!