Triangular and prime numbers¶
Save this function and the next one into a file named
triangulars.py. Write the functionis_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, ifnis 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.txtWrite the function
triangulars()that given an integer,maxn, gets the triangular numbers from 1 tomaxn(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.txtSave this function and the next ones into a file named
prime.py. Write the functionis_prime()that, given an integer, returnsTrueif it is a prime number orFalseotherwise. 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.txtWrite an improved version of the previous function,
improved_is_prime(), taking into account the two following properties: ifncan’t be divided by 2, it can’t be divided by any even number, and all the divisors ofngreater 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.txtWrite the function
iterations_is_prime()that, given a integer, returnsTrueif it is prime orFalseotherwise. 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.txtWrite 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.txtWrite the function
prime()that, given an integer,maxn, returns a list with the prime numbers less than or equal tomaxn(included). This function must call functionimproved_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.