Triangular and prime numbers

  1. Save this function and the next one into a file named triangulars.py. Write the function is_triangular() that given an integer, n, checks whether or not it is a triangular number. An integer, n, is said to be a triangular number if it exists a base, b, such that \(\sum_{i=1}^b i = n\). The function must return an integer corresponding to the base, b, if n is triangular number or a zero otherwise. Examples:

    >>> is_triangular (15)
    5
    >>> is_triangular (27)
    0
    >>> is_triangular (66)
    11
    >>> is_triangular (44)
    0
    

    Note

    More tests are provided in file is_triangular.txt

  2. Write the function triangulars() that given an integer, maxn, gets the triangular numbers from 1 to maxn (both included). The function must return a list of tuples, where each tuple represents a triangular number and is made up of two elements: the triangular number, n, and the corresponding base, b. Examples:

    >>> triangulars (10)
    [(1, 1), (3, 2), (6, 3), (10, 4)]
    >>> triangulars (50)
    [(1, 1), (3, 2), (6, 3), (10, 4), (15, 5), (21, 6), (28, 7), (36, 8), (45, 9)]
    

    Note

    More tests are provided in file triangulars.txt

  3. Save this function and the next ones into a file named prime.py. Write the function is_prime() that, given an integer, returns True if it is a prime number or False otherwise. Examples:

    >>> is_prime(1)
    False
    >>> is_prime(2)
    True
    >>> is_prime(53)
    True
    >>> is_prime(63)
    False
    

    Note

    More tests are provided in file is_prime.txt

  4. Write an improved version of the previous function, improved_is_prime(), taking into account the two following properties: if n can’t be divided by 2, it can’t be divided by any even number, and all the divisors of n greater than \(int(\sqrt{n})\) are redundant. See Primality tests . Examples:

    >>> improved_is_prime(1)
    False
    >>> improved_is_prime(2)
    True
    >>> improved_is_prime(53)
    True
    >>> improved_is_prime(63)
    False
    

    Note

    More tests are provided in file improved_is_prime.txt

  5. Write the function iterations_is_prime() that, given a integer, returns True if it is prime or False otherwise. This function must also count and return the number of performed iterations. Examples:

    >>> iterations_is_prime(1)
    (False, 0)
    >>> iterations_is_prime(2)
    (True, 0)
    >>> iterations_is_prime(53)
    (True, 51)
    >>> iterations_is_prime(63)
    (False, 2)
    

    Note

    More tests are provided in file iterations_is_prime.txt

  6. Write the improved version of the previous function, improved_iterations_is_prime(). Compare the number of iterations of this improved version with that of the not improved one. Examples:

    >>> improved_iterations_is_prime(1)
    (False, 0)
    >>> improved_iterations_is_prime(2)
    (True, 0)
    >>> improved_iterations_is_prime(53)
    (True, 3)
    >>> improved_iterations_is_prime(63)
    (False, 1)
    

    Note

    More tests are provided in file improved_iterations_is_prime.txt

  7. Write the function prime() that, given an integer, maxn, returns a list with the prime numbers less than or equal to maxn (included). This function must call function improved_is_prime(). Examples:

    >>> prime(10)
    [2, 3, 5, 7]
    >>> prime(50)
    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
    >>> prime(5)
    [2, 3, 5]
    

Note

More tests are provided in file prime.txt

Solution

A solution of these functions is provided in files triangulars.py and prime.py.