{ "cells": [ { "cell_type": "markdown", "metadata": { "toc": true }, "source": [ "
Using Spyder (or your own prefered method), write a Python script to output the first 20 prime numbers.
\n", "\n", "Tips:\n", "a
divides b
exactly, then a%b==0
, which gives a quick test. When testing your code, you should expect the output for the first 5 primes to be [2, 3, 5, 7, 11]
.
If you have time, convert the script into a function to calculate all prime numbers smaller than an input, $n$.
\n", "\n", "A [model solution](https://msc-acse.github.io/ACSE-1/lectures/lecture7-solutions.html#exercise1) for the script is available.\n", "Write a script to calculate the mean of a sequence of numbers. If you have time, make it take extra options (using the `argparse` module) -b
, -o
and -x
to work with with binary (i.e. base 2, with 101 == 5
decimal), octal (i.e. base 8, with 31 == 25
decimal) and hexadecimal (i.e. base 16 2A == 42
decimal) numbers.
Test your basic script on the following sequences: 1
(mean 1) 1 5 7 13 8
(mean 6.8), 2.5, 4 ,-3.2, 9.3
(mean 3.15).
Also try feeding it no input.
\n", "\n", "_Tips_:\n", "\n", "For the longer version can use the 2 argument version of the int
function to change the base of numbers. For example int('11',2) == 3
and int('3A', 16)==58
.
Write a script to plot the functions $y=\\sin(x)$, $y=\\cos(x)$ and $y=\\tan(x)$ to screen over the range [0,$2\\pi$] and then run it in a terminal/prompt.
\n", "\n", "Make sure to include labels on your axes.
\n", "\n", "Change the script to output a .png
file to disk.
Next do the same to write a .pdf
.
Model answers are available.
\n", "\n", "Copy the following script into your IDE and run the static analysis tool (`pylint`) on it. Fix the errors and warnings that it gives you.
\n", "Write a function which accepts a real number and returns the complex square roots of that number.
\n", "\n", "Your function should include a docstring conforming to the numpydoc standard.
\n", "\n", "_Tips_: \n", "Questions: how many complex square roots does each real number have? Is it the same for **every** real number?
\n", "\n", "A model answer is available.
\n", "\n", "\n", "\n", "The minute that code is going to be read a second time (including by you in two months time) then it becomes unacceptable to write it as though it is disposable. Functions need docstrings, and variables should have names which make sense (and not just to you now).\n", "\n", "Similarly, **when you've tested your code**, and you know that a specific function takes 90% of the runtime, it may make sense to rewrite it in a faster way, even if that is harder to maintain (more `numpy`, writing your own `C` extension modules, and so on).\n", " \n", "Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
Turn your \"find the primes\" module file into a package called `primes` by creating a suitable directory structure and an `__init__.py` so that you can access a function to give you the first $n$ primes as well as all primes smaller than $n$.
\n", "\n", "Try `import`ing your new package from the IPython console. Check that you can call your function.
\n", "\n", "If you have time, add a function to the package to give you a list of the prime factors of an integer.
\n", "\n", "A model answer is available.
\n", "\n", "Make a `setup.py` script for your module and try `install`ing and `uninstall`ing it using `pip`. In the directory containg the `setup.py` file run
\n", " \n", "```\n", "pip install .`\n", "```\n", "\n", "and\n", "\n", "```\n", "pip uninstallFrom another directory, see when you can and can't import your new module.
\n", " \n", "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
\n", "\n", "Find the sum of all the multiples of 3 or 5 below 1000.
\n", " \n", "2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
\n", "\n", "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
\n", "\n", "73167176531330624919225119674426574742355349194934\n", "96983520312774506326239578318016984801869478851843\n", "85861560789112949495459501737958331952853208805511\n", "12540698747158523863050715693290963295227443043557\n", "66896648950445244523161731856403098711121722383113\n", "62229893423380308135336276614282806444486645238749\n", "30358907296290491560440772390713810515859307960866\n", "70172427121883998797908792274921901699720888093776\n", "65727333001053367881220235421809751254540594752243\n", "52584907711670556013604839586446706324415722155397\n", "53697817977846174064955149290862569321978468622482\n", "83972241375657056057490261407972968652414535100474\n", "82166370484403199890008895243450658541227588666881\n", "16427171479924442928230863465674813919123162824586\n", "17866458359124566529476545682848912883142607690042\n", "24219022671055626321111109370544217506941658960408\n", "07198403850962455444362981230987879927244284909188\n", "84580156166097919133875499200524063689912560717606\n", "05886116467109405077541002256983155200055935729725\n", "71636269561882670428252483600823257530420752963450\n", "\n", "
The four adjacent digits in this number that have the greatest product are 9 × 9 × 8 × 9 = 5832. Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
\n", "\n", "In this lecture we learned:\n", "\n", "
Tomorrow:\n", "
git
.