-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkbook10.Rmd
210 lines (136 loc) · 5.83 KB
/
workbook10.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
title: "STAT 33A Workbook 10"
date: "Nov 5, 2020"
author: "YOUR NAME (YOUR SID)"
output: pdf_document
---
This workbook is due __Oct Nov 5, 2020__ by 11:59pm PT.
The workbook is organized into sections that correspond to the lecture videos
for the week. Watch a video, then do the corresponding exercises _before_
moving on to the next video.
Workbooks are graded for completeness, so as long as you make a clear effort to
solve each problem, you'll get full credit. That said, make sure you understand
the concepts here, because they're likely to reappear in homeworks, quizzes,
and later lectures.
As you work, write your answers in this notebook. Answer questions with
complete sentences, and put code in code chunks. You can make as many new code
chunks as you like.
In the notebook, you can run the line of code where the cursor is by pressing
`Ctrl` + `Enter` on Windows or `Cmd` + `Enter` on Mac OS X. You can run an
entire code chunk by clicking on the green arrow in the upper right corner of
the code chunk.
Please do not delete the exercises already in this notebook, because it may
interfere with our grading tools.
You need to submit your work in two places:
* Submit this Rmd file with your edits on bCourses.
* Knit and submit the generated PDF file on Gradescope.
If you have any last-minute trouble knitting, **DON'T PANIC**. Submit your Rmd
file on time and follow up in office hours or on Piazza to sort out the PDF.
Printing Output
===============
Watch the "Printing Output" lecture video.
## Exercise 1
The `readline` function provides a way to collect _input_ from the user. Read
the documentation for `readline` to get an idea of how it works.
Write code that:
1. Calls `readline` to collect the user's name.
2. Prints `"Hello, NAME!"` where `NAME` is replaced by the collected name.
Write your code so that it works with any input name (that is, don't assume the
same name will always be entered).
**YOUR ANSWER GOES HERE:**
For-loops
=========
Watch the "For-loops" lecture video.
## Exercise 2
The Pell numbers are a sequence of numbers related to the Fibonacci numbers.
Each Pell number is the previous number doubled plus the number before that.
For example, the first two Pell numbers are 0 and 1. The third Pell number is
`2*1 + 0`, which is 2.
The first 10 Pell numbers are:
```
0 1 2 5 12 29 70 169 408 985
```
Write a loop that computes the first `n` Pell numbers.
Test your loop with `n = 30`.
**YOUR ANSWER GOES HERE:**
## Exercise 3
The Pell-Lucas numbers are computed the same way as the Pell numbers, but the
first two numbers are 2 and 2.
So the first 5 Pell-Lucas numbers are:
```
2 2 6 14 34
```
Write a function `compute_pell` that can compute Pell numbers or Pell-Lucas
numbers. Your function should have a parameter `n` that controls how many
numbers are computed, and a parameter `initial` that controls the first two
numbers in the sequence.
For example, the call `compute_pell(10, c(0, 1))` should return the first 10
Pell numbers. The call `compute_pell(10, c(2, 2))` should return the first 10
Pell-Lucas numbers.
Test that your function can compute both Pell numbers and Pell-Lucas numbers
for a few different values of `n`.
_Hint: Reuse your code from Exercise 2. You should only need to make a few
small changes to turn it into a function._
**YOUR ANSWER GOES HERE:**
## Exercise 4
The Pell-Lucas numbers and Pell numbers are interesting because you can use
them to approximate the square root of 2. The approximation is a Pell-Lucas
number divided by two times the corresponding Pell number.
For example, the fourth Pell-Lucas number is 14, and the fourth Pell number is
5, so `14 / (2 * 5)`, or 1.4, is an approximation for the square root of 2.
Use your function `compute_pell` to compute the first 100 Pell-Lucas numbers
and Pell numbers. Then use vectorized operations to divide the Pell-Lucas
numbers by two times the corresponding Pell numbers.
Does the approximation get better or worse for larger Pell/Pell-Lucas numbers?
_Historical Note: This approximation was first discovered by Indian
mathematicians around 300 BC._
**YOUR ANSWER GOES HERE:**
Loop Indices
============
Watch the "Loop Indices" lecture video.
No exercises for this section.
Preallocation
=============
Watch the "Preallocation" lecture video.
## Exercise 5
In the context of computer programming, _benchmarking_ means timing code to see
how long it takes to run.
The R package microbenchmark provides a function `microbenchmark` that can
benchmark R code.
For example, to benchmark `(1:100) * 4`, you can run:
```{r}
# Don't forget to install microbenchmark first!
library(microbenchmark)
microbenchmark(a = {
(1:100) * 4
})
```
As usual, the curly braces `{` are optional if the code you want to benchmark
is only one line long.
You can also use `microbenchmark` to benchmark multiple expressions at once,
for comparison:
```{r}
microbenchmark(a = {
(1:100) * 4
}, b = {
(1:100) + 4
})
```
Use the microbenchmark package to benchmark the "BAD" and "GOOD" example from
the lecture video.
Benchmark with three different values of `n` (testing both the "BAD" and "GOOD"
example for each value). About how much faster is the "GOOD" example?
_Hint 1: The `microbenchmark` function tries to automatically select
appropriate units for the timings. Make sure to pay attention to the units so
that your comparisons are valid! You can also use the `unit` parameter to
override the automatic unit selection._
_Hint 2: For accuracy, the `microbenchmark` function runs the code multiple
times in order to compute timing statistics. The default is 100 times. This may
be too many times for very slow code; you can use the `times` parameter to
adjust how many runs are used. To avoid inaccurate statistics, make sure
`times` is at least 30._
**YOUR ANSWER GOES HERE:**
Loops Example
=============
Watch the "Loops Example" lecture video.
No exercises for this section.