Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

CIT239 Java Programming Lab Exercise 7a Prime Numbers Due Date You must demonstrate the solution to this lab exercise to the instructor by Sunday, February

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

CIT239 Java Programming Lab Exercise 7a Prime Numbers Due Date You must demonstrate the solution to this lab exercise to the instructor by Sunday, February 26, 2017, in order to receive credit for this work. Prime Numbers Lab Exercise enhancing a (partially) numbers and keeps a count of how many divisors were tested in order to determine whether each number is really a prime. The assignment is to implement another, slightly different, algorithm for finding prime numbers, and keep a count of how many divisors are tested in order to determine whether or not each number is a prime. The starter code also includes a method named "outputResults" which will display the results of both algorithms side-by-side on the screen inefficient Prime Number Generator: Sequential Divisors The method named primesSequentialDivisors determines whether a number n is prime by checking whether 2, 3, 4, 5, 6, n/2 is a divisor with no remainder. If such a divisor is found, then n is NOT a prime. (That is, if the integer division n divisor has remainder 0, then n is NOT a prime number.) This code stores each prime number along with the number of divisor values which were checked in order to determine that the number is a prime. These are stored in a two-dimensional array for later display. The divisorCount variable is really a count of how many times the if number divisor 0) instruction was executed in order to determine that the number is indeed a prime number A More Efficient Prime Number Generator: Prime Divisors A more efficient approach is to check whether any of the prime numbers less than orequal to the square root of n can divide n evenly (with remainder 0). If not, then n is a prime number. To understand this, consider a few examples There is no point in testing for divisibility by 9 when the program has already checked for divisibility by 3. There is no point in testing for divisibility by 15 when the program has already checked for divisibility by 5 There are actually two advantages which this algorithm has over the first: The new algorithm is using only proven prime numbers as the test divisors, rather than all numbers 2 as test divisors. This approach avoids unnecessary passes through the loop The new algorithm takes advantage of the fact that we do not need to test divisors up to half of the candidate prime number; instead, testing divisors up to the square root of the candidate prime number is enough. CIT239-SU Lab07 a PrimeNumbers 20170212.docx 2/11/2017 3:18 PM

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Students also viewed these Databases questions

Question

Describe how to train managers to coach employees. page 404

Answered: 1 week ago

Question

Discuss the steps in the development planning process. page 381

Answered: 1 week ago