Summary exercises#

Exercise 1#

Write a function containing a for loop and a conditional such that it prints the even numbers up to 100.

Exercise 2#

Write a function which sums the numbers up to n, and returns the result. Test it.

Exercise 3#

Using some of the stuff defined above, find $\( \sum_{k=0}^{100} \frac{4(-1)^k}{2k+1},\)$ write your code in the cell (you may add more cells) below. We showed two examples for computing sums here, one using a for loop and a running count, one using the sum function, try using both. Which is more appropriate here? Discuss!

Exercise 4#

  • Create a list of the first 100 natural numbers. A natural number is a non-negative integer, though some definitions omit 0.

  • Square (multiply the number by itself) all the numbers in the list which are not divisible by 7. Remember that ‘divide and give remainder’ is the % operator.

Exercise 5#

Find $\( \sum_{k=0}^{100} \frac{4}{(4k+1)(4k+3)},\)$ write your code in the cell (you may add more cells) below.

Exercise 6#

Create a function consec_integers that has two inputs:

  • a, an integer

  • b, an integer

and it returns a list of all integers between \(a\) and \(b\) (including them).

Test your function with a few inputs.

Exercise 7#

To check if \(x\) is an element of a list mylist you may use: $\(\verb|x in mylist |\)$

To check if \(x\) is not an element of a list mylist you may use: $\(\verb|x not in mylist |\)$ Create a function common_element that has two inputs:

  • listA, a list of some elements,

  • listB, a list of some elements.

It should return all elements that are both in listA and listB, so common elements of these two lists.

Test your function with the following lists:

  • \([1, 4, 5, 6, 8, 9, 13, 16, 18, 23]\),

  • \([5, 8, 11, 12, 13, 15, 17, 18]\)

Exercise 8#

Write a nested for loop (one loop inside another) that prints a truth table for the ‘and’ operator. Use two variables a and b that take all combinations of True and False, and show the value of (a and b). For example, you should have

a b => (a and b)
True True => True
True False => False
False True => False
False False => False

Exercise 9#

In the cell below, by using the for loop, calculate and print the following sum $\( \sum_{k=m}^n \frac{(k+1)^2}{k^3},\)\( where \)m=10\(, \)n=100$.

Exercise 10#

Compute the first 15 numbers in the Fibonacci series (we’ve described this in previous classes …. or Google it) that are greater than 10 and are multiples of 3. The 15th number should be 10610209857723.

Exercise 11 - The factorial function#

Let us, again, consider a factorial of a natural number \(n\) where n is a positive integer. It is defined as follows $\( n! = 1 \cdot 2 \cdot 3 \cdots (n-1)\cdot n,\)\( and \)0! = 1$.

We can write this in shorthand using the big pi operator.

\[n ! = \prod_{k=1}^n k\]

So big pi is a similar operator to \(\Sigma\) but represents a product rather than a sum.

Write a factorial function using a similar approach to our summation function (except using multiplication instead of addition!)

Exercise 12#

Calculate $\( \prod_{k=1}^{10} \frac{1}{k^2}\)$

(The answer should be very small!)

Exercise 13#

The \(n\)-th term of the arithmetic sequence $\( a_1, a_2, \ldots, a_n \ldots \)\( is given by \)\( a_n = a_1 + (n-1)d,\)\( where \)a_1\( is the first term of the sequence, and \)d$ is the common difference of successive members.

Write a function arithm_seq with three inputs:

  • a1 (any numerical)

  • n (positive int)

  • d (any numerical)

so that it returns the n-th element of the arithmetic sequence with these parameters.

Given that \(a_1 = 3\), \(n = 17\) and \(d = \frac{4}{3}\) use your function geo_seq to print \(a_{17}\).

Then find the sum $\(\sum_{k=1}^{100}a_k\)\( of first \)100\( terms of the arithmetic progression, with \)a_1 = 5\(, \)d = \frac{3}{2}$.

Exercise 14#

Write a function which determines if an integer is prime (meaning it has no number other than one that divides it without a remainder). Remember the remainder operator is x % y. This function will need a loop and a conditional.

You can check if two things are not equal either with

if x != y:

or

if not x == y:

Exercise 15 - Challenging#

In 14th century an Indian mathematician and astronomer, Madhava of Sangamagrama, gave a method for computing the value of \(\pi\) with precision to \(11\) decimal places.

He showed that $\( \pi \approx \sqrt{12}\sum_{k=0}^{21} \frac{(-3)^{-k}}{2k+1} = \sqrt{12}\sum_{k=0}^{21} \frac{(-\frac{1}{3})^k}{2k+1} = \sum_{k=0}^{21} \sqrt{12} \frac{(-\frac{1}{3})^k}{2k+1}\)$ is correct to 11 decimal places

Use this expression to compute \(\pi\) and check it’s accuracy against math.pi

Exercise 16 - Challenging#

In 1656 English mathematician, John Wallis, approximated \(\pi\) with products, that is, $\(\pi = 2 \prod_{n=1}^{M} \frac{ 4n^2 }{ 4n^2 - 1 } \)\( when \)M$ becomes large (converges to infinity).

As you increase \(M\) the product will get closer to \(\pi\).

Compute this with the largest value of \(M\) that you can (NOTE: using M = 210938120983723897546823764 will not work as the code will never finish!) How accurate can you make this?

Exercise 17#

Write a function to return the first 10 even prime numbers.

NOTE Sometimes as mathematicians/physicists if you understand the question being asked you can solve it much quicker than someone without your background! Think carefully about this one!

Exercise 18 Caesar Shift - Challenge#

Write functions to impliment (code and decode) a Caesar Shift cipher.

Use your functions to decode “KYV ERKZFERC TZGYVI TYRCCVEXV JKRIKJ FE KYV JZOKY FW FTKFSVI. ZK JKRIKJ HLZKV VRJP SLK JFFE XVKJ UZWWZTLCK. ZK ZJ WIVV KF VEKVI.”

There are several ways to do this. One way that uses only skills we have covered so far, and the fact that Python has a function ‘ord’ that returns 65 for ‘A’ and 90 for ‘Z’, is outlined below:

  1. Define a string that contains the alphabet in upper case letters.

  2. Write a function that slices the alphabet, making a new string with letters shifted n places, and wrapped so all letters are present. I’ll call this the key.

  3. Write a new function that creates a new string containing the coded/decoded text, by examining each letter in the text in turn and adding the equivalent letter from the key to the new string.

  4. Write a short function that prints all 26 possible Caesar shift versions of the text. One of them should make sense.

Things to consider: How to cope with a character that is not in the alphabet. Ciphertext is conventionally in upper case, with plaintext in lower case. The method described above is a brute force approach. You could use frequency analysis on a long text, or look for single-letter words.