Using Recursion tree to solve recursive equations - math

𝑇(𝑛) = 𝑇(𝑛 − 1) + 𝑇 (n/2) + n
How can I solve this recursive equation with recursion tree?

Related

How to solve for X in an equation with modulus?

Fairly new to modular arithmetic and I've searched through several sources and have not figured out how to do this. I have the following equation:
s = (k * (h + x * r)) mod q
How would one solve for x? I am quite unsure of what to do with the mod q.

Solving a gradient dependet ODE in Julia

I am trying to solve the following ODE using DifferentialEquation.jl :
Where P is a matrix used for a projection. I am having a hard time imagining how to solve this problem. Is there a way to directly solve it using Julia? Or should I try and rearrange the equation by hand (which I already tried) to fit the usual differential equation format?
I already started by writing down some equations which can be found below but I am not getting very far.
function ODE(u, p, t)
g,N = p
Jacg = ForwardDiff.jacobian(g, u)
sum = zeros(size(N,1))
for i in 1:size(Jacg,1)
sum = sum + Jacg[i,:] .* (u / norm(u)) .* N[:,i]
end
Proj_N(N) * sum
nothing
end
prob = ODEProblem(ODE, u0, (0.0, 3.0), (g, N))
sol = solve(prob)
Any help is appreciated and thanks in advance.
If you want to use the out of place form you have to return the derivative, i.e.
function ODE(u, p, t)
g,N = p
Jacg = ForwardDiff.jacobian(g, u)
sum = zeros(size(N,1))
for i in 1:size(Jacg,1)
sum = sum + Jacg[i,:] .* (u / norm(u)) .* N[:,i]
end
Proj_N(N) * sum
end
I think you were just mixing up the mutating and non-mutating derivative forms.

Master Theorem & Recurrences

I want to find out how to solve the Master Theorem for this code:
unsigned long fac (unsigned long n ) {
if (n == 1 )
return 1;
else
return fact(n-1)*n;
}
So based on the fact that I have only 1 time calling itself a=1. Besides that function call there is nothing else so O(n) = 1 as well. Now I am struggling with my b. Normally the general formula is:
T(n) = a*T(n/2) + f(n)
In this case I don't divide the main problem though. The new problem has to solve just n-1. What is b now? Because my recurrence would be:
T(n) = 1*T(n-1) + O(1)
How can I use the Master Theorem now, since I don't know my exact b?
You can "cheat" by using a change of variable.
Let T(n) = S(2^n). Then the recurrence says
S(2^n) = S(2^n/2) + O(1)
which we rewrite
S(m) = S(m/2) + O(1).
By the Master theorem with a=1, b=2, the solution is logarithmic
S(m) = O(log m),
which means
T(n) = S(2^n) = O(log 2^n) = O(n).
Anyway, the recurrence is easier to solve directly, using
T(n) = T(n-1) + O(1) = T(n-2) + O(1) + O(1) = ... = T(0) + O(1) + O(1) + ... O(1) = O(n).
The Master Theorem doesn't apply to this particular recurrence relation, but that's okay - it's not supposed to apply everywhere. You most commonly see the Master Theorem show up in divide-and-conquer style recurrences where you split the input apart into blocks that are a constant fraction of the original size of the input, and in this particular case that's not what's happening.
To solve this recurrence, you'll need to use another method like the iteration method or looking at the shape of the recursion tree in a different way.

Calculating varied summation formula?

I'm trying to pragmatically emulate my calculator's summation function without the use of loops, the reason why is that they become very expensive once the function gets bloated. So far, I know of the formula n(n+1)/2, but that only works if the function looks like:
from X = 1 to 100, Σ (X), result = 5050.
Without a loop, is there a way to implement a function where:
from X = 1 to 100, Σ (X^2+X)?
EDIT: Note that the formula must account for all possible function bodies.
Thanks for the answers
The formula Σ (X^2+X) is equal to Σ (X) + Σ (X^2). You already know how to calculate Σ (X).
As for the Σ (X^2), this is known as Square Pyramidal Number. You can see a longer explanation here, but the formula is:
n3/3 + n2/2 + n/6
Together, that's
n3/3 + n2/2 + n/6 + n(n+1)/2
Or
(n3+2n)/3 + n2

Solving a recurrence equation with multiple recursive steps

I'm looking at some algorithms, and I'm trying to ascertain how multiple recursive steps are treated when forming the equation.
So exhibit A:
It is obvious to me that the recurrence equation here is: T(n) = c + 2T(n/2) which which in big O notation simplifies to O(n)
However here, we have something similar going on as well and I get the recurrence equation T(n) = n + 2T(n/2) since we have two recursive calls not unlike the first one, which in big O notation simplifies to O(n), however that is not the case here. Any input as to how to get the correct recurrence equation in this second one over here?
Any input as to how to go about solving this would be brilliant.
You might be interested in the Master Theorem:
http://en.wikipedia.org/wiki/Master_theorem
The recurrence equation T(n) = n + 2T(n/2) is Theta(n log n), which can be derived using the theorem. To do it manually, you can also assume n = 2^k and do:
T(n) = 2T(n/2) + n
= 2(2T(n/4) + n/2) + n
= (2^2)T(n/(2^2)) + 2n
= (2^2)(2T(n/(2^3)) + n/(2^2)) + 2n
= (2^3)T(n/(2^3)) + 3n
= ...
= (2^k)T(n/(2^k)) + kn
= nT(1) + n log2 n
= Theta(n log n)

Resources