Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
lecture materials
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
generic software skills
lecture materials
Commits
04bdb09e
Commit
04bdb09e
authored
1 year ago
by
Tobias Koelling
Browse files
Options
Downloads
Patches
Plain Diff
fibonacci: hands-on session
parent
9fa1865f
No related branches found
No related tags found
1 merge request
!24
complexity lecture
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lectures/complexity/slides.qmd
+17
-2
17 additions, 2 deletions
lectures/complexity/slides.qmd
with
17 additions
and
2 deletions
lectures/complexity/slides.qmd
+
17
−
2
View file @
04bdb09e
...
@@ -309,9 +309,16 @@ None
...
@@ -309,9 +309,16 @@ None
## Fibonacci series example (3/3)
## Fibonacci series example (3/3)
$$
$$
\begin{pmatrix}f_{n
-1
}\\f_{n}\end{pmatrix} = \begin{pmatrix}0 & 1\\1 & 1\end{pmatrix} \begin{pmatrix}f_{n-
2
}\\f_{n
-1
}\end{pmatrix}
\begin{pmatrix}f_{n}\\f_{n
+1
}\end{pmatrix} = \begin{pmatrix}0 & 1\\1 & 1\end{pmatrix} \begin{pmatrix}f_{n-
1
}\\f_{n}\end{pmatrix}
$$
$$
:::{.fragment}
$$
\begin{pmatrix}f_{n}\\f_{n+1}\end{pmatrix} = \begin{pmatrix}0 & 1\\1 & 1\end{pmatrix}^{n} \begin{pmatrix}f_0\\f_1\end{pmatrix}
$$
:::
## vector
## vector
```{python}
```{python}
...
@@ -362,13 +369,16 @@ m * m * V2(0, 1)
...
@@ -362,13 +369,16 @@ m * m * V2(0, 1)
## power function
## power function
```{python}
```{python}
#| echo:
tru
e
#| echo:
fals
e
def power(a: "A", n: int, op: "A,A -> A, assoc" = lambda a, b: a * b):
def power(a: "A", n: int, op: "A,A -> A, assoc" = lambda a, b: a * b):
assert n > 0
assert n > 0
if n == 1: return a
if n == 1: return a
if n % 2 == 0: return power(op(a, a), n // 2, op)
if n % 2 == 0: return power(op(a, a), n // 2, op)
return op(power(op(a, a), n // 2, op), a)
return op(power(op(a, a), n // 2, op), a)
```
```{python}
#| echo: true
power(2, 4)
power(2, 4)
```
```
...
@@ -380,6 +390,11 @@ power(m, 2) * V2(0, 1)
...
@@ -380,6 +390,11 @@ power(m, 2) * V2(0, 1)
```
```
:::
:::
## Hands-on! {.handson}
* Implement that generic `power` function.
* Do so with a time complexity better than $\mathcal{O}(n)$
## fib3
## fib3
```{python}
```{python}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment