Theory exercises 1
Contents
Theory exercises 1¶
These exercises relate to the first lecture where we showed 2 problems, several algorithms for each problem and several programs for each algorithm.
The problems from the lecture were:
Calculate all prime numbers smaller or equal to a given bound
Calculate the product of two non negative integers
Exercises¶
What would you do if asked to say whether a positive integer number is prime or not?
Using the first algorithm we showed for deciding whether a number is prime or not (implemente in the function
is_prime
), how many times is the testn % i == 0
executeda) for
n == 100
b) for
n == 101
c) for
n == 102
d for
n == 10000000
In the lecture we plainly state that a factor of \(n\) cannot be larger than \(\lfloor \sqrt{n} \rfloor\). Eplain why this is so. Provide examples supporting your argument.
For a prime number
p
, how many times is the testn % i == 0
executed in the functionsis_prime(p)
andis_prime_better(p)
?Do a by-hand execution of the recursive algorithm for multiplication, programmed in the function
rec_mul
for numbers 123 and 321.Explain why we needed the statement
if len(m) % 2 == 1: mid+=1
in this function.Provide the mathematical argument for using only three recursive calls in Karatsuba’s algorithm.