{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Workbook for Chapter 3 of Explorations in Computing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3.7 The `sieve` Function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Initialization " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the following code cell to import `ceil` and `sqrt` from the math library:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import sqrt, ceil" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Also run this cell, which includes the `sift` helper function from the previous section and a new helper function called `non_nulls`. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def sift(k, a):\n", " \"Remove multiples of k from list a\"\n", " for i in range(2*k, len(a), k):\n", " a[i] = None\n", " \n", "def non_nulls(a):\n", " \"Return a copy of list a with None objects removed\"\n", " res = []\n", " for x in a:\n", " if x is not None:\n", " res.append(x)\n", " return res" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T67. Call `ceil` a few times to make sure you understand what it does:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T68. Compute the upper limit of the range of values to sift for different values of $n$:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T71. Make a small list to test `non_nulls`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T72. Test the function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T73. Make up a few other lists and pass them to `non_nulls`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `sieve` " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the code cell below to define your own version of the `sieve` function. The first version will simply initialize the worksheet, but gradually you will add more lines." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# define sieve here\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "T74. Here is the first version of `sieve`. Copy this code to the cell above and run the cell by tpying ⌃⏎\n", "\n", "
\n",
"def sieve(n):\n",
" worksheet = [None, None] + list(range(2,n))\n",
" return worksheet\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T75. Test the first version by calling `sieve(30)`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T76. Edit the `return` statement so it looks like this:\n",
"\n",
"\n",
"return non_nulls(worksheet)\n",
"\n",
"\n",
"Run the code cell again to update the function definition. Use the code cell below to repeat the tests you made earlier."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"T78. Add the `for` loop that iterates over the worksheet:\n",
"\n",
"\n",
"for k in range(2, ceil(sqrt(n))):\n",
" if worksheet[k] is not Null:\n",
" sift(k, worksheet)\n",
"\n",
"Run the code cell once more."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"At this point your program should look like the code in Figure 3.9. Call the program a few times to make sure it is generating a complete list of prime numbers up to a specified upper limit."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}