Python 05
Python 05
Solutions must be submitted into Moodle as a single text 2ile, with name lab05.py,
into Moodle. Don’t use an archiver.
Your 2ile must contain two functions de2initions, sine and collatz.
Goal of this exercise is that you learn how to run a Python program, that you
become familiar with the lay out of a Python program, and that you learn some
basic functions of Python.
Make sure that you are using Python version 3.10 or higher.
Test your implementation for real numbers and for complex numbers. You
!"#$
can stop when the factor (!"#$)! becomes very small, or repeat up to a high
number, for example 50.
Make sure that your function starts with def sine(x) : and that it can
be called as lab05.sine from the Python interpreter.
2. The Collatz sequence is de2ined as follows: If n is even, then the next number
is n/2. If n is odd, then the next number is 3n + 1. For example, starting with
6 gives the following sequence:
6 ⇒ 3 ⇒ 10 ⇒ 5 ⇒ 16 ⇒ 8 ⇒ 4 ⇒ 2 ⇒ 1.
1
’q’, or ’Q’ then the program stops. If the user types a number less or equal to
one, then the program must tell that the number is too small and ask for a
new number. If the number is greater than one, the program should show
the Collatz sequence for this number. Your function must start with def
collatz( ) : Here is an example:
>> lab05.collatz( )
Please type a number greater than
one or ’quit’ to quit
6
Giving Collatz sequence for 6
iteration 1 results in 6
iteration 2 results in 3
iteration 3 results in 10
iteration 4 results in 5
iteration 5 results in 16
iteration 6 results in 8
iteration 7 results in 4
iteration 8 results in 2
iteration 9 results in 1