



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Goal: use the remainder to find out if one integer divides another. 1. Program a function named quotient_remainder(a,b) that does the following tasks for two.
Typology: Schemes and Mind Maps
1 / 7
This page cannot be seen from the preview
Don't miss anything!




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):
We have the two fundamental properties that define q and r :
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:
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.
must always be true!
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"
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.
For example smallest_divisor(91) returns 7, because 91 = 7 × 13. Method.
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.
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.
p n.
p
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.
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
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?