In cryptography, it is important to use prime numbers. Write a function is_prime that has a parameter num and returns True if the number num is prime or False if not. A number num is prime if there does not exist a number, say x, which is between 2 and the number (not including the number) and evenly divides into number num. Note that if a number num is evenly divisible by number x that num % x will be zero. Suggest using a for x in range loop and return False as soon as a number is encountered that evenly divides into num. Only after testing that all numbers between 2 and num (exclusive of num) do not evenly divide into num should the function return True. Continuing with the theme of cryptography, we now want to provide a function get_primes_between that is passed parameters n, min_value, and max_value that will return a list of n numbers of randomly chosen values between min_value and max_value. The numbers in the returned list must be unique (i.e. no duplicates) . Python provides a function randint in module random that can be used to get a random number between two values passed as arguments. get_primes_between can use randint to randomly choose a number. Furthermore, is_prime function from the previous question can be used to test if the randomly chosen number is a prime number (you can assume is_prime is written and can be called - do not need an import statement). Suggest defining an empty list, then use a loop that will loop until there are n elements in the list. In the body of the loop randomly choose a number, test if the number is prime, and if so append the number to the list. However, do not append the number if it is already in the list. Return the list after the loop. Function get_primes_between must also validate the values of the parameters. If any of the parameters are negative return the message 'Invalid value - parameter cannot be negative'. Furthermore, min_value cannot be 0 or even 1. In such case return the message 'Invalid value - min_value must be greater than 1'. Lastly, max_value must be greater than min_value. If not return the message 'Invalid value - max_value must be greater than min_value'. Write the function get_primes_between.
In cryptography, it is important to use prime numbers. Write a function is_prime that has a parameter num and returns True if the number num is prime or False if not. A number num is prime if there does not exist a number, say x, which is between 2 and the number (not including the number) and evenly divides into number num. Note that if a number num is evenly divisible by number x that num % x will be zero. Suggest using a for x in range loop and return False as soon as a number is encountered that evenly divides into num. Only after testing that all numbers between 2 and num (exclusive of num) do not evenly divide into num should the function return True.
Continuing with the theme of cryptography, we now want to provide a function get_primes_between that is passed parameters n, min_value, and max_value that will return a list of n numbers of randomly chosen values between min_value and max_value. The numbers in the returned list must be unique (i.e. no duplicates) .
Python provides a function randint in module random that can be used to get a random number between two values passed as arguments. get_primes_between can use randint to randomly choose a number. Furthermore, is_prime function from the previous question can be used to test if the randomly chosen number is a prime number (you can assume is_prime is written and can be called - do not need an import statement). Suggest defining an empty list, then use a loop that will loop until there are n elements in the list. In the body of the loop randomly choose a number, test if the number is prime, and if so append the number to the list. However, do not append the number if it is already in the list. Return the list after the loop.
Function get_primes_between must also validate the values of the parameters. If any of the parameters are negative return the message 'Invalid value - parameter cannot be negative'. Furthermore, min_value cannot be 0 or even 1. In such case return the message 'Invalid value - min_value must be greater than 1'. Lastly, max_value must be greater than min_value. If not return the message 'Invalid value - max_value must be greater than min_value'.
Write the function get_primes_between.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images