Download Arithmetic – While loop and more Schemes and Mind Maps Computer Programming in PDF only on Docsity!
The activities in this sheet focus on arithmetic: long division, prime numbers... This is an opportunity to use the “while” loop intensively.
Lesson 1 (Arithmetic). Let us recall what Euclidean division is. Here is the division of a by b , a is a positive integer, b is a strictly positive integer (with the example of 100 divided by 7):
q
r
b^ a
quotient
remainder
We have the two fundamental properties that define q and r :
a = b × q + r and 0 6 r < b
For example, for the division of a = 100 by b = 7: we have the quotient q = 14 and the remainder r = 2 that verify a = b × q + r because 100 = 7 × 14 + 2 and also r < b because 2 < 7.
With Python:
- a // b returns the quotient,
- a % b returns the remainder.
It is easy to check that: b is a divisor of a if and only if r = 0.
Activity 1 (Quotient, remainder, divisibility). Goal: use the remainder to find out if one integer divides another.
- Program a function named quotient_remainder(a,b) that does the following tasks for two
integers a > 0 and b > 0:
- It displays the quotient q of the Euclidean division of a per b ,
- it displays the remainder r of this division,
- it displays True if the remainder r is positive or zero and strictly less than b , and False otherwise,
- it displays True if you have equality a = bq + r , and False if not. Here is an example of what the call should display for quotient_remainder(100,7): Division of a = 100 by b = 7 The quotient is q = 14 The remainder is r = 2 Check remainder: 0 <= r < b? True Check equality: a = bq + r? True
Note. You have to check without cheating that we have 0 6 r < b and a = bq + r , but of course it
must always be true!
- Program a function called is_even(n) that tests if the integer n is even or not. The function should return True or False. Hints. - First possibility: calculate n % 2. - Second possibility: calculate n % 10 (which returns the digit of units). - The smartest people will be able to write the function with only two lines (one for def... and the other for return...).
- Program a function called is_divisible(a,b) that tests if b divides a. The function should return True or False.
Lesson 2 (“while” loop). The “while” loop executes instructions as long as a condition is true. As soon as the condition becomes false, it proceeds to the next instructions.
while condition : instruction_ instruction_ ... other instructions
keyword "while"
a condition
colon
indented block will
be executed as long as
the condition is true
program continuation
Example. For this last loop we have already prepared a function called is_even(n) which returns True if the integer n is even and False otherwise. The loop does this: as long as the integer n is even, n becomes n/ 2. This amounts to removing all factors 2 from the integer n. As n = 56 = 2 × 2 × 2 × 7, this program displays 7.
n = 56 while is_even(n) == True: n = n // 2 print(n)
Input: n = 56 n “is n even”? new value of n 56 yes 28 28 yes 14 14 yes 7 7 no Display: 7
For the latter example, it is much more natural to start the loop with while is_even(n): Indeed is_even(n) is already a value “True” or “False”. Therefore we’re getting closer to the English sentence “while n is even...”
Operation “ += ”. To increment a number you can use these two methods: nb = nb + 1 or nb += 1 The second writing is shorter but makes the program less readable.
Activity 2 (Prime numbers).
Goal: test if an integer is (or not) a prime number.
- Smallest divisor. Program a function called smallest_divisor(n) that returns, the smallest divisor d > 2 of the
integer n > 2.
For example smallest_divisor(91) returns 7, because 91 = 7 × 13. Method.
- We remind you that d divides n if and only if n % d is equal to 0.
- It is a bad idea to use a loop “for d ranging from 2 to n ”, since, if for example we know that 7 is a divisor of 91 it is useless to test if 8 , 9 , 10... are also divisors because we have already found a smaller one.
- A good idea is to use a “while” loop! The principle is: “as long as I haven’t got my divisor, I should keep looking for”. (And so, as soon as I find it, I stop looking.)
- In practice here are the main lines: - Begin with d = 2. - As long as d does not divide n move on to the next candidate ( d becomes d + 1). - At the end d is the smallest divisor of n (in the worst case d = n ).
- Prime numbers (1).
Slightly modify your smallest_divisor(n) function to write your first prime function is_prime_1(n) which returns “True” if n is a prime number and “False” otherwise. For example is_prime_1(13) returns True, is_prime_1(14) returns False.
- Fermat numbers. Pierre de Fermat (∼1605–1665) thought that all integers of the form Fn = 2 (^2 n )^ + 1 were prime numbers. Indeed F 0 = 3, F 1 = 5 and F 2 = 17 are prime numbers. If he had known Python he would probably have changed his mind! Find the smallest integer Fn which is not prime. Hint. With Python bc^ is written b ** c and therefore a ( bc^ )^ is written a ** (b ** c).
We will improve our function which tests if a number is prime or not, it will allow us to test lots of numbers or very large numbers more quickly.
- Prime numbers (2). Enhance your previous function to become is_prime_2(n). It should not test all the divisors d from 2 to n , but only up to p n. Explanations. - For example, to test if 101 is a prime number, just see if it divisible by 2, 3 ,... , 10. It is faster! - This improvement is due to the following proposal: if an integer is not prime then it admits a
divisor d that verifies 2 6 d 6
p n.
- Instead of testing if d 6
p
n , it is easier to test if d^2 6 n.
- Prime numbers (3). Improve your function to become is_prime_3(n) using the following idea. We test if n is divisible by d = 2, but from d = 3, we just test the odd divisors (we test d , then d + 2... ). - For example to test if n = 419 is a prime number, we first test if n is divisible by d = 2, then d = 3 and then d = 5, d = 7... - This allows you to do about half less tests! - Explanations: if an even number d divides n , then we already know that 2 divides n.
- Calculation time. Compare the calculation times of your different functions is_prime() by repeating the call is_prime(97), for example, a million times. See the course below for more information on how to do this.
Lesson 3 (Calculation time). There are two ways to make programs run faster: a good way and a bad way. The bad way is to buy a more powerful computer. The good method is to find a more efficient algorithm! With Python, it is easy to measure the execution time of a function in order to compare it with the execution time of another. Just use the module timeit. Here is an example: we measure the computation time of two functions that have the same purpose, test if an integer n is divisible by 7.
First function (not very clever)
def my_function_1(n): divis = False for k in range(n): if k*7 == n: divis = True return divis
germain_after(n) function that returns the pair p , 2 p + 1 where p is the first Germain prime
number p > n.
For example, the first Germain prime number after n = 60 is p = 83, with 2 p + 1 = 167. What is the first Germain prime number after n = 100 000?