May you assist me with the output of this Fibonacci sequence - fibonacci

enter image description here
Thank you

n = 1 : 1
n = 2 : 1
n = 3 : Fib(n-1) + Fib(n-2) + Fib(n-3) = Fib(2) + Fib(1) + Fib(0) = 1 + 1 + 0 = 2
n = 4 : Fib(n-1) + Fib(n-2) + Fib(n-3) = Fib(3) + Fib(2) + Fib(1) = 2 + 1 + 1 = 4
Basically, you add the 3 previous number before n.

Related

Show full calculation in Mathematica to copy into latex

I'm new to Mathematica I was hoping that this is a functionality built in. I am calculating the conditional entropy of a table and have the following 4 variables:
a = 1/8*Log[(1/8)/(1/2)] + 1/16*Log[(1/16)/(1/2)] + 1/16*Log[(1/16)/(1/2)] + 1/4*Log[(1/4)/(1/2)]
b = 1/16*Log[(1/16)/(1/4)] + 1/8*Log[(1/8)/(1/4)] + 1/32*Log[(1/32)/(1/4)] + 0
c = 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 1/16*Log[(1/16)/(1/8)] + 0
d = 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] + 0
Then the final calculation is:
a + b + c + d
I was hoping there was a way to display the expanded a + b + c + d as a output so I can right-click/copy as latex to past into a document so I don't have to type it out. I can't figure out how to do it though. I'm guessing there is a way to expand those variables in the output to show the full calculation but maybe it is not possible. Thanks for any help.
I'm not sure how expanded you wanted the mathematical expression to be. See if the following expression is in the form you want to convert to LaTeX: (1/16 Log[1/(16/4)]+1/8 Log[1/(8/4)]+1/32 Log[1/(32/4)]+0)+(1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+1/16 Log[1/(16/8)]+0)+(1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+1/32 Log[1/(32/8)]+0)+(1/8 Log[1/(8/2)]+1/16 Log[1/(16/2)]+1/16 Log[1/(16/2)]+1/4 Log[1/(4/2)]).
You can get that by using HoldForm on the right-hand side of your assignments. Then use TeXForm[a+b+c+d] to convert:
a = HoldForm[
1/8*Log[(1/8)/(1/2)] + 1/16*Log[(1/16)/(1/2)] +
1/16*Log[(1/16)/(1/2)] + 1/4*Log[(1/4)/(1/2)]];
b = HoldForm[
1/16*Log[(1/16)/(1/4)] + 1/8*Log[(1/8)/(1/4)] +
1/32*Log[(1/32)/(1/4)] + 0];
c = HoldForm[
1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] +
1/16*Log[(1/16)/(1/8)] + 0];
d = HoldForm[
1/32*Log[(1/32)/(1/8)] + 1/32*Log[(1/32)/(1/8)] +
1/32*Log[(1/32)/(1/8)] + 0];
TeXForm[a + b + c + d]

Lua integer won't increment

I have this simple lua function designed to solve the problem of consecutive prime sum.
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred.
this is my function:
function numOfConsecPrimes(limit)
a = allPrimes(limit/2)
length = table.getn(a)
sumSoFar = 0 innerSum = 0 finalSum = 0
pos = 1
items = 0 innerItems = 0 finalItems = 0
resetpos = pos
while resetpos < length do
pos = resetpos
resetpos = resetpos + 1
items = 0
sumSoFar = 0
while sumSoFar < limit and pos < length do
if isPrime(sumSoFar) == true then innerSum = sumSoFar innerItems = items end
print(sumSoFar)
sumSofar = sumSoFar + a[pos]
print(a[pos] .."->"..sumSoFar)
pos = pos + 1
items = items + 1
end
if innerItems > finalItems then finalItems = innerItems finalSum = innerSum end
end
end
But for some reason, sumSoFar just won't change. I'm printing it before and after the addition of a[pos] and it stays zero always. I'm printing a[pos] as you see and the values are fine. So what's going on?
If this is your exact code then you simply have a typo.
sumSofar = sumSoFar + a[pos]
Capitalize the f in the first sumSofar so it matches all the other ones.

Recurrence relation: T(n) = T(n - 1) + n - 1

I am trying to understand how to solve recurrence relations. I understand it to the point where we have to simplify.
T(N) = T(N-1) + N-1 Initial condition: T(1)=O(1)=1
T(N) = T(N-1) + N-1
T(N-1) = T(N-2) + N-2
T(N-2) = T(N-3) + N-3
……
T(2) = T(1) + 1
**Summing up right and left sides**
T(N) + T(N-1) + T(N-2) + T(N-3) + …. T(3) + T(2) =
= T(N-1) + T(N-2) + T(N-3) + …. T(3) + T(2) + T(1) +
(N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
** Canceling like terms and simplifying **
T(N) = T(1) + N*(N-1)/2 1 + N*(N - 1)/2
T(N) = 1 + N*(N - 1)/2
I really don't understand the last part. I understand canceling like terms but don't understand how the simplification below works:
T(N) = T(1) + (N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
T(N) = T(1) + N*(N-1)/2 1 + N*(N - 1)/2
How is the second line derived from the first? Doesn't make any sense to me.
Would be a great help if someone can help me understand this. Thanks =)
In your second-to-last-line:
S = (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
You can say:
2S = S + S
= (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
1 + 2 + 3 + ... + (N-3) + (N-2) + (N-1)
= N + N + N + ... + N + N + N
|__________________ N-1 times ________________|
You're counting from N - 1 to 1, so there are N - 1 terms in the sequence. But the whole sequence is just N so you can say:
2S = N * (N - 1)
S = (N * (N - 1)) / 2
So in your last chunk:
T(N) = T(1) + (N-1) + (N-2) + (N-3) + ... + 3 + 2 + 1
= T(1) + (N * (N - 1)) / 2
T(N) = T(1) + (N-1) + (N-2) + (N-3) + …. +3 + 2 + 1
= T(1) + (N-1) + (N-2) + (N-3) + ..... + ( N-(N-3)) + (N-(N-2)) + (N-(N-1))
= T(1) + [N+N+N+..... n-1 times] - [1+2+3+......+(N-3)+(N-2)+(N-1)]
= T(1) + N*(N-1) - (N*(N-1))/2
= T(1) + N*(N-1)/2

Recurrence Relation Homework Struggles

Here's the question:
Solve the recurrence by obtaining a theta bound for T(n) given that T(1) = theta(1).
T(n) = n + T(n-3)
Attempted Solution:
T(n) = T(n-6) + (n-3) + n
= T(n-9) + (n-6) + (n-3) + n
= T(n-(n-1)) + [(n-n) + (n-(n-3)) + (n-(n-6)) + ... + n]
= T(1) + [0 + 3 + 6 + ... + n]
= theta(1) = 3[1 + 2 + 3 + ... + n/3]
= theta(1) + [(n/3)(n/3 + 1)]/2
= theta(1) + (n^2+3n)/6
When I double check to see if the solution fits the recurrence, it doesn't work.
The issue was that you were getting the wrong summation.
It doesn't start at 0, since your last T function was T(n - (n-1)) , which means previous one was T(n-(n-4)). So the summation starts at 4, and goes up till n.
If you don't know how to find the summation of this, I'd suggest you look at some of the proofs from the summation formula. This is what the solution looks like.
T(n) = T(n-3) + n
= T(n-6) + (n-3) + n
= T(n-(n-1)) + [ (n-(n-4)) + (n-(n-7)) + ... + n]
= T(1) + [4 + 7 + ... + n]
= theta(1) + (4 + n) * (n - 1)/6

Recurrence Relation: Finding Big O

I am trying to find the big O bound for the following recurrence relation:
T(n) = T(n-1) + n^c, where c >= 1 is a constant
So I've decided to solve this by using iteration:
T(n) = T(n-1) + n^c
T(n-1) = T(n-2) + (n-1)^c
T(n) = T(n-2) + n^c + (n-1)^c
T(n-2) = T(n-3) + (n-2)^c
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + .... + (n-k+1)^c
Suppose k = n-1, then:
T(n) = T(1) + n^c + (n-1)^c + (n-n+1+1)^c
T(n) = n^c + (n-1)^c + 2^c + 1
I'm not sure if this is correct however, plus I would really appreciate some guidance as to how to derive Big O from this. Thanks a lot!
Yes, you are correct:
T(n) = nc + (n-1)c + (n-2)c + … + 3c + 2c + 1,
and this sum is
T(n) = O(nc+1). See e.g. Faulhaber's formula. In fact, you can even determine the constant in the leading term (even if it's not germane to the algorithm's asymptotics): the sum is nc+1/(c+1) + O(c), as you can determine through e.g., using, say, integration.
What you have is not correct, but you were on the right track.
The mistake you made:
T(n) = T(n-3) + n^c + (n-1)^c + (n-2)^c
T(n) = T(n-k) + n^c + (n-1)^c + (n-k+1)^c
You cannot just go from the first line to the second line.
As you increase k, the number of terms in the right hand side increases too.
To see that think of writing it this way:
T(n) - T(n-1) = n^c.
T(n-1) - T(n-2) = (n-1)^c
..
T(n-k) - T(n-k-1) = (n-k)^c.
..
T(2) - T(1) = 2^c
What happens if you add these up?
Once you do that, can you see what the answer will be for c=1 and c=2? Can you figure out a pattern for the final answer from there?
Instead of working you way down from n, how about start by working your way up from 0 (I assume the recursion terminates at 0 and you left that bit out). When you start noticing a fixed point (ie a pattern which repeats the same in all cases) you have a good candidate for an answer. Try proving the answer, e.g. through induction.
I would start by observing that n^c, whilst of course influenced by the value of n, is not going to be any more complex for n vs. n + 1 - it's c that determines the "runtime" of that particular term. Given that, you can assume (at least I would) that the term has constant runtime and determine how many times the recursion will execute for a given n and you'll get your bound.
To figure these out, fill out a couple of terms and look for the pattern:
T(1) = 0 + 1^c
T(2) = T(1) + 2^c = 0 + 1^c + 2^c
T(3) = T(2) + 3^c = 0 + 1^c + 2^c + 3^c
T(4) = ...
Now express the pattern in terms of n and you have your answer.
Here it is:
T(n) = n^c + (n-1)^c + (n-2)^c + ... + 2^c + 1^c
< n^c + n^c + n^c + ... + n^c + n^c
= n * n^c
= n^(c+1)
which is O(nc+1).
To show this is a reasonable bound, note that when c = 1,
T(n) = n + (n-1) + (n-2) + ... + 2 + 1
= n * (n-1) / 2
which is clearly Θ(n2).

Resources