Question: Write a program that figures out how many prime numbers exist within an arbitrary, random range of numbers. See next page for more on the

Write a program that figures out how many prime numbers exist within an arbitrary, random range of numbers.

See next page for more on the "random range" part.

See second-to-next page for tips on how to determine a number's "prime-ness" using simple deduction.

Requirements:

As per usual, be sure to leave some comments (in your code) on how certain complex / unique parts of your program work.

You MUST use a loop in your program, where the code you have inside the body, is the main determining factor of figuring out a number's "prime-ness".

Double check your algorithm's logic to make sure the following numbers are NOT counted as prime:

One, zero, and all negative values

Like always, be sure your output is easy to read and understand. At the bare minimum, make sure the message of what the meaning of the final tally result being printed is clear to the [oblivious] user.

Tips:

Get started by coming up with code that figures out just a single number's "prime-ness" first, then you can make adjustments to your solution afterwards to incorporate the range of values aspect using a [normal] loop.

You can use hardcoded values for the min / max range limits for testing purposes; but keep in mind that your code should work for ANY given range of real numbers. Here are some cases you should try:

0 to 10 ==> has 4 primes: 2, 3, 5, and 7

-10 to 0 ==> has 0 primes

-7 to 7 ==> has 4 primes: 2, 3, 5, and 7

-50 to 50 ==> has 15 primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47

Think about what major aspects of your code would "fit" within the notion of using "loop logic" as you're [very likely] going to need to use a nested loop.

Set up a Boolean variable to keep track a number's "prime-ness" over time.

How to generate a "random range"

You'll need to have two variables that represent the 'start' and 'end' points of your [imaginary] range. So, in order to initialize them with random values (each time your program runs), you'll need to make use of the following code:

(int) -(50 * Math.random()) //You can replace '50' with //whatever MIN range value you want

(int) (50 * Math.random()) //You can replace '50' with //whatever MAX range value you want

How to determine a number's "prime-ness"

There are a number of different ways you can go about solving this part, however the approach I recommend you using is to:

"Assume the number IS prime, until proven FALSE"

How do we prove that a number is not prime? Simply find a divisor from 2 to (number - 1) that divides evenly into the number then we know that the number is no longer prime.

For example, assume we have "number = 9", so the potential divisors are 2 8. Does 9 / 2 have a remainder, yes, so move on to next divisor. Does 9 / 3 have a remainder, no, NOW we know '9' is NOT a prime.

Another example, assume we have "number = 5", so the potential divisors are 2 4. Does 5 / 2 have a remainder, yes, so move on to next divisor. Does 5 / 3 have a remainder, yes, so move on to next divisor.

Does 5 / 4 have a remainder, yes, so move on to next divisor.

No more divisors to check, this [logically] means that '5' MUST be prime.

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!