Triangular and prime numbers ============================= #. Save this function and the next one into a file named ``triangulars.py``. Write the function :py:func:`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 :math:`\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: .. literalinclude:: is_triangular.txt :language: python3 :lines: 3-10 .. note:: More tests are provided in file :download:`is_triangular.txt ` #. Write the function :py:func:`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: .. literalinclude:: triangulars.txt :language: python3 :lines: 3- .. note:: More tests are provided in file :download:`triangulars.txt ` #. Save this function and the next ones into a file named ``prime.py``. Write the function :py:func:`is_prime` that, given an integer, returns ``True`` if it is a prime number or ``False`` otherwise. Examples: .. literalinclude:: is_prime.txt :language: python3 :lines: 3-10 .. note:: More tests are provided in file :download:`is_prime.txt ` #. Write an improved version of the previous function, :py:func:`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 :math:`int(\sqrt{n})` are redundant. See `Primality tests `_ . Examples: .. literalinclude:: improved_is_prime.txt :language: python3 :lines: 3-10 .. note:: More tests are provided in file :download:`improved_is_prime.txt ` #. Write the function :py:func:`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: .. literalinclude:: iterations_is_prime.txt :language: python3 :lines: 3-10 .. note:: More tests are provided in file :download:`iterations_is_prime.txt ` #. Write the improved version of the previous function, :py:func:`improved_iterations_is_prime`. Compare the number of iterations of this improved version with that of the not improved one. Examples: .. literalinclude:: improved_iterations_is_prime.txt :language: python3 :lines: 3-10 .. note:: More tests are provided in file :download:`improved_iterations_is_prime.txt ` #. Write the function :py:func:`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 :py:func:`improved_is_prime`. Examples: .. literalinclude:: prime.txt :language: python3 :lines: 3- .. note:: More tests are provided in file :download:`prime.txt ` .. rubric:: Solution A solution of these functions is provided in files :download:`triangulars.py ` and :download:`prime.py `.