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.

  1. Write a function that checks if the given integer argument is a prime.

Python signature

def is_prime(n: int) -> bool:
  1. 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;
}
  1. Write a program to solve project Euler problem 1. Use different data structures, language constructs, and libraries and write at least three distinct solutions.

  2. The famous Collatz-Kakutani-Ulam sequence aka the Collatz sequence aka the hailstone sequence is defined as follows:

    1. Start with any positive integer

    2. If the integer is even divide it by 2

    3. If the integer is odd multiple by 3 and add 1.

    4. 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