Summation of series 1 + (1+2+1) + (1+2+1+3+1+2+1) - algorithm

The function is : F(n-1) n F(n-1)
Its a type of palindrome function called Zimmer Series.
The values would be : 1, 121, 1213121, ...
I want to figure the summation of the individual digits.
1 + (1+2+1) + (1+2+1+3+1+2+1) + ...
Any help is welcome.

Breaking this down into steps, we first find out a formula for the summation of a single value of the series and then we can find out the summation of said formula.
Expanding the definition you gave and manipulating it:
F(n) = n + 2F(n-1)
F(n) = n + 2(n-1) + 22(n-2) + 23(n-3) + ... + 2n-1
2F(n) = 2n + 22(n-1) + 23(n-2) + ... + 2n-1(2) + 2n
F(n) - 2F(n) = -F(n) = n - 2 - 22 - 23 - ... - 2n
From this and using the formula for Geometric Progression we can then get an expression for a single term of the series.
F(n) = (2n + 2n-1 + ... + 2) - n
= (2n+1 - 2) - n
Now we just have to work out the summation of this expression.
G(n) = Σ F(n) = Σ (2n+1 - 2 - n)
G(n) = (2n+2 - 22) - (2n) - (n(n+1)/2)
Simplifying this should hopefully give you the answer you seek!
G(n) = (2n+2 - (n(n+5)/2) - 22)
Trying this out on a few of the terms just to double check.
G(1) = (21+2 - (1(1+5)/2) - 22)
G(1) = 1
G(2) = (22+2 - (2(2+5)/2) - 22)
G(2) = 5 = 1 + (1 + 2 + 1)
G(3) = (23+2 - (3(3+5)/2) - 22)
G(3) = 16 = 1 + (1 + 2 + 1) + (1 + 2 + 1 + 3 + 1 + 2 + 1)

EDIT: Mark Dickinson is right, I misinterpreted the question, this solution is incorrect.
I think after the second term the sequence is of the form difference in Arithmetic Progression.
Let me show you how
Second Term = 1+2+1
Third Term = 1+2+1+3 + 1+2+1
Difference = 1+2+1+3 = 7
Third Term = 1+2+1+3+1+2+1
Fourth Term = 1+2+1+3+ 1+4+1+3 +1+2+1
Difference = 1+4+1+3 = 9
Fourth Term = 1+2+1+3+1+4+1+3+1+2+1
Fifth Term = 1+2+1+3+1+4+ 1+5+1+4 +1+3+1+2+1
Difference = 1+5+1+4 = 11
So as you can see the difference is in the arithmetic progression and you find the sum of the terms using the formula for the sum of numbers whose different are in Arithmetic Progression

Related

Find formula to describe recursion in method

I am struggling with writing the formula that describes the recursive nature of the foo method.
The problem is that as far as I can tell, since every time n is divided with 2,
the binary tree formula should apply here.
This says that when in each call we divide the data we get a formula like this:
And then if we analyze for 2 so :
We get:
Which means that C(N) = log(N + 1), namely O(logN)
That all makes sense and seems to be the right choice for the foo method but it cant be because for
n = 8 I would get 3 + 1 iterations that are not n + 1 = 8 + 1 = 9 iterations
So here is your code:
void foo(int n) {
if (n == 1) System.out.println("Last line I print");
if (n > 1) {
System.out.println("I am printing one more line");
foo(n/2);
}
}
We can write a recurrence relation down for its runtime T as a function of the value of the parameter passed into it, n:
T(1) = a, a constant
T(n) = b + T(n/2), b constant, n > 1
We can write out some values of T(n) for various values of n to see if a pattern emerges:
n T(n)
---------
1 a
2 a + b
4 a + 2b
8 a + 3b
...
2^k a + kb
So for n = 2^k, T(n) = a + kb. We can solve for k in terms of n as follows:
n = 2^k <=> k = log(n)
Then we recover the expression T(n) = a + blog(n). We can verify this expression works easily:
a + blog(1) = a, as required
a + blog(n) = b + (a + blog(n/2))
= b + (a + b(log(n) - 1)
= b + a + blog(n) - b
= a + blog(n), as required
You can also use mathematical induction to do the same thing.

Solving a recurrence relation using Smoothness Rule

Consider this recurrence relation: x(n) = x(n/2) + n, for n > 1 and x(1) = 0.
Now here the method of back substitution will struggle for values of n not powers of 2, so it is best known here is to use the smoothness rule to solve this types of questions, and when we use the smoothness rule, where we will solve for n = 2^k (for n = values powers of 2) we will have a solution of x(n) = 2n - 1.
However, if we use the method of backward substitution, this recurrence relation will have a solution!
x(n) = x(n/2) + n = x(n/4) + n/2 + n = x(n/8) + n/4 + n/2 + n = x(n/16) + n/8 + n/4 + n/2 + n = ....
where the pattern is
x(n) = x(n/i) + n/(i/2) + n/(i/4) + n/(i/8) + n/(i/16) + ...
which will stop when n = 1 (i.e when i = n) and in this case
x(n) = x(n/n) + n/(n/2) + n/(n/4) + n/(n/8) + n/(n/16) + ... = 1 + 2 + 4 + 8 + 16 + ... = 2^(n+1) - 1
which is two different answers!
So please I am so confused here because in the textbook (Introduction to Analysis and Design of Algorithms by Anany Levitin) it is mention that we should use here the smoothness rule, but as you can see I have solved it exactly by the method of backward substitution where the method was expected here to struggle but nothing has happened!
The transition 1 + 2 + 4 + 8 + 16 + ... = 2^(n+1) - 1 is false.
That is since the number of elements in the left series is log n so the sum is 2^(log n + 1) - 1, which is exactly 2n - 1.
The reason there are log n elements is that n/(2^i) = 1 (the last element of the series is 1) when i = log n.

Solving a recurrence relation for a recursive function

I need to create and solve a recurrence relation for the worst-case analysis for the following psuedocode. I am counting the number additions (not including the for loop counter) as my basic operation.
I am assuming n=2^k.
Here is the progress I have made...
Base Case:
T(n<=4) = 1
W(n)=W(2^k)=additions to calculate answer+additions in next recursion+addition in for loop
W(2^k) = 2 + W(2^(k-2)) + (2^k) - 2 = W(2^(k-2)) + (2^k)
I use back substitution and get the following recurrence relation...
for the jth recursive call
W(2^k) = W(2^(k-2j)) + (2^k) + sum(t=1,j,2^(k-2(t-1)))
I know that I can simplify this because I take W(2^(k-2j)) = W(4) and solve for j to see how many recursive steps the code takes.
In this case, j=(k/2) - 1. Reducing the recurrence gives me...
W(2^k) = 1 + (2^k) + sum(t=1,j,2^(k-2(t-1))).
Reducing the summation gives me...
W(2^k) = 1 + (2^k) + (2^k)*(2^2)*sum(t=1,j,2^(-2t)) or
W(n) = 1 + n + 4n*sum(t=1,j,2^(-2t))
What I cannot simplify is the summation. In lectures, we may have a summation of sum(i=1,n,2^i), which would be 2^(n+1)-1, but this one is different.
int function calc(int n) {
int num,answer;
if(n<=4) {return n+10;}
else {
num=calc(n/4);
answer=(num+num+10);
for(int i=2;i<=n-1;i++) {
answer=answer+answer;
}
return answer;
}
}
Any help would be appreciated. This assignment is due tonight. Thanks
The time complexity of the problem is T(n) = T(n/4) + n. The term n could mean \Theta(n). Hence, T(n) = n + n/4 + n/4^2 + ... + n/(4^log_4(n)) = n(1 + 1/4 + ... + 1/n) = \Theta(n). Notice that lim_{n\to \infty} 1 + 1/4 + ... + 1/4^log_4(n) = 4/3 which is a constant number.
T(n) = T(2^k) // by assumption
T(n) = T(n/4) + n // is the recurrence relation
T(2^k) = T(2^(k-2)) + (2^k) // rewriting
T(2^k) = T(2^(k-2j)) + (2^k)*SUM(i=0;j-1;(1/4)^i) // is the relation for iteration j
T(4) = T(2^(k-2j)) = 1 // when the recursion ends, base case reached, only 1 addition
2^2 = 2^(k-2j) // rewrite and remove T
2=k-2j // remove common base
j=(k/2)-1 // solve for jth iteration
Notice: cannot have decimal for iteration, so j=CEILING((k/2)-1)
SUM(i=0;j-1;(1/4)^i) = (1-(1/4)^j)/(1-(1/4))
For geometric sums and proof of transformation above, see wiki link below
Substituting j with CEILING((k/2)-1), the summation is...
SUM = (1-(1/4)^(CEILING((k/2)-1)))/(1-(1/4))
Finally, T(2^k) = 1 + (2^k)*SUM
In terms of the input n, T(n) = 1 + (n*SUM)
This formula is good for all k>=1
Testing several values...
k=1, T(2) = 1 + (2*0) = 1 // we know this is true because T(2) is a base case
k=2, T(4) = 1 + (4*0) = 1 // we know this is true because T(4) is a base case
k=3, T(8) = 1 + (8*1) = 9 // by recurrence formula, T(8) = T(2) + 8 = 9, true
k=4, T(16) = 1 + (16*1) = 17 // by recurrence formula, T(16) = T(4) + 16 = 17, true
k=5, T(32) = 1 + (32*1.25) = 41 // by recurrence formula, T(32) = T(8) + 32, T(8) = 9, 32+9=41 true
For geometric sums
https://en.wikipedia.org/wiki/Geometric_series

time complexity of following recurrence?

Find out the time complexity (Big Oh Bound) of the recurrence T(n) = T(⌊n⌋) + T(⌈n⌉) + 1.
How the time complexity of this comes out to be O(n)??
You probably ment T(n)=T(⌊n/2⌋)+ T(⌈n/2⌉) + 1.
Lets calculate first few values of T(n).
T(1) = 1
T(2) = 3
T(3) = 5
T(4) = 7
We can guess that T(n) = 2 * n - 1.
Lets prove that by mathematical induction
Basis
T(1) = 1
T(2) = 3
T(3) = 5
T(4) = 7
Inductive step
T(2*n) = T(⌊2*n/2⌋)+ T(⌈2*n/2⌉) + 1
= T(⌊n⌋)+ T(⌈n⌉) + 1
= (2*n - 1) + (2*n - 1) + 1
= 4*n - 1
= 2 * (2*n) - 1
T(2*n+1) = T(⌊(2*n+1)/2⌋)+ T(⌈(2*n+1)/2⌉) + 1
= T(n)+ T(n+1) + 1
= (2*n - 1) + (2*(n+1) - 1) + 1 =
= 4*n + 1 =
= (2*n+1)*2 - 1
Since both the basis and the inductive step have been proved, it has now been proved by mathematical induction that T(n) holds for all natural 2*n - 1.
T(n) = 2*n - 1 = O(n)
What you have currently does not make sense. Since n is usually taken to be a natural number, then n=⌊n⌋=⌈n⌉. The recurrence then reads: break down a problem of size n into two problems of size n and spend time 1 doing that. The two new problems you just created will be split in turn, and so on- all you are doing is creating more work for yourself.

Big O Question - Algorithmic Analysis III

I have the following question:
Solve the recurrence relation simplifying the answer using Big 'O' notation:
f(0) = 2
f(n) = 6f(n-1)-5, n>0
I know this is a first order inhomogenous recurrence relation and have had a go at the question but I cannot seem to get the right output for the base case (f(0) = 2).
The question MUST use the sum of geometric series forumla within the proof.
Here is my answer - Note sum(x = y, z) is a replacement for capital sigma notation, where x is the lower bound of the summation initialised to y and z is the upper bound of the summation:
1. *change forumla:*
2. f(n) = 6^n.g(n)
3. => 6^n.g(n) = 6.6^(n-1) .g(n-1) -5
4. => g(n) = g(n-1)-5/6^n
5. => g(n) = sum(i=1, n)-5/6^i
6. => f(n) = 6^n.sum(i=1, n)-5/6^i
7. => *Evaluate the sum using geometric series forumla*
8. => sum(i = 1, n)-5/6^i = [sum(i = 1, n)a^i] -------> (a = -5/6)
9. => *sub a = -5/6 into geometric forumla [a(1-a^n)/(1-a)]*
10. => [(-5/6(1 - (-5/6)^n))/(1-(-5/6))]
11. => g(n) = [(-5/6(1 + (5/6)^n))/(1+5/6)]
12. => f(n) = 6^n . g(n) = 6^n[(-5/6(1 + (5/6)^n))/(1+5/6)]
13. => *sub in n = 0 to see if f(0) = 2*
Firstly, I am sure the equation on line 11 can be simplified further and secondly subbing in n = 0 should yield 2 as the result. I cannot obtain this answer when reaching line 13...
EDIT: What I need to know is why I am not getting f(0) = 2 when subbing n = 0 into the equation in line 12. Also what I would like to know is how can I simplify the equation for f(n) in line 12?
Anyone...?
Without thinking too hard about this, I'm going to say that f(n + 1) is 6 times larger than f(n), minus a constant. f(n) is therefore certainly O(6^n). Although you may find a tighter bound, that's about as far as I'd go in practice!
For the fun of it, I'll try this:
f(1) = 6f(0) - 5
= 6^1.f(0)
f(2) = 6f(1) - 5
= 6(6f(0) - 5) - 5
= 6^2.f(0) - 6^1.5 - 5
f(3) = 6f(2) - 5
= 6^3.f(0) - 6^2.5 - 6^1.5 - 5
I'll hazard a guess that
f(n) = 6^n.f(0) - 5.(6^0 + 6^1 + ... + 6^(n-1))
and I'm pretty sure that I could prove this by induction in a few lines (exercise left as an exercise for the student).
Now,
sum (k in 0..n-1) 6^k = (1 - 6^n) / (1 - 6)
therefore
f(n) = 6^n.f(0) - 5.(1 - 6^n) / (1 - 6)
= 6^n.f(0) + (1 - 6^n)
= 6^n.(2 - 1) + 1
= 6^n + 1
confirming my earlier intuition.
Let's just do a few quick check calculations:
f(0) = 2 = 6^0 + 1
f(1) = 6.2 - 5 = 7 = 6^1 + 1
f(2) = 6.7 - 5 = 37 = 6^2 + 1
f(3) = 6.37 - 5 = 237 = 6^3 + 1
That's enough for me for homework :-)

Resources