PW 3: Sub-Program (Functions) : UE 104 Introduction To Computer Science 1
PW 3: Sub-Program (Functions) : UE 104 Introduction To Computer Science 1
PW 3: Sub-Program (Functions) : UE 104 Introduction To Computer Science 1
PW 3: Sub-program (functions)
Goal
1
UE 104 Introduction to Computer Science 1
Exercice 2:
● Can we use the same parameter name for sp1 and sp2?
● When does the ‘a’ parameter of sp1 exist?
● How to understand the expression sp1 (a = a) in main?
● What is the output of this program?
● Run the program with http://www.pythontutor.com
2
UE 104 Introduction to Computer Science 1
Exercice 3:
Write a program that calculates the nth power of a number using only sum and multiplication
● don’t use **
● n and the number itself is given by the user.
● Don’t forget to write a function that contains asserts to verify your code.
● "Docstring" is obligatory
Exercice 4:
A natural number is said to be perfect if it is equal to the sum of its divisors, excluding it. For example,
28 is a perfect number (28 = 1 + 2 + 4 + 7 + 14).
Two numbers N and M are said to be friends if the sum of the divisors of M (excluding M itself) is
equal to N and the sum of the divisors of N (excluding N itself) is equal to M. For example, 220 and
284 are friends. Indeed, the sum of the divisors of 220 out of 220 is
3
UE 104 Introduction to Computer Science 1
Our goal is to write two subprograms. The first displays in ascending order and progressively the
perfect numbers from 2 to a given natural number. The second displays all the friendly numbers (N,
M) between 2 and a given natural number MAX such as 0 <N ≤ M ≤ MAX.
Before coding in Python, write the algorithm of the problem using the refining method.
Once done, you can write your code. DON’T FORGET THE “Docstring”
4