Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CIT239 Java Programming Lab Exercise 7a Prime Numbers Due Date You must demonstr

ID: 3809286 • Letter: C

Question

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

Explanation / Answer

As you have not provided any code I have witten for both sequential division and primes. (As I didn't knew structure of code you are looking for I have put everything in main for sequential understanding).

File: Prime.java

public class Primes {
public static void main(String args[]) {
  
int num = 229;
int [] primes = new int[num];
int primeCount = 0;
  
System.out.println("Prime SeqDiv Prime PrimeDiv");
for (int i = 1; i < num; i++)
{
int n = i+1;
boolean isPrime = true;
  
int normalDivCount = 0;

for(int j = 2; j <= n/2; j++)
{
if ((n % j) != 0)
{
normalDivCount++;
}
else
{
isPrime = false;
break;
}
}
if (isPrime)
{
System.out.print(n + " " + normalDivCount + " ");
}
  
int primeDivCount = 0;

isPrime = true;
for(int j = 0; (j < primeCount && primes[j] <= Math.sqrt(n)); j++)
{
if ((n % primes[j]) != 0)
{
primeDivCount++;
}
else
{
isPrime = false;
break;
}
}
if (isPrime)
{
primes[primeCount++] = n;
System.out.println(n + " " + primeDivCount);
}
  
}
}
}

Sample run

Prime   SeqDiv   Prime   PrimeDiv
2   0   2   0
3   0   3   0
5   1   5   1
7   2   7   1
11   4   11   2
13   5   13   2
17   7   17   2
19   8   19   2
23   10   23   2
29   13   29   3
31   14   31   3
37   17   37   3
41   19   41   3
43   20   43   3
47   22   47   3
53   25   53   4
59   28   59   4
61   29   61   4
67   32   67   4
71   34   71   4
73   35   73   4
79   38   79   4
83   40   83   4
89   43   89   4
97   47   97   4
101   49   101   4
103   50   103   4
107   52   107   4
109   53   109   4
113   55   113   4
127   62   127   5
131   64   131   5
137   67   137   5
139   68   139   5
149   73   149   5
151   74   151   5
157   77   157   5
163   80   163   5
167   82   167   5
173   85   173   6
179   88   179   6
181   89   181   6
191   94   191   6
193   95   193   6
197   97   197   6
199   98   199   6
211   104   211   6
223   110   223   6
227   112   227   6
229   113   229   6