Solutions#

Question 1#

1. Using a for loop print the types of the variables in each of the following iterables:

1. iterable = (1, 2, 3, 4)

for variable in (1, 2, 3, 4):
    print(type(variable))
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

2. iterable = (1, 2.0, 3, 4.0)

for variable in (1, 2.0, 3, 4.0):
    print(type(variable))
<class 'int'>
<class 'float'>
<class 'int'>
<class 'float'>

3. iterable = (1, "dog", 0, 3, 4.0)

for variable in (1, "dog", 0, 3, 4.0):
    print(type(variable))
<class 'int'>
<class 'str'>
<class 'int'>
<class 'int'>
<class 'float'>

Question 2#

2. Consider the following polynomial:

\[ 3 n ^ 3 - 183n ^ 2 + 3318n - 18757 \]

1. Use the sympy.isprime function to find the lowest positive integer value of \(n\) for which the absolute value of that polynomial is not prime?

Start by defining the cubic:

import sympy as sym


def cubic(n):
    """
    Return the value of the absolute value of the cubic for the given value of n
    """
    return abs(3 * n ** 3 - 183 * n ** 2 + 3318 * n - 18757)

Increment n until cubic(n) is no longer prime:

n = 1
while sym.isprime(cubic(n)) is True:
    n += 1

n
47

2. How many unique primes up until the first non prime value are there? (Hint: the set tool might prove useful here.)

primes = [cubic(n_value) for n_value in range(1, n)]
unique_primes = set(primes)
unique_primes
{37,
 41,
 59,
 109,
 229,
 409,
 419,
 499,
 739,
 829,
 877,
 1201,
 1531,
 1597,
 1669,
 1823,
 1999,
 2389,
 2749,
 2917,
 3061,
 3271,
 3307,
 3469,
 3491,
 3529,
 4789,
 5441,
 6367,
 7691,
 8221,
 10259,
 10369,
 12829,
 13163,
 15619,
 16421,
 20051,
 24071,
 28499,
 33353,
 38651}

Let us count them:

len(unique_primes)
42

Question 3#

3. Check the following identify for each value of \(n\in\{0, 10, 100, 2000\}\): \( \sum_{i=0}^n i=\frac{n(n+1)}{2} \)

Define a function to check the identity:

def check_identity(n):
    """
    Computes lhs and the rhs of the given identity.
    """
    lhs = sum(i for i in range(n + 1))
    rhs = n * (n + 1) / 2
    return lhs == rhs

Checks the identify for the given values:

all(check_identity(n) for n in (0, 10, 100, 2000))
True

Question 4#

4. Check the following identify for all positive integer values of \(n\) less than 5000: \( \sum_{i=0}^n i^2=\frac{n(n+1)(2n+1)}{6} \)

Define a function to check the identity:

def check_identity(n):
    """
    Computes lhs and the rhs of the given identity.
    """
    lhs = sum(i ** 2 for i in range(n + 1))
    rhs = n * (n + 1) * (2 * n + 1) / 6
    return lhs == rhs

Checks the identify for the given values:

all(check_identity(n) for n in range(1, 5001))
True

Question 5#

5. Repeat the experiment of selecting a random integer between 0 and 10 until it is even 1000 times (see Repeat code while a given condition holds). What is the average number of times taken to select an even number?

Write a function to repeat the code from Repeat code while a given condition holds:

import random


def count_number_of_selections_until_even(seed):
    """
    Repeatedly sample an integer between 0 and 10 for a given random seed.

    This function returns the number of selections needed.
    """
    random.seed(seed)
    selected_integer = random.randint(0, 10)
    number_of_selections = 1
    while selected_integer % 2 == 1:
        selected_integer = random.randint(0, 10)
        number_of_selections += 1
    return number_of_selections

Now use this for 1000 random repetitions (we use each repetition as a seed):

number_of_selections = [
    count_number_of_selections_until_even(seed) for seed in range(1000)
]
number_of_selections
[1,
 1,
 1,
 3,
 2,
 2,
 4,
 2,
 3,
 4,
 2,
 2,
 2,
 1,
 3,
 2,
 4,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 1,
 2,
 1,
 2,
 1,
 1,
 1,
 3,
 2,
 1,
 1,
 2,
 1,
 1,
 2,
 3,
 1,
 1,
 1,
 1,
 1,
 2,
 3,
 1,
 3,
 2,
 2,
 1,
 4,
 1,
 3,
 1,
 1,
 4,
 3,
 1,
 2,
 2,
 3,
 3,
 1,
 2,
 3,
 2,
 1,
 2,
 2,
 3,
 1,
 2,
 3,
 3,
 1,
 3,
 1,
 1,
 1,
 1,
 4,
 1,
 11,
 1,
 1,
 1,
 3,
 4,
 3,
 1,
 7,
 1,
 1,
 3,
 2,
 3,
 1,
 1,
 3,
 1,
 6,
 1,
 5,
 2,
 4,
 1,
 1,
 1,
 5,
 2,
 1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