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.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter8: Arrays And Strings
Section: Chapter Questions
Problem 21PE
icon
Related questions
Question

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.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr