Daniel Sutantyo, Department of Computing, Macquarie University
2.2 - Induction and Recursive Functions
2.2 - Induction and Recursive Functions
2.2 - Induction and Recursive Functions
2.2 - Induction and Recursive Functions
3
1
2
4
. . .
3
1
2
4
2
1
5
. . .
5
3
2
1
2.2 - Induction and Recursive Functions
factorial(n):
if (n <= 1)
return 1
else
return n * factorial(n-1)
2.2 - Induction and Recursive Functions
factorial(n):
if (n <= 1)
return 1
else
return n * factorial(n-1)
2.2 - Induction and Recursive Functions
factorial(n):
if (n <= 1)
return 1
else
return n * factorial(n-1)
2.2 - Induction and Recursive Functions
factorial(n):
if (n <= 1)
return 1
else
return n * factorial(n-1)
2.2 - Induction and Recursive Functions
2.2 - Induction and Recursive Functions
\[ f(x) = c_nx^n + c_{n-1}x^{n-1} + \cdots +c_1x + c_0 \]
2.2 - Induction and Recursive Functions
\[ f(x) = 4x^5 + 3x^4 + 12x^3 + x^2 + 15x + 7 \]
evaluate(x,i):
if (i == 0)
return c_0
else
t = 1
for j = 1 to i:
t = t * x
return c_i * t + evaluate(x,i-1)
(disclaimer: if you think that this is a slow algorithm, you are correct)
2.2 - Induction and Recursive Functions
evaluate(x,i):
if (i == 0)
return c_0
else
t = 1
for j = 1 to i:
t = t * x
return c_i * t + evaluate(x,i-1)
\[ f(x) = c_nx^n + c_{n-1}x^{n-1} + \cdots +c_1x + c_0 \]
\[\begin{aligned} i &= 0 &\rightarrow\ &c_0\\ i &= 1 &\rightarrow\ &c_1x\\ & \vdots & \vdots & \\ i &= n &\rightarrow\ &c_nx^n\end{aligned}\]
2.2 - Induction and Recursive Functions
evaluate(x,i):
if (i == 0)
return c_0
else
t = 1
for j = 1 to i:
t = t * x
return c_i * t + evaluate(x,i-1)
\[\text{evaluate}(x,0) = c_0\]
\[\text{evaluate}(x,1) = c_1x + c_0\]
\[\text{evaluate}(x,n) = c_nx^n + c_{n-1}x^{n-1} + \cdots + c_1x + c_0\]
\[\vdots\]
\[\begin{aligned} i &= 0 &\rightarrow\ &c_0\\ i &= 1 &\rightarrow\ &c_1x\\ & \vdots & \vdots & \\ i &= n &\rightarrow\ &c_nx^n\end{aligned}\]
\[ f(x) = c_nx^n + c_{n-1}x^{n-1} + \cdots +c_1x + c_0 \]
\[\text{evaluate}(x,k) = c_kx^k + c_{k-1}x^{k-1} + \cdots + c_1x + c_0\]
\[\text{evaluate}(x,k+1) = c_{k+1}x^{k+1} + c_{k}x^{k} + \cdots + c_1x + c_0\]
\[\text{evaluate}(x,0) = c_0\]
\[\text{evaluate}(x,i) = c_ix^i + c_{i-1}x^{i-1} + \cdots + c_1x + c_0\]
2.2 - Induction and Recursive Functions
\[\text{evaluate}(x,k) = c_kx^k + c_{k-1}x^{k-1} + \cdots + c_1x + c_0\]
2.2 - Induction and Recursive Functions
\[ f(x) = 4x^5 + 3x^4 + 12x^3 + x^2 + 15x + 7 \]
\(i=3\)
\[ f(x) = 4x^5 + 3x^4 + 12x^3 + x^2 + 15x + 7 \]
\(i=4\)
\[ f(x) = 4x^5 + 3x^4 + 12x^3 + x^2 + 15x + 7 \]
\[ f(x) = 4x^5 + 3x^4 + 12x^3 + x^2 + 15x + 7 \]
\(i=2\)
2.2 - Induction and Recursive Functions
evaluate(x,i):
if (i == 0)
return c_0
else
t = 1
for j = 1 to i:
t = t * x
return c_i * t + evaluate(x,i-1)
2.2 - Induction and Recursive Functions
evaluate(x,i):
if (i == 0)
return c_0
else
t = 1
for j = 1 to i:
t = t * x
return c_i * t + evaluate(x,i-1)
\[\text{evaluate}(x,k) = c_kx^k + c_{k-1}x^{k-1} + \cdots + c_1x + c_0\]
2.2 - Induction and Recursive Functions
\[\text{evaluate}(x,k) = c_kx^k + c_{k-1}x^{k-1} + \cdots + c_1x + c_0\]
\[\text{evaluate}(x,k+1) = c_{k+1}x^{k+1} + c_{k}x^{k} + \cdots + c_1x + c_0\]
2.2 - Induction and Recursive Functions
\[\text{evaluate}(x,k+1) = c_{k+1}x^{k+1} + c_{k}x^{k} + \cdots + c_1x + c_0\]
evaluate(x,i):
if (i == 0)
return c_0
else
t = 1
for j = 1 to i:
t = t * x
return c_i * t + evaluate(x,i-1)
2.2 - Induction and Recursive Functions
2.2 - Induction and Recursive Functions
evaluate(x,i):
if (i == 0)
return c_0
else
t = 1
for j = 1 to i:
t = t * x
return c_i * t + evaluate(x,i-1)