Problem Set 2¶
Write good, clean, elegant code.
Focus on good naming, functional decomposition and testing.
Writing auxiliary functions that simplify the task and/or add to the utility of the function to be written is strongly encouraged.
Write a function that checks if the given integer argument is a prime.
Python signature
def is_prime(n: int) -> bool:
Write a function to factorize a given positive integer. Use it to solve project Euler problem 3.
C++ signature
vector<int> factorize(int a) {
vector<int> factors;
return factors;
}
Java signature
public List<Integer> factorize(int a) {
List<Integer> factors = new ArrayList<>();
return factors;
}
Write a program to solve project Euler problem 1. Use different data structures, language constructs, and libraries and write at least three distinct solutions.
The famous Collatz-Kakutani-Ulam sequence aka the Collatz sequence aka the hailstone sequence is defined as follows:
Start with any positive integer
If the integer is even divide it by 2
If the integer is odd multiple by 3 and add 1.
Using the newly obtained number, repeat from step 2
For example, starting number 7 gives you the sequence: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.
If you try out a few more you will notice that all of them end in 4, 2, 1 (since applying the steps to 1 will repeat 4, 2, 1). Write a program that prints out the sequence for the given argument.
Remember: the sequence includes the starting number and ends in 4, 2, 1