Files
CalculusWithJuliaNotes.jl/quarto/_freeze/precalc/ranges/execute-results/html.json
2022-09-08 07:03:08 -04:00

15 lines
66 KiB
JSON

{
"hash": "fcd2119d34dfe51e6f05838cda8a4e5c",
"result": {
"markdown": "# Ranges and Sets\n\n\n\n\n\n## Arithmetic sequences\n\n\nSequences of numbers are prevalent in math. A simple one is just counting by ones:\n\n\n\n$$\n1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \\dots\n$$\n\n\nOr counting by sevens:\n\n\n\n$$\n7, 14, 21, 28, 35, 42, 49, \\dots\n$$\n\n\nMore challenging for humans is [counting backwards](http://www.psychpage.com/learning/library/assess/mse.htm) by 7:\n\n\n\n$$\n100, 93, 86, 79, \\dots\n$$\n\n\nThese are examples of [arithmetic sequences](http://en.wikipedia.org/wiki/Arithmetic_progression). The form of the first $n+1$ terms in such a sequence is:\n\n\n\n$$\na_0, a_0 + h, a_0 + 2h, a_0 + 3h, \\dots, a_0 + nh\n$$\n\n\nThe formula for the $a_n$th term can be written in terms of $a_0$, or any other $0 \\leq m \\leq n$ with $a_n = a_m + (n-m)\\cdot h$.\n\n\nA typical question might be: The first term of an arithmetic sequence is equal to $200$ and the common difference is equal to $-10$. Find the value of $a_{20}$. We could find this using $a_n = a_0 + n\\cdot h$:\n\n::: {.cell hold='true' execution_count=3}\n``` {.julia .cell-code}\na0, h, n = 200, -10, 20\na0 + n * h\n```\n\n::: {.cell-output .cell-output-display execution_count=4}\n```\n0\n```\n:::\n:::\n\n\nMore complicated questions involve an unknown first value, as with: an arithmetic sequence has a common difference equal to $10$ and its $6$th term is equal to $52$. Find its $15$th term, $a_{15}$. Here we have to answer: $a_0 + 15 \\cdot 10$. Either we could find $a_0$ (using $52 = a_0 + 6\\cdot(10)$) or use the above formula\n\n::: {.cell hold='true' execution_count=4}\n``` {.julia .cell-code}\na6, h, m, n = 52, 10, 6, 15\na15 = a6 + (n-m)*h\n```\n\n::: {.cell-output .cell-output-display execution_count=5}\n```\n142\n```\n:::\n:::\n\n\n### The colon operator\n\n\nRather than express sequences by the $a_0$, $h$, and $n$, `Julia` uses the starting point (`a`), the difference (`h`) and a *suggested* stopping value (`b`). That is, we need three values to specify these ranges of numbers: a `start`, a `step`, and an `endof`. `Julia` gives a convenient syntax for this: `a:h:b`. When the difference is just $1$, all numbers between the start and end are specified by `a:b`, as in\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\n1:10\n```\n\n::: {.cell-output .cell-output-display execution_count=6}\n```\n1:10\n```\n:::\n:::\n\n\nBut wait, nothing different printed? This is because `1:10` is efficiently stored. Basically, a recipe to generate the next number from the previous number is created and `1:10` just stores the start and end point and that recipe is used to generate the set of all values. To expand the values, you have to ask for them to be `collect`ed (though this typically isn't needed in practice):\n\n::: {.cell execution_count=6}\n``` {.julia .cell-code}\ncollect(1:10)\n```\n\n::: {.cell-output .cell-output-display execution_count=7}\n```\n10-element Vector{Int64}:\n 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n 10\n```\n:::\n:::\n\n\nWhen a non-default step size is needed, it goes in the middle, as in `a:h:b`. For example, counting by sevens from $1$ to $50$ is achieved by:\n\n::: {.cell execution_count=7}\n``` {.julia .cell-code}\ncollect(1:7:50)\n```\n\n::: {.cell-output .cell-output-display execution_count=8}\n```\n8-element Vector{Int64}:\n 1\n 8\n 15\n 22\n 29\n 36\n 43\n 50\n```\n:::\n:::\n\n\nOr counting down from 100:\n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\ncollect(100:-7:1)\n```\n\n::: {.cell-output .cell-output-display execution_count=9}\n```\n15-element Vector{Int64}:\n 100\n 93\n 86\n 79\n 72\n 65\n 58\n 51\n 44\n 37\n 30\n 23\n 16\n 9\n 2\n```\n:::\n:::\n\n\nIn this last example, we said end with $1$, but it ended with $2$. The ending value in the range is a suggestion to go up to, but not exceed. Negative values for `h` are used to make decreasing sequences.\n\n\n### The range function\n\n\nFor generating points to make graphs, a natural set of points to specify is $n$ evenly spaced points between $a$ and $b$. We can mimic creating this set with the range operation by solving for the correct step size. We have $a_0=a$ and $a_0 + (n-1) \\cdot h = b$. (Why $n-1$ and not $n$?) Solving yields $h = (b-a)/(n-1)$. To be concrete we might ask for $9$ points between $-1$ and $1$:\n\n::: {.cell hold='true' execution_count=9}\n``` {.julia .cell-code}\na, b, n = -1, 1, 9\nh = (b-a)/(n-1)\ncollect(a:h:b)\n```\n\n::: {.cell-output .cell-output-display execution_count=10}\n```\n9-element Vector{Float64}:\n -1.0\n -0.75\n -0.5\n -0.25\n 0.0\n 0.25\n 0.5\n 0.75\n 1.0\n```\n:::\n:::\n\n\nPretty neat. If we were doing this many times - such as once per plot - we'd want to encapsulate this into a function, for example:\n\n::: {.cell execution_count=10}\n``` {.julia .cell-code}\nfunction evenly_spaced(a, b, n)\n h = (b-a)/(n-1)\n collect(a:h:b)\nend\n```\n\n::: {.cell-output .cell-output-display execution_count=11}\n```\nevenly_spaced (generic function with 1 method)\n```\n:::\n:::\n\n\nGreat, let's try it out:\n\n::: {.cell execution_count=11}\n``` {.julia .cell-code}\nevenly_spaced(0, 2pi, 5)\n```\n\n::: {.cell-output .cell-output-display execution_count=12}\n```\n5-element Vector{Float64}:\n 0.0\n 1.5707963267948966\n 3.141592653589793\n 4.71238898038469\n 6.283185307179586\n```\n:::\n:::\n\n\nNow, our implementation was straightforward, but only because it avoids somethings. Look at something simple:\n\n::: {.cell execution_count=12}\n``` {.julia .cell-code}\nevenly_spaced(1/5, 3/5, 3)\n```\n\n::: {.cell-output .cell-output-display execution_count=13}\n```\n3-element Vector{Float64}:\n 0.2\n 0.4\n 0.6\n```\n:::\n:::\n\n\nIt seems to work as expected. But looking just at the algorithm it isn't quite so clear:\n\n::: {.cell execution_count=13}\n``` {.julia .cell-code}\n1/5 + 2*1/5 # last value\n```\n\n::: {.cell-output .cell-output-display execution_count=14}\n```\n0.6000000000000001\n```\n:::\n:::\n\n\nFloating point roundoff leads to the last value *exceeding* `0.6`, so should it be included? Well, here it is pretty clear it *should* be, but better to have something programmed that hits both `a` and `b` and adjusts `h` accordingly.\n\n\nEnter the base function `range` which solves this seemingly simple - but not really - task. It can use `a`, `b`, and `n`. Like the range operation, this function returns a generator which can be collected to realize the values.\n\n\nThe number of points is specified with keyword arguments, as in:\n\n::: {.cell execution_count=14}\n``` {.julia .cell-code}\nxs = range(-1, 1, length=9) # or simply range(-1, 1, 9) as of v\"1.7\"\n```\n\n::: {.cell-output .cell-output-display execution_count=15}\n```\n-1.0:0.25:1.0\n```\n:::\n:::\n\n\nand\n\n::: {.cell execution_count=15}\n``` {.julia .cell-code}\ncollect(xs)\n```\n\n::: {.cell-output .cell-output-display execution_count=16}\n```\n9-element Vector{Float64}:\n -1.0\n -0.75\n -0.5\n -0.25\n 0.0\n 0.25\n 0.5\n 0.75\n 1.0\n```\n:::\n:::\n\n\n:::{.callout-note}\n## Note\nThere is also the `LinRange(a, b, n)` function which can be more performant than `range`, as it doesn't try to correct for floating point errors.\n\n:::\n\n## Modifying sequences\n\n\nNow we concentrate on some more general styles to modify a sequence to produce a new sequence.\n\n\n### Filtering\n\n\nFor example, another way to get the values between $0$ and $100$ that are multiples of $7$ is to start with all $101$ values and throw out those that don't match. To check if a number is divisible by $7$, we could use the `rem` function. It gives the remainder upon division. Multiples of `7` match `rem(m, 7) == 0`. Checking for divisibility by seven is unusual enough there is nothing built in for that, but checking for division by $2$ is common, and for that, there is a built-in function `iseven`.\n\n\nThe act of throwing out elements of a collection based on some condition is called *filtering*. The `filter` function does this in `Julia`; the basic syntax being `filter(predicate_function, collection)`. The \"`predicate_function`\" is one that returns either `true` or `false`, such as `iseven`. The output of `filter` consists of the new collection of values - those where the predicate returns `true`.\n\n\nTo see it used, lets start with the numbers between `0` and `25` (inclusive) and filter out those that are even:\n\n::: {.cell execution_count=16}\n``` {.julia .cell-code}\nfilter(iseven, 0:25)\n```\n\n::: {.cell-output .cell-output-display execution_count=17}\n```\n13-element Vector{Int64}:\n 0\n 2\n 4\n 6\n 8\n 10\n 12\n 14\n 16\n 18\n 20\n 22\n 24\n```\n:::\n:::\n\n\nTo get the numbers between $1$ and $100$ that are divisible by $7$ requires us to write a function akin to `iseven`, which isn't hard (e.g., `is_seven(x) = x%7 == 0` or if being fancy `Base.Fix2(iszero∘rem, 7)`), but isn't something we continue with just yet.\n\n\nFor another example, here is an inefficient way to list the prime numbers between $100$ and $200$. This uses the `isprime` function from the `Primes` package\n\n``` {.julia .cell-code}\nusing Primes\n```\n\n\n::: {.cell execution_count=18}\n``` {.julia .cell-code}\nfilter(isprime, 100:200)\n```\n\n::: {.cell-output .cell-output-display execution_count=19}\n```\n21-element Vector{Int64}:\n 101\n 103\n 107\n 109\n 113\n 127\n 131\n 137\n 139\n 149\n 151\n 157\n 163\n 167\n 173\n 179\n 181\n 191\n 193\n 197\n 199\n```\n:::\n:::\n\n\nIllustrating `filter` at this point is mainly a motivation to illustrate that we can start with a regular set of numbers and then modify or filter them. The function takes on more value once we discuss how to write predicate functions.\n\n\n### Comprehensions\n\n\nLet's return to the case of the set of even numbers between $0$ and $100$. We have many ways to describe this set:\n\n\n * The collection of numbers $0, 2, 4, 6 \\dots, 100$, or the arithmetic sequence with step size $2$, which is returned by `0:2:100`.\n * The numbers between $0$ and $100$ that are even, that is `filter(iseven, 0:100)`.\n * The set of numbers $\\{2k: k=0, \\dots, 50\\}$.\n\n\nWhile `Julia` has a special type for dealing with sets, we will use a vector for such a set. (Unlike a set, vectors can have repeated values, but as vectors are more widely used, we demonstrate them.) Vectors are described more fully in a previous section, but as a reminder, vectors are constructed using square brackets: `[]` (a special syntax for [concatenation](http://docs.julialang.org/en/latest/manual/arrays/#concatenation)). Square brackets are used in different contexts within `Julia`, in this case we use them to create a *collection*. If we separate single values in our collection by commas (or semicolons), we will create a vector:\n\n::: {.cell execution_count=19}\n``` {.julia .cell-code}\nx = [0, 2, 4, 6, 8, 10]\n```\n\n::: {.cell-output .cell-output-display execution_count=20}\n```\n6-element Vector{Int64}:\n 0\n 2\n 4\n 6\n 8\n 10\n```\n:::\n:::\n\n\nThat is of course only part of the set of even numbers we want. Creating more might be tedious were we to type them all out, as above. In such cases, it is best to *generate* the values.\n\n\nFor this simple case, a range can be used, but more generally a [comprehension](https://docs.julialang.org/en/v1/manual/arrays/#man-comprehensions) provides this ability using a construct that closely mirrors a set definition, such as $\\{2k: k=0, \\dots, 50\\}$. The simplest use of a comprehension takes this form (as we described in the section on vectors):\n\n\n`[expr for variable in collection]`\n\n\nThe expression typically involves the variable specified after the keyword `for`. The collection can be a range, a vector, or many other items that are *iterable*. Here is how the mathematical set $\\{2k: k=0, \\dots, 50\\}$ may be generated by a comprehension:\n\n::: {.cell execution_count=20}\n``` {.julia .cell-code}\n[2k for k in 0:50]\n```\n\n::: {.cell-output .cell-output-display execution_count=21}\n```\n51-element Vector{Int64}:\n 0\n 2\n 4\n 6\n 8\n 10\n 12\n 14\n 16\n 18\n 20\n 22\n 24\n ⋮\n 78\n 80\n 82\n 84\n 86\n 88\n 90\n 92\n 94\n 96\n 98\n 100\n```\n:::\n:::\n\n\nThe expression is `2k`, the variable `k`, and the collection is the range of values, `0:50`. The syntax is basically identical to how the math expression is typically read aloud.\n\n\nFor some other examples, here is how we can create the first $10$ numbers divisible by $7$:\n\n::: {.cell execution_count=21}\n``` {.julia .cell-code}\n[7k for k in 1:10]\n```\n\n::: {.cell-output .cell-output-display execution_count=22}\n```\n10-element Vector{Int64}:\n 7\n 14\n 21\n 28\n 35\n 42\n 49\n 56\n 63\n 70\n```\n:::\n:::\n\n\nHere is how we can square the numbers between $1$ and $10$:\n\n::: {.cell execution_count=22}\n``` {.julia .cell-code}\n[x^2 for x in 1:10]\n```\n\n::: {.cell-output .cell-output-display execution_count=23}\n```\n10-element Vector{Int64}:\n 1\n 4\n 9\n 16\n 25\n 36\n 49\n 64\n 81\n 100\n```\n:::\n:::\n\n\nTo generate other progressions, such as powers of $2$, we could do:\n\n::: {.cell execution_count=23}\n``` {.julia .cell-code}\n[2^i for i in 1:10]\n```\n\n::: {.cell-output .cell-output-display execution_count=24}\n```\n10-element Vector{Int64}:\n 2\n 4\n 8\n 16\n 32\n 64\n 128\n 256\n 512\n 1024\n```\n:::\n:::\n\n\nHere are decreasing powers of $2$:\n\n::: {.cell execution_count=24}\n``` {.julia .cell-code}\n[1/2^i for i in 1:10]\n```\n\n::: {.cell-output .cell-output-display execution_count=25}\n```\n10-element Vector{Float64}:\n 0.5\n 0.25\n 0.125\n 0.0625\n 0.03125\n 0.015625\n 0.0078125\n 0.00390625\n 0.001953125\n 0.0009765625\n```\n:::\n:::\n\n\nSometimes, the comprehension does not produce the type of output that may be expected. This is related to `Julia`'s more limited abilities to infer types at the command line. If the output type is important, the extra prefix of `T[]` can be used, where `T` is the desired type. We will see that this will be needed at times with symbolic math.\n\n\n### Generators\n\n\nA typical pattern would be to generate a collection of numbers and then apply a function to them. For example, here is one way to sum the powers of $2$:\n\n::: {.cell execution_count=25}\n``` {.julia .cell-code}\nsum([2^i for i in 1:10])\n```\n\n::: {.cell-output .cell-output-display execution_count=26}\n```\n2046\n```\n:::\n:::\n\n\nConceptually this is easy to understand, but computationally it is a bit inefficient. The generator syntax allows this type of task to be done more efficiently. To use this syntax, we just need to drop the `[]`:\n\n::: {.cell execution_count=26}\n``` {.julia .cell-code}\nsum(2^i for i in 1:10)\n```\n\n::: {.cell-output .cell-output-display execution_count=27}\n```\n2046\n```\n:::\n:::\n\n\n(The difference being no intermediate object is created to store the collection of all values specified by the generator.)\n\n\n### Filtering generated expressions\n\n\nBoth comprehensions and generators allow for filtering through the keyword `if`. The following shows *one* way to add the prime numbers in $[1,100]$:\n\n::: {.cell execution_count=27}\n``` {.julia .cell-code}\nsum(p for p in 1:100 if isprime(p))\n```\n\n::: {.cell-output .cell-output-display execution_count=28}\n```\n1060\n```\n:::\n:::\n\n\nThe value on the other side of `if` should be an expression that evaluates to either `true` or `false` for a given `p` (like a predicate function, but here specified as an expression). The value returned by `isprime(p)` is such.\n\n\nIn this example, we use the fact that `rem(k, 7)` returns the remainder found from dividing `k` by `7`, and so is `0` when `k` is a multiple of `7`:\n\n::: {.cell execution_count=28}\n``` {.julia .cell-code}\nsum(k for k in 1:100 if rem(k,7) == 0) ## add multiples of 7\n```\n\n::: {.cell-output .cell-output-display execution_count=29}\n```\n735\n```\n:::\n:::\n\n\nThe same `if` can be used in a comprehension. For example, this is an alternative to `filter` for identifying the numbers divisible by `7` in a range of numbers:\n\n::: {.cell execution_count=29}\n``` {.julia .cell-code}\n[k for k in 1:100 if rem(k,7) == 0]\n```\n\n::: {.cell-output .cell-output-display execution_count=30}\n```\n14-element Vector{Int64}:\n 7\n 14\n 21\n 28\n 35\n 42\n 49\n 56\n 63\n 70\n 77\n 84\n 91\n 98\n```\n:::\n:::\n\n\n#### Example: Making change\n\n\nThis example of Stefan Karpinski's comes from a [blog](http://julialang.org/blog/2016/10/julia-0.5-highlights) post highlighting changes to the `Julia` language with version `v\"0.5.0\"`, which added features to comprehensions that made this example possible.\n\n\nFirst, a simple question: using pennies, nickels, dimes, and quarters how many different ways can we generate one dollar? Clearly $100$ pennies, or $20$ nickels, or $10$ dimes, or $4$ quarters will do this, so the answer is at least four, but how much more than four?\n\n\nWell, we can use a comprehension to enumerate the possibilities. This example illustrates how comprehensions and generators can involve one or more variable for the iteration.\n\n\nFirst, we either have $0,1,2,3$, or $4$ quarters, or $0$, $25$ cents, $50$ cents, $75$ cents, or a dollar's worth. If we have, say, $1$ quarter, then we need to make up $75$ cents with the rest. If we had $3$ dimes, then we need to make up $45$ cents out of nickels and pennies, if we then had $6$ nickels, we know we must need $15$ pennies.\n\n\nThe following expression shows how counting this can be done through enumeration. Here `q` is the amount contributed by quarters, `d` the amount from dimes, `n` the amount from nickels, and `p` the amount from pennies. `q` ranges over $0, 25, 50, 75, 100$ or `0:25:100`, etc. If we know that the sum of quarters, dimes, nickels contributes a certain amount, then the number of pennies must round things up to $100$.\n\n::: {.cell execution_count=30}\n``` {.julia .cell-code}\nways = [(q, d, n, p) for q = 0:25:100 for d = 0:10:(100 - q) for n = 0:5:(100 - q - d) for p = (100 - q - d - n)]\nlength(ways)\n```\n\n::: {.cell-output .cell-output-display execution_count=31}\n```\n242\n```\n:::\n:::\n\n\nWe see $242$ cases, each distinct. The first $3$ are:\n\n::: {.cell execution_count=31}\n``` {.julia .cell-code}\nways[1:3]\n```\n\n::: {.cell-output .cell-output-display execution_count=32}\n```\n3-element Vector{NTuple{4, Int64}}:\n (0, 0, 0, 100)\n (0, 0, 5, 95)\n (0, 0, 10, 90)\n```\n:::\n:::\n\n\nThe generating expression reads naturally. It introduces the use of multiple `for` statements, each subsequent one depending on the value of the previous (working left to right). Now suppose, we want to ensure that the amount in pennies is less than the amount in nickels, etc. We could use `filter` somehow to do this for our last answer, but using `if` allows for filtering while the events are generating. Here our condition is simply expressed: `q > d > n > p`:\n\n::: {.cell execution_count=32}\n``` {.julia .cell-code}\n[(q, d, n, p) for q = 0:25:100\n for d = 0:10:(100 - q)\n for n = 0:5:(100 - q - d)\n for p = (100 - q - d - n)\n if q > d > n > p]\n```\n\n::: {.cell-output .cell-output-display execution_count=33}\n```\n4-element Vector{NTuple{4, Int64}}:\n (50, 30, 15, 5)\n (50, 30, 20, 0)\n (50, 40, 10, 0)\n (75, 20, 5, 0)\n```\n:::\n:::\n\n\n## Random numbers\n\n\nWe have been discussing structured sets of numbers. On the opposite end of the spectrum are random numbers. `Julia` makes them easy to generate, especially random numbers chosen uniformly from $[0,1)$.\n\n\n * The `rand()` function returns a randomly chosen number in $[0,1)$.\n * The `rand(n)` function returns a vector of `n` randomly chosen numbers in $[0,1)$.\n\n\nTo illustrate, this will command return a single number\n\n::: {.cell execution_count=33}\n``` {.julia .cell-code}\nrand()\n```\n\n::: {.cell-output .cell-output-display execution_count=34}\n```\n0.4997607881985997\n```\n:::\n:::\n\n\nIf the command is run again, it is almost certain that a different value will be returned:\n\n::: {.cell execution_count=34}\n``` {.julia .cell-code}\nrand()\n```\n\n::: {.cell-output .cell-output-display execution_count=35}\n```\n0.1236462916571216\n```\n:::\n:::\n\n\nThis call will return a vector of $10$ such random numbers:\n\n::: {.cell execution_count=35}\n``` {.julia .cell-code}\nrand(10)\n```\n\n::: {.cell-output .cell-output-display execution_count=36}\n```\n10-element Vector{Float64}:\n 0.5218415896207655\n 0.3465613421305773\n 0.3024511276042172\n 0.7593751281138323\n 0.24170903517256648\n 0.5858817033715646\n 0.5131987956957759\n 0.27962430273398187\n 0.5119649223983749\n 0.819278263397468\n```\n:::\n:::\n\n\nThe `rand` function is easy to use. The only common source of confusion is the subtle distinction between `rand()` and `rand(1)`, as the latter is a vector of $1$ random number and the former just $1$ random number.\n\n\n## Questions\n\n\n###### Question\n\n\nWhich of these will produce the odd numbers between $1$ and $99$?\n\n::: {.cell hold='true' execution_count=36}\n\n::: {.cell-output .cell-output-display execution_count=37}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='3162062178398904219' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_3162062178398904219\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_3162062178398904219_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_3162062178398904219\"\n id=\"radio_3162062178398904219_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>1:3:99</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_3162062178398904219_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_3162062178398904219\"\n id=\"radio_3162062178398904219_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>1:2:99</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_3162062178398904219_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_3162062178398904219\"\n id=\"radio_3162062178398904219_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>1:99</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='3162062178398904219_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_3162062178398904219\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 2;\n var msgBox = document.getElementById('3162062178398904219_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_3162062178398904219\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_3162062178398904219\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhich of these will create the sequence $2, 9, 16, 23, \\dots, 72$?\n\n::: {.cell hold='true' execution_count=37}\n\n::: {.cell-output .cell-output-display execution_count=38}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='6393553213140521043' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_6393553213140521043\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_6393553213140521043_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_6393553213140521043\"\n id=\"radio_6393553213140521043_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>2:72</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_6393553213140521043_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_6393553213140521043\"\n id=\"radio_6393553213140521043_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>2:9:72</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_6393553213140521043_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_6393553213140521043\"\n id=\"radio_6393553213140521043_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>72:-7:2</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_6393553213140521043_4\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_6393553213140521043\"\n id=\"radio_6393553213140521043_4\" value=\"4\">\n </input>\n <span class=\"label-body px-1\">\n <code>2:7:72</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='6393553213140521043_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_6393553213140521043\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 4;\n var msgBox = document.getElementById('6393553213140521043_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_6393553213140521043\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_6393553213140521043\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nHow many numbers are in the sequence produced by `0:19:1000`?\n\n::: {.cell hold='true' execution_count=38}\n\n::: {.cell-output .cell-output-display execution_count=39}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='11773566419931069743' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_11773566419931069743\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"11773566419931069743\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='11773566419931069743_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"11773566419931069743\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 53) <= 0);\n var msgBox = document.getElementById('11773566419931069743_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_11773566419931069743\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_11773566419931069743\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nThe range operation (`a:h:b`) can also be used to countdown. Which of these will do so, counting down from `10` to `1`? (You can call `collect` to visualize the generated numbers.)\n\n::: {.cell hold='true' execution_count=39}\n\n::: {.cell-output .cell-output-display execution_count=40}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='12831899208654243237' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_12831899208654243237\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_12831899208654243237_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_12831899208654243237\"\n id=\"radio_12831899208654243237_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>1:-1:10</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_12831899208654243237_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_12831899208654243237\"\n id=\"radio_12831899208654243237_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>10:-1:1</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_12831899208654243237_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_12831899208654243237\"\n id=\"radio_12831899208654243237_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>1:10</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_12831899208654243237_4\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_12831899208654243237\"\n id=\"radio_12831899208654243237_4\" value=\"4\">\n </input>\n <span class=\"label-body px-1\">\n <code>10:1</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='12831899208654243237_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_12831899208654243237\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 2;\n var msgBox = document.getElementById('12831899208654243237_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_12831899208654243237\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_12831899208654243237\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhat is the last number generated by `1:4:7`?\n\n::: {.cell hold='true' execution_count=40}\n\n::: {.cell-output .cell-output-display execution_count=41}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='18318320108952616980' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_18318320108952616980\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"18318320108952616980\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='18318320108952616980_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"18318320108952616980\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 5) <= 0);\n var msgBox = document.getElementById('18318320108952616980_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_18318320108952616980\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_18318320108952616980\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhile the range operation can generate vectors by collecting, do the objects themselves act like vectors?\n\n\nDoes scalar multiplication work as expected? In particular, is the result of `2*(1:5)` *basically* the same as `2 * [1,2,3,4,5]`?\n\n::: {.cell hold='true' execution_count=41}\n\n::: {.cell-output .cell-output-display execution_count=42}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='982737945835622637' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_982737945835622637\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_982737945835622637_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_982737945835622637\"\n id=\"radio_982737945835622637_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n Yes\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_982737945835622637_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_982737945835622637\"\n id=\"radio_982737945835622637_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n No\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='982737945835622637_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_982737945835622637\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 1;\n var msgBox = document.getElementById('982737945835622637_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_982737945835622637\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_982737945835622637\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\nDoes vector addition work? as expected? In particular, is the result of `(1:4) + (2:5)` *basically* the same as `[1,2,3,4]` + `[2,3,4,5]`?\n\n::: {.cell hold='true' execution_count=42}\n\n::: {.cell-output .cell-output-display execution_count=43}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='6142550369190684866' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_6142550369190684866\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_6142550369190684866_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_6142550369190684866\"\n id=\"radio_6142550369190684866_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n Yes\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_6142550369190684866_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_6142550369190684866\"\n id=\"radio_6142550369190684866_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n No\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='6142550369190684866_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_6142550369190684866\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 1;\n var msgBox = document.getElementById('6142550369190684866_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_6142550369190684866\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_6142550369190684866\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\nWhat if parentheses are left off? Explain the output of `1:4 + 2:5`?\n\n::: {.cell hold='true' execution_count=43}\n\n::: {.cell-output .cell-output-display execution_count=44}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='14617937889860922947' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_14617937889860922947\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_14617937889860922947_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_14617937889860922947\"\n id=\"radio_14617937889860922947_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n It is just random\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_14617937889860922947_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_14617937889860922947\"\n id=\"radio_14617937889860922947_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n Addition happens prior to the use of <code>:</code> so this is like <code>1:&#40;4&#43;2&#41;:5</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_14617937889860922947_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_14617937889860922947\"\n id=\"radio_14617937889860922947_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n It gives the correct answer, a generator for the vector <code>&#91;3,5,7,9&#93;</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='14617937889860922947_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_14617937889860922947\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 2;\n var msgBox = document.getElementById('14617937889860922947_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_14617937889860922947\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_14617937889860922947\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nHow is `a:b-1` interpreted:\n\n::: {.cell hold='true' execution_count=44}\n\n::: {.cell-output .cell-output-display execution_count=45}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='17469012375245838894' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_17469012375245838894\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_17469012375245838894_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_17469012375245838894\"\n id=\"radio_17469012375245838894_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n as <code>a:&#40;b-1&#41;</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_17469012375245838894_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_17469012375245838894\"\n id=\"radio_17469012375245838894_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n as <code>&#40;a:b&#41; - 1</code>, which is <code>&#40;a-1&#41;:&#40;b-1&#41;</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='17469012375245838894_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_17469012375245838894\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 1;\n var msgBox = document.getElementById('17469012375245838894_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_17469012375245838894\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_17469012375245838894\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nCreate the sequence $10, 100, 1000, \\dots, 1,000,000$ using a list comprehension. Which of these works?\n\n::: {.cell hold='true' execution_count=45}\n\n::: {.cell-output .cell-output-display execution_count=46}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='13488088805275503982' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_13488088805275503982\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_13488088805275503982_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_13488088805275503982\"\n id=\"radio_13488088805275503982_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;10^i for i in 1:6&#93;</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_13488088805275503982_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_13488088805275503982\"\n id=\"radio_13488088805275503982_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;10^i for i in &#91;10, 100, 1000&#93;&#93;</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_13488088805275503982_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_13488088805275503982\"\n id=\"radio_13488088805275503982_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;i^10 for i in &#91;1:6&#93;&#93;</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='13488088805275503982_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_13488088805275503982\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 1;\n var msgBox = document.getElementById('13488088805275503982_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_13488088805275503982\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_13488088805275503982\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nCreate the sequence $0.1, 0.01, 0.001, \\dots, 0.0000001$ using a list comprehension. Which of these will work:\n\n::: {.cell hold='true' execution_count=46}\n\n::: {.cell-output .cell-output-display execution_count=47}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='15857697427831502788' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_15857697427831502788\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_15857697427831502788_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_15857697427831502788\"\n id=\"radio_15857697427831502788_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;10^-i for i in 1:7&#93;</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_15857697427831502788_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_15857697427831502788\"\n id=\"radio_15857697427831502788_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;&#40;1/10&#41;^i for i in 1:7&#93;</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_15857697427831502788_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_15857697427831502788\"\n id=\"radio_15857697427831502788_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;i^&#40;1/10&#41; for i in 1:7&#93;</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='15857697427831502788_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_15857697427831502788\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 2;\n var msgBox = document.getElementById('15857697427831502788_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_15857697427831502788\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_15857697427831502788\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nEvaluate the expression $x^3 - 2x + 3$ for each of the values $-5, -4, \\dots, 4, 5$ using a comprehension. Which of these will work?\n\n::: {.cell hold='true' execution_count=47}\n\n::: {.cell-output .cell-output-display execution_count=48}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='1761261977235588895' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_1761261977235588895\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_1761261977235588895_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_1761261977235588895\"\n id=\"radio_1761261977235588895_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;x^3 - 2x &#43; 3 for x in -&#40;5:5&#41;&#93;</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_1761261977235588895_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_1761261977235588895\"\n id=\"radio_1761261977235588895_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;x^3 - 2x &#43; 3 for x in -5:5&#93;</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_1761261977235588895_3\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_1761261977235588895\"\n id=\"radio_1761261977235588895_3\" value=\"3\">\n </input>\n <span class=\"label-body px-1\">\n <code>&#91;x^3 - 2x &#43; 3 for i in -5:5&#93;</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='1761261977235588895_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_1761261977235588895\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 2;\n var msgBox = document.getElementById('1761261977235588895_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_1761261977235588895\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_1761261977235588895\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nHow many prime numbers are there between $1100$ and $1200$? (Use `filter` and `isprime`)\n\n::: {.cell hold='true' execution_count=48}\n\n::: {.cell-output .cell-output-display execution_count=49}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='12607195168787647782' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_12607195168787647782\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"12607195168787647782\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='12607195168787647782_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"12607195168787647782\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 12) <= 0);\n var msgBox = document.getElementById('12607195168787647782_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_12607195168787647782\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_12607195168787647782\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWhich has more prime numbers the range `1000:2000` or the range `11000:12000`?\n\n::: {.cell hold='true' execution_count=49}\n\n::: {.cell-output .cell-output-display execution_count=50}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='1630049560840223159' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_1630049560840223159\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_1630049560840223159_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_1630049560840223159\"\n id=\"radio_1630049560840223159_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n <code>1000:2000</code>\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_1630049560840223159_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_1630049560840223159\"\n id=\"radio_1630049560840223159_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n <code>11000:12000</code>\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='1630049560840223159_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_1630049560840223159\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 1;\n var msgBox = document.getElementById('1630049560840223159_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_1630049560840223159\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_1630049560840223159\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nWe can easily add an arithmetic progression with the `sum` function. For example, `sum(1:100)` will add the numbers $1, 2, ..., 100$.\n\n\nWhat is the sum of the odd numbers between $0$ and $100$?\n\n::: {.cell hold='true' execution_count=50}\n\n::: {.cell-output .cell-output-display execution_count=51}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='5029491279191088340' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_5029491279191088340\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"5029491279191088340\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='5029491279191088340_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"5029491279191088340\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 2500) <= 0);\n var msgBox = document.getElementById('5029491279191088340_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_5029491279191088340\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_5029491279191088340\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nThe sum of the arithmetic progression $a, a+h, \\dots, a+n\\cdot h$ has a simple formula. Using a few cases, can you tell if this is the correct one:\n\n\n\n$$\n(n+1)\\cdot a + h \\cdot n(n+1)/2\n$$\n\n::: {.cell hold='true' execution_count=51}\n\n::: {.cell-output .cell-output-display execution_count=52}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='13188258358847755619' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_13188258358847755619\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_13188258358847755619_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_13188258358847755619\"\n id=\"radio_13188258358847755619_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n Yes, this is true\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_13188258358847755619_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_13188258358847755619\"\n id=\"radio_13188258358847755619_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n No, this is false\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='13188258358847755619_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_13188258358847755619\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 1;\n var msgBox = document.getElementById('13188258358847755619_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_13188258358847755619\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_13188258358847755619\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nA *geometric progression* is of the form $a^0, a^1, a^2, \\dots, a^n$. These are easily generated by comprehensions of the form `[a^i for i in 0:n]`. Find the sum of the geometric progression $1, 2^1, 2^2, \\dots, 2^{10}$.\n\n::: {.cell hold='true' execution_count=52}\n\n::: {.cell-output .cell-output-display execution_count=53}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='13437010918044858292' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_13437010918044858292\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"13437010918044858292\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='13437010918044858292_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"13437010918044858292\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 2047) <= 0);\n var msgBox = document.getElementById('13437010918044858292_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_13437010918044858292\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_13437010918044858292\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\nIs your answer of the form $(1 - a^{n+1}) / (1-a)$?\n\n::: {.cell hold='true' execution_count=53}\n\n::: {.cell-output .cell-output-display execution_count=54}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='1781599702138593964' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_1781599702138593964\">\n <div style=\"padding-top: 5px\">\n <div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_1781599702138593964_1\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_1781599702138593964\"\n id=\"radio_1781599702138593964_1\" value=\"1\">\n </input>\n <span class=\"label-body px-1\">\n Yes\n </span>\n </label>\n</div>\n<div class=\"form-check\">\n <label class=\"form-check-label\" for=\"radio_1781599702138593964_2\">\n <input class=\"form-check-input\" type=\"radio\" name=\"radio_1781599702138593964\"\n id=\"radio_1781599702138593964_2\" value=\"2\">\n </input>\n <span class=\"label-body px-1\">\n No\n </span>\n </label>\n</div>\n\n \n </div>\n </div>\n <div id='1781599702138593964_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.querySelectorAll('input[name=\"radio_1781599702138593964\"]').forEach(function(rb) {\nrb.addEventListener(\"change\", function() {\n var correct = rb.value == 1;\n var msgBox = document.getElementById('1781599702138593964_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_1781599702138593964\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_1781599702138593964\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n})});\n\n</script>\n```\n:::\n:::\n\n\n###### Question\n\n\nThe [product](http://en.wikipedia.org/wiki/Arithmetic_progression) of the terms in an arithmetic progression has a known formula. The product can be found by an expression of the form `prod(a:h:b)`. Find the product of the terms in the sequence $1,3,5,\\dots,19$.\n\n::: {.cell hold='true' execution_count=54}\n\n::: {.cell-output .cell-output-display execution_count=55}\n```{=html}\n<form class=\"mx-2 my-3 mw-100\" name='WeaveQuestion' data-id='12028719529852036585' data-controltype=''>\n <div class='form-group '>\n <div class='controls'>\n <div class=\"form\" id=\"controls_12028719529852036585\">\n <div style=\"padding-top: 5px\">\n </br>\n<div class=\"input-group\">\n <input id=\"12028719529852036585\" type=\"number\" class=\"form-control\" placeholder=\"Numeric answer\">\n</div>\n\n \n </div>\n </div>\n <div id='12028719529852036585_message' style=\"padding-bottom: 15px\"></div>\n </div>\n </div>\n</form>\n\n<script text='text/javascript'>\ndocument.getElementById(\"12028719529852036585\").addEventListener(\"change\", function() {\n var correct = (Math.abs(this.value - 654729075) <= 0);\n var msgBox = document.getElementById('12028719529852036585_message');\n if(correct) {\n msgBox.innerHTML = \"<div class='pluto-output admonition note alert alert-success'><span> 👍&nbsp; Correct </span></div>\";\n var explanation = document.getElementById(\"explanation_12028719529852036585\")\n if (explanation != null) {\n explanation.style.display = \"none\";\n }\n } else {\n msgBox.innerHTML = \"<div class='pluto-output admonition alert alert-danger'><span>👎&nbsp; Incorrect </span></div>\";\n var explanation = document.getElementById(\"explanation_12028719529852036585\")\n if (explanation != null) {\n explanation.style.display = \"block\";\n }\n }\n\n});\n\n</script>\n```\n:::\n:::\n\n\n",
"supporting": [
"ranges_files"
],
"filters": [],
"includes": {
"include-in-header": [
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js\" integrity=\"sha512-c3Nl8+7g4LMSTdrm621y7kf9v3SDPnhxLNhcjFJbKECVnmZHTdo+IRO05sNLTH/D3vA6u1X32ehoLC7WFVdheg==\" crossorigin=\"anonymous\"></script>\n<script src=\"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js\" integrity=\"sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==\" crossorigin=\"anonymous\"></script>\n<script type=\"application/javascript\">define('jquery', [],function() {return window.jQuery;})</script>\n"
]
}
}
}