Solve the following recurrence: \[f(x) =\begin{cases} x & x \le 3 \\ f(x-3) + 4 & x > 3\end{cases}\]
Attempt: if x ≡ 0 (mod 3) f(x) = f(x-3) + 4 = f(x-6) + 8 = ... = f(x-3i) + 4i = ... = f(x - 3 (x/3 - 1)) + 4(n/3 - 1) = 4n/3 - 1 if x ≡ 1 (mod 3) f(x) = f(x-3) + 4 = f(x-6) + 8 = ... = f(x-3i) + 4i = ... \(= f(x - 3 (\lfloor{\frac{x}{3}}\rfloor )) + 4 (\lfloor{\frac{x}{3}}\rfloor)= f(1)+4(\lfloor{\frac{x}{3}}\rfloor) = 4(\lfloor{\frac{x}{3}}\rfloor ) + 1\) if x ≡ 3 (mod 3) f(x) = f(x-3) + 4 = f(x-6) + 8 = ... = f(x-3i) + 4i = ... \(= f(x - 3 (\lfloor{\frac{x}{3}}\rfloor )) + 4 (\lfloor{\frac{x}{3}}\rfloor)=f(2) + 4 (\lfloor{\frac{x}{3}}\rfloor)=4(\lfloor{\frac{x}{3}}\rfloor ) + 2\) This is the best I can do :(
Hmmm, start with \[ \begin{split} f(x) &= f(x-3) +4 \\ &= (f(x-3-3) + 4) + 4 = f(x-2(3)) + 2(4) \\ &= f(x-3n) +4n \end{split} \]
Then we need to solve for \(n\) in: \[ x-3n \leq 3 \]
\[x-3 \le 3n\]\[n\ge\frac{x-3}{3}\]
But why do we need to solve for n in x−3n≤3 ?
So that we can let \(f(x-3n) = x\)
Now we need to find \(n=g(x)\) and substitute it in.
Also, \(n\) has to be an integer as well.
What is n=g(x)?
The point is that there is some expression for \(x\) that will allow us to replace \(n\) with an expression of \(x\).
\[n=\lfloor\frac{x}{3}\rfloor-1\] ?
So far we have: \[ f(x) =\begin{cases} x &x\leq 3\\ (x-3n)+4n&x>3 \end{cases} \]
The best way is to just test values of \(x\) such as \(4,5,6,7\).
\[ \begin{array}{ccccc} x&4&5&6&7\\ n&1&1&1&2 \end{array} \]
Looks like \(n = \lfloor \frac{x-1}{3} \rfloor\)
\[ (x−3n)+4n = x-\left\lfloor \frac{x-1}{3} \right\rfloor \]
\[ n=\left\lfloor \frac{x-1}{3}\right\rfloor \implies n\leq \frac{x-1}{3} < n+1 \]
How do you get that (x-1)/3?
If you can somehow get this inequality theoretically, can you can do this problem more theoretically without using test points.
I started with \(x/3\), but since \(6/3=2\) that means we need to make \(x\) smaller. So I did \((x-1)/3\) and since \((6-1)/3 = 5/3 = 1+2/3\), I knew I needed to floor it to get just \(1\).
Why are you doing that?
Then I tested this on 7. \((7-1)/3= 2\). Then I tested it on \(4\).
Rather than "make \(x\) smaller" I should say, "make the numerator smaller".
Here is a display of what I got: http://www.wolframalpha.com/input/?i=Table%5BFloor%5B%28n+-+1%29%2F3%5D+%2C+%7Bn%2C+3%2C+10%7D%5D
Actually, what is going on here...
Remember that \(n\) just represents the number of times we invoked recursion.
looks like putting 0.001 to adjust value at zero was bad idea.
@RolyPoly ceil(x/3)-1 = floor((x-1)/3)
works for non negative values http://www.wolframalpha.com/input/?i=Table%5Bn+%2B+Ceiling%5B%28n+-+3%29%2F3%5D+%2C+%7Bn%2C+1%2C+10%7D%5D
What's the point of putting up WolframAlpha link without explanation?
@experimentX What are you trying to get?
the general formula for $f(x)$ in terms of $x$
@RolyPoly, where are you confused?
starting from the inequality x−3≤3n n≥(x−3)/3 n=ceil((x-3)/3)=ceil(x/3)-1 or n=floor((x-3)/3+2/3)=floor((x-1)/3) 2/3 is added to convert from ceil to floor
Hmm, I guess we should also test \(3.5\) and \(6.5\)
in general, ceil(x/k)=floor(x/k+(k-1)/k) and floor(x/k)=ceil(x/k-(k-1)/k)
\[f(x)=f(x−3)+4 = f(x−3n)+4n = x-3n + 4n = x+n\] Take \(n=\lceil\frac{x}{3}\rceil-1\), the function becomes \[f(x) = x+\lceil\frac{x}{3}\rceil-1\] To test if it is correct: Using the given recurrence equation: x | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 f(x) | 1 | 2 | 3 | 5 | 6 | 7 | 9 | 10 | 11 Using the equation I got: x | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 f(x) | 1 | 2 | 3 | 5 | 6 | 7 | 9 | 10 | 11 Looks good.
blows up at zero lol
Ehmmmmm
Never mind, where x is a positive integer. :P
ok ok
To confirm again why we are using ceiling instead of floor function here: \(x-3n \le 3\) The left actually gives us what the base case would be, either 1, 2, or 3. Solving for n: \(x-3n \le 3\) \(x-3 \le 3n\) \(n\ge \frac{x-3}{3}\) Since n is an non-negative integer, we need either ceiling or floor function here. If we use floor function, when x=4, we get \(n = \lfloor \frac{4-3}{3}\rfloor = 0 \), but n should be grater \(\frac{4-3}{3}\), i.e. \(\frac{1}{3}\), so, we cannot use ceiling function here, right?
yup; the ceiling function finds an integer greater than or equal to x; the floor function, an integer less than or equal to x
A type again. It should be "so, we cannot use floor function here, right?"
just add +1 after using floor function ... it's the same thing lol
Okay, how about this: \[ x-3n\leq 3\\ x\leq 3n+3\\ \frac x3 \leq n+1 \]If we add a restriction \(n<x/3\) then\[ n<\frac x3 \leq n+1\\ n-1<\frac x3 -1 \leq n \implies n=\left\lceil\frac{x}{3}-1\right\rceil = \left\lceil\frac{x}{3}\right\rceil -1 \]
How to you get ceiling function from that inequality?
From definitions. \[ n = \lfloor x \rfloor \implies n\leq x<n+1\\ n = \lceil x \rceil \implies n-1 < x \leq n \]
Should say \(\iff\).
Ok... But why do we add the restriction n<x/3?
Think about it. If \(7=\lceil x\rceil \) then \(6<x\leq 7\)
We can add any arbitrary restriction we want so long as it never contradicts existing restrictions.
Actually, let me think give a better justification.
Remember that \(n\) is just the number of times we will invoke recursion.
Hmmm, ok... Wait! Are you going to write an recursive function for n? :O
No, I am not.
Since \[ \frac x3\leq n+1 \]give us a lower bound for \(n\), how would you suppose we find an upper bound?
That is, what do we know to be the maximum number of times that we would invoke recursion?
To be honest, I added \(n<x/3\) by design.
However, we know we want the smallest \(n\) that satisfies the equation: \[ \frac x3\leq n+1 \]
The restriction \(n < x/3\) exists so that we don't get multiple integer solutions for \(n\).
Why?
Why do we want the smallest \(n\)? Why what?
Why does the existence of the restriction n<x/3 prevent us from getting multiple integer solutions for n?
Because if \(n<x/3\leq n+1\) then, that means \(x/3\in (n,n+1]\). The only integer in this interval is \(n+1\). We know \(n\) is not in it. We know \(n+2\) is not in it.
Why do we not want a multiple integer solutions for n?
If we have multiple solutions for \(n\), then the larger ones will have us invoke recursion when we should be sticking with our base case.
x/3≤n+1 <=> x/3 - 1 ≤ n Is x/3 -1 the lower bound of n?
Yes. The other bound is the fact that \(n\) is an integer.
Okay, here is an idea...
We said \(x-3n\leq 3\) is important, because it will tell us when \(n\) is large enough to give us the base case. Let's consider the value \(x-3m> 3\) to find out where \(m\) is small enough to require recursion. This just gives us \(x/3>m+1\). If we add one more recursion to \(m\), it should put us into the base case. That means \(n=m+1\). Putting it into our equation of \(m\) gives \(x/3>n\).
Basically: We need \(n\) or more recursions to get to the base case. We need \(m\) or less recursions to stay out of the base case. We know that \(n>m\). We also know \(n-m>0\) and \(n-m\) is an integer. For \(n\) and \(m\) to be closest together, we minimize \(|n-m|=n-m\). The next integer following \(0\) is \(1\). If \(n-m=1\), then \(n=m+1\).
Why do we need to minimize |n-m| ?
That means \(n\) and \(m\) are closest together. Remember that \(|n-m|\) is just the distance between the two.
What is the significance of finding the condition where they are closest together though?
Join our real-time social learning platform and learn together with your friends!