-
Notifications
You must be signed in to change notification settings - Fork 1
/
hw01.Rmd
316 lines (242 loc) · 5.57 KB
/
hw01.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
---
title: "Otter Assign for Rmd Sample"
author: "Chris Pyles"
output: pdf_document
---
<!--
# ASSIGNMENT CONFIG
requirements: requirements.R
generate: true
solutions_pdf: true
seed:
variable: rng_seed
autograder_value: 42
student_value: 90
-->
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Otter Assign for Rmd Sample
```{r}
library(ggplot2)
library(latex2exp)
rng_seed = 42
```
<!--
# BEGIN QUESTION
name: q1
points: 2
-->
**Question 1.** Write a function called `sieve` that takes in a positive integer `n` and returns a `set` of the prime numbers less than or equal to `n`. Use the Sieve of Eratosthenes to find the primes.
<!-- # BEGIN SOLUTION -->
```{r}
sieve = function(n) {
# BEGIN SOLUTION
is_prime = rep(TRUE, n)
p = 2
while (p^2 <= n) {
if (is_prime[p]) {
is_prime[seq(p^2, n, p)] = FALSE
}
p = p + 1
}
is_prime[1] = FALSE
return(seq(n)[is_prime])
# END SOLUTION
}
```
<!-- # END SOLUTION -->
<!-- # BEGIN TESTS -->
```{r}
testthat::expect_equal(length(sieve(1)), 0)
```
```{r}
testthat::expect_equal(sieve(2), c(2))
```
```{r}
testthat::expect_equal(sieve(3), c(2, 3))
```
```{r}
# HIDDEN
testthat::expect_equal(sieve(20), c(2, 3, 5, 7, 11, 13, 17, 19))
```
```{r}
. = " # BEGIN TEST CONFIG
points: 1
hidden: true
" # END TEST CONFIG
testthat::expect_equal(sieve(100), c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97))
```
<!-- # END TESTS -->
<!-- # END QUESTION -->
<!--
# BEGIN QUESTION
name: q2
manual: true
-->
**Question 2.** Evaluate the integral:
$$
\int x e^x \, \mathrm dx
$$
<!-- # BEGIN PROMPT -->
$$
\begin{aligned}
\int x e^x \, \mathrm dx &= \texttt{YOUR MATH HERE} \\
\end{aligned}
$$
<!-- # END PROMPT -->
<!-- # BEGIN SOLUTION -->
$$
\begin{aligned}
\int x e^x \, \mathrm dx &= x e^x - \int e^x \, \mathrm dx \\
&= x e^x - e^x + C \\
&= e^x (x - 1) + C \\
\end{aligned}
$$
<!-- # END SOLUTION -->
<!-- # END QUESTION -->
<!--
# BEGIN QUESTION
name: q3
manual: true
-->
**Question 3.** Graph the function $f(x) = \arctan \left ( e^x \right )$.
<!-- # BEGIN SOLUTION -->
```{r}
# BEGIN SOLUTION
xs = seq(-10, 10, length.out=100)
ys = atan(exp(xs))
ggplot(data.frame(xs, ys), aes(x=xs, y=ys)) +
geom_line() +
xlab(TeX("$x$")) +
ylab(TeX("$f(x)$")) +
labs(title=TeX("f(x) = arctan (e^x)")) +
theme(legend.position = "none")
# END SOLUTION
```
<!-- # END SOLUTION -->
<!-- # END QUESTION -->
<!--
# BEGIN QUESTION
name: q4
-->
**Question 4.** Write a function `hailstone` that returns the hailstone sequence for a positive integer ``n`` as a ``list``.
<!-- # BEGIN SOLUTION -->
```{r}
hailstone = function(n) {
# BEGIN SOLUTION
if (n == 1) return(c(n))
else if (n %% 2 == 0) return(c(n, hailstone(n %/% 2)))
else return(c(n, hailstone(3 * n + 1)))
# END SOLUTION
}
```
<!-- # END SOLUTION -->
<!-- # BEGIN TESTS -->
```{r}
testthat::expect_equal(hailstone(1), c(1))
```
```{r}
testthat::expect_equal(hailstone(2), c(2, 1))
```
```{r}
testthat::expect_equal(hailstone(4), c(4, 2, 1))
```
```{r}
. = " # BEGIN TEST CONFIG
points: 1.5
hidden: true
" # END TEST CONFIG
testthat::expect_equal(hailstone(20), c(20, 10, 5, 16, 8, 4, 2, 1))
```
```{r}
. = " # BEGIN TEST CONFIG
points: 1.5
hidden: true
" # END TEST CONFIG
testthat::expect_equal(hailstone(9), c(9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1))
```
<!-- # END TESTS -->
<!-- # END QUESTION -->
<!--
# BEGIN QUESTION
name: q5
-->
**Question 5.** Write a function ``gcd`` that takes in two positive integers ``a`` and ``b`` and returns their greatest common divisor.
<!-- # BEGIN SOLUTION -->
```{r}
gcd = function(a, b) {
# BEGIN SOLUTION
if (a == 0) return(b)
return(gcd(b %% a, a))
# END SOLUTION
}
```
<!-- # END SOLUTION -->
<!-- # BEGIN TESTS -->
```{r}
testthat::expect_equal(gcd(1, 1), 1)
```
```{r}
testthat::expect_equal(gcd(2, 1), 1)
```
```{r}
testthat::expect_equal(gcd(5, 5), 5)
```
```{r}
testthat::expect_equal(gcd(10, 4), 2)
```
```{r}
. = " # BEGIN TEST CONFIG
points: 0.5
hidden: true
" # END TEST CONFIG
testthat::expect_equal(gcd(121, 11), 11)
```
```{r}
. = " # BEGIN TEST CONFIG
points: 0.5
hidden: true
" # END TEST CONFIG
testthat::expect_equal(gcd(807306, 622896), 4098)
```
<!-- # END TESTS -->
<!-- # END QUESTION -->
<!--
# BEGIN QUESTION
name: q6
-->
**Question 6.** Write a function `box_muller` that takes in two $U(0,1)$ random variables and returns two standard normal random variables using the Box-Muller algorithm. Use this function to generate 1000 standard normal random variables. Make sure to use the provided RNG to generate your $U(0,1)$ RVs.
<!-- # BEGIN SOLUTION -->
```{r}
set.seed(rng_seed)
box_muller = function(u1, u2) {
# BEGIN SOLUTION
r = sqrt(-2 * log(u1))
theta = 2 * pi * u2
return(c(r * cos(theta), r * sin(theta)))
# END SOLUTION
}
rvs = as.vector(mapply(box_muller, runif(500), runif(500))) # SOLUTION
```
<!-- # END SOLUTION -->
<!-- # BEGIN TESTS -->
```{r}
testthat::expect_equal(length(rvs), c(1000))
testthat::expect_equal(dim(rvs), NULL)
```
```{r}
testthat::expect_equal(box_muller(0.5, 0.5), c(-1.1774100225154747, 1.4419114153575892e-16), tolerance=1e-5)
```
```{r}
. = " # BEGIN TEST CONFIG
points: 1
hidden: true
" # END TEST CONFIG
set.seed(1048)
smpl = sample(rvs, 10)
testthat::expect_equal(smpl, c(0.488075015538835, 0.194588102062245, 0.166299257619479, 0.835872276439119, 1.24125215479332,
-0.249328292625541, -0.823870507269021, 0.610713558462346, -0.212479727693556, -1.63148721078593))
```
<!-- # END TESTS -->
<!-- # END QUESTION -->