Question: Description Develop a function that identifies the set of prime numbers within a range from 1 to n, where n is the highest number that

Description

Develop a function that identifies the set of prime numbers within a range from 1 to n, where n is the highest number that it tests for. A prime number is a whole number greater than 1 whose only factors are 1 and itself. Factors are whole numbers that can be divided evenly into another number[1] (Links to an external site.)Links to an external site.. For example:

2 is a prime number because its only factors are 1 and 2

3 is a prime number because its only factors are 1 and 3

4 is not a prime number because its factors are 1 (1x4), 2 (2x2), and 4 (4X1).

Basic requirements

Pass the upper limit of the range of integers that you will check for primes

Store the resulting set of prime numbers in a list and output the list to the screen when you code finishes processing.

Include some basic error checking to handle scenarios that would cause your function to fail or prevent the proper identification of the set of prime numbers (e.g. passing a number <=2, passing a non-numeric value, passing non-integer values, etc.)

Optional:

If you have a good handle on the basic functionality, add one or more of these extras:

Add support to pass both an upper and lower limit for the range of integers

Add support to run the function more than once automatically and input a different upper limit/range before each pass

Include documentation (e.g. comments, docstrings) to help a user understand the elements of your function

Advice:

Code your loops to be as efficient as possible. For example, if you find a factor of a number between 1 and the number you are testing, move on to the next integer in the range rather than continuing to search for other possible factors for the current number. So when checking whether 4 is a prime number, you already know 1 and 4 are factors. Therefore, your loop should start at 2 and stop when it determines 2 is a factor. Another way to think of this is when you find a third factor, move on to the next number in your range to continue processing.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!