Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A prime number is a whole number which has only two divisors, I and itself. The simplest way to test primality of a number

image text in transcribed 

A prime number is a whole number which has only two divisors, I and itself. The simplest way to test primality of a number is to keep dividing it by prime numbers that are smaller than or equal to the floor of its square root. If the number is not divisible with any of those primes, it is a prime itself, otherwise it is not a prime. For instance, to test whether 23 is prime, you need to have the list of all prime numbers smaller than or equal to the floor of the square root of 29, i.e. 5, which are 2, 3 and 5 (Note that the square root of 29 is 5.3851 so its floor is 5). Since 29 is not divisible by 2, 3 or 5, it is prime. To do the primality test for 63, we first need to find all prime numbers that are smaller than or equal to the floor of the square root of 63 (i.e. 7), which an 2,3,5 and 7. We know that 3 divides 63 and therefore 63 is not a prime. To check whether a divides b, we can just see if the remainder of b divides by a is zero. In Java, we can simply check condition ba=0. For this assignment, you need to write a program called PrimeFinder that takes two whole numbers on the command line that are greater than 0 and smaller than 1,000,001 and finds all prime numbers between the two numbers (both inclusive) and then prints them on the screen once all those numbers have been found. Note that you should not print the primes as you find them. First, find all of them and then print them out. Note: The second number supplied on the command line must be greater than or equal to the first number. Testing your program with very large number might take a long time, so try to test it with smaller numbers. As always, validate user's input and print an appropriate error message if there's something wrong. Use arrays to store the primes. You may need to use more than one array depending on how you code your program. Examples: $ java PrimeFinder 2 2 2 $ java PrimeFinder 7.001 30.50 11, 13, 17, 19, 23, 29 $ java PrimeFinder 32 21 Error: max must be greater than or equal to min. $ java PrimeFinder 2 Error: Wrong number of arguments Usage: PrimeFinder $ java PrimeFinder 20 x Error: max must be a number $ java PrimeFinder -20 20 Error: min must be between 0 and 1,000,000. Activate Windows Go to Settings to activate

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Java public class PrimeFinder public static void mainString ar... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Elementary Statistics A Step By Step Approach

Authors: Allan Bluman

8th Edition

73386103, 978-0073386102

More Books

Students also viewed these Programming questions

Question

Give eye contact, but do not stare.

Answered: 1 week ago