Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Arithmetic – While loop, Schemes and Mind Maps of Computer Programming

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

2022/2023

Uploaded on 03/01/2023

gaurish
gaurish 🇺🇸

4.6

(12)

5 documents

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download Arithmetic – While loop and more Schemes and Mind Maps Computer Programming in PDF only on Docsity! Arithmetic – While loop – I 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 ab quotient remainder 14 2 1007 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. 1. 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, ARITHMETIC – WHILE LOOP – I 2 • 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! 2. 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...). 3. 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_1 instruction_2 ... other instructions keyword "while" a condition colon indented block will be executed as long as the condition is true program continuation ARITHMETIC – WHILE LOOP – I 5 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. 3. Fermat numbers. Pierre de Fermat (∼1605–1665) thought that all integers of the form Fn = 2(2 n) + 1 were prime numbers. Indeed F0 = 3, F1 = 5 and F2 = 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(b c) 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. 4. 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 d2 6 n. 5. 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. 6. 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 ARITHMETIC – WHILE LOOP – I 6 # Second function (faster) def my_function_2(n): if n % 7 == 0: return True else: return False # Measurement of execution times import timeit print(timeit.timeit("my_function_1(1000)", setup="from __main__ import my_function_1", number=100000)) print(timeit.timeit("my_function_2(1000)", setup="from __main__ import my_function_2", number=100000)) Results. The result depends on the computer, but allows the comparison of the execution times of the two functions. • The measurement for the first function (called 100 000 times) returns 5 seconds. The algorithm is not very clever. We’re testing if 7× 1= n, then test 7× 2= n, 7× 3= n. . . • The measurement for the second function returns 0.01 second! We test if the remainder of n divided by 7 is 0. The second method is therefore 500 times faster than the first. Explanations. • The module is named timeit. • The function timeit.timeit() returns the execution time in seconds. The function takes the following parameters: – a string for the call of the function to be tested (here we ask if 1000 is divisible by 7), – an argument setup="..." which indicates where to find this function, – the number of times you have to repeat the call to the function (here number=100000). • The number of repetitions must be large enough to avoid uncertainties. Activity 3 (More prime numbers). Goal: program more “while” loops and study different kinds of prime numbers using your is_prime() function. 1. Write a prime_after(n) function that returns the first prime number p greater than or equal to n. For example, the first prime number after n= 60 is p = 61. What is the first prime number after n= 100000? 2. Two prime numbers p and p+2 are called twin prime numbers. Write a twin_prime_after(n) function that returns the first pair p, p+ 2 of twin prime numbers, with p > n. For example, the first pair of twin primes after n= 60 is p = 71 and p+ 2= 73. What is the first pair of twin primes after n= 100000? 3. An integer p is a Germain prime number if p and 2p + 1 are prime numbers. Write a ARITHMETIC – WHILE LOOP – I 7 germain_after(n) function that returns the pair p, 2p+ 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 2p+ 1= 167. What is the first Germain prime number after n= 100 000?
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved