Problem Solving with Iteration
Problem Set
The following problem sets are intended for practice. Attempt these on your own.
Unique Divisor
Problem
Problem Description
Given an integer \(n\), another integer \(d\) is a divisor if \(n\) is fully divisible by \(d\). By fully divisible, we mean that there is an integer \(k\) such that \(d \times k = n\). Actually, both \(d\) and \(k\) are both divisors! What we want is to find the number of unique divisors of a number \(n\). We ensure that \(n\) is positive (i.e., \(n > 0\)) and we want only positive \(d\).
Let \(n = 6\), there are 4 unique divisors: \(1\), \(2\), \(3\) and \(6\) because \(1 \times 2 = 6\) and \(2 \times 3 = 6\).
Let \(n = 9\), there are 3 unique divisors: \(1\), \(3\), and \(9\) because \(1 \times 9 = 9\) and \(3 \times 3 = 9\).
Let \(n = 1\), there is only 1 unique divisor, which is \(1 \times 1 = 1\).
Task
Write Python code to compute and print the number of unique divisors of a number n
.
Assumptions
n
is a positive integer (i.e.,n > 0
).
Hints
Hint #1
Say we have a number d
, how do we know if d
is a divisor of n
?
Hint #2
What are the possible range of divisors of n
?
The smallest is 1 and the largest is n
.
Hint #3
What kind of loop can we use?
We can easily use fixed repetition.
Fixed Arctan
Problem
Problem Description
The Taylor series expansion for \(\text{arctan}(x)\) is also quite simple as shown below.
Task
Write Python code to compute \(\text{arctan}(x)\) approximated to n
iterations.
In other words, compute the following.
Assumptions
x
is a floating point number between 0 and 1 (i.e.,0 < x < 1
).n
is a positive integer (i.e.,n < 0
).x
andn
are initialized.
Hints
There is no hints, this should be a straightforward modification of \(\text{ln}(1 + x)\).
Precise Arctan
Problem
Problem Description
This is the same problem as above, but we will stop at 4 decimal places.
Task
Write Python code to compute \(\text{arctan}(x)\) approximated to 4 decimal places.
Assumptions
x
is a floating point number between 0 and 1 (i.e.,0 < x < 1
).x
is initialized.
Hints
There is no hints, this should be a straightforward modification of \(\text{ln}(1 + x)\).
Number of Digits
Problem
Problem Description
Given a positive integer \(n\), we want to find the number of digits of \(n\).
Task
Write Python code to compute and print the number of digits of n
.
Assumptions
n
is a positive integer (i.e.,n > 0
).
Restrictions
- You are NOT allowed to convert to other data types.
- You must work only on integer.
- Otherwise, it is simply
len(str(n))
.
Hints
Hint #1
What kind of loop can we use?
To use fixed repetition, we need to know how many times we need to loop. The number of times we need to loop is exactly the number of digits. Unfortunately, that is this current problem. So we need to use arbitrary repetitions.
Hint #2
What should be the condition to stop?
What is/are the number(s) with the smallest number of digits? Since \(n > 0\), we can say that we stop when it is a single digit. Alternatively, we can stop when \(n = 0\).
Hint #3
Given the condition to stop, what is the condition to continue?
- Stop when number of digit is one:
while n >= 10:
- Stop when \(n\) is zero:
while n > 0:
Hint #4
How do we get simpler number with fewer number of digits to help us towards the stopping condition?
We remove one digit.
Possible Solution
We will explain the solution in more details later.