Question

In: Advanced Math

Scenario Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given...

Scenario

Implementing the sieve of Eratosthenes algorithm to find all prime numbers up to a given limit.

Aim

Develop code for implementing the sieve of Eratosthenes.

Steps for Completion

  1. Implement the isPrime() method of the SieveOfEratosthenes class that should return true if the number is prime, and false otherwise.

  2. Consider building the sieve in the class constructor.

CODE GIVEN

public class SieveOfEratosthenes {

public SieveOfEratosthenes(int maxValue) {

// Build the sieve here

}

public boolean isPrime(int value) {

// Write your code here

}

}

Solutions

Expert Solution

ANS) /run the program on local machine main method is not created in this as implementaion of functions is required/

public class SieveOfEratosthenes {

public SieveOfEratosthenes(int maxValue) {

  

// Build the sieve here

  

boolean prime[] = new boolean[maxValue + 1];

for(int i=0;i<maxValue;i++)

prime[i] = true;

for(int p = 2; p*p <=maxValue; p++)

{

// If number is not changed, then it is a prime

if (prime[p] == true) {

// Update all multiples of p

for (int i = p * p; i <= maxValue; i += p)

prime[i] = false;

}

}

for(int i = 2; i <= maxValue; i++)

{

if(prime[i] == true)

System.out.print(i + " ");

}

}

public boolean isPrime(int value) {

// Write your code here

boolean flag = false;

for(int i = 2; i <= value/2; ++i)

{

// condition for nonprime number

if(value % i == 0)

{

flag = true;

break;

}

}

if (flag == false) {

return true;

}

else {

return false;

}

}

}


Related Solutions

The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to...
The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit,” Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n - 1, whether the number is prime. In Java
Here is a (buggy) version of the Eratosthenes sieve: # should return a list of prime...
Here is a (buggy) version of the Eratosthenes sieve: # should return a list of prime numbers from 1..n (including n) def sieve(n): if n < 2: #there are no prime numbers < 2 return [] primes = [2] # 2 is the first prime number for i in range(n): #put all of the numbers into primes initially primes.append(i) for current in range(n): # start at current, and remove all multiples of current up to n for j in range(current,n,current):...
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
IN PYTHON Use the sieve of Eratosthenes to find all primes less than 10,000.
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
Consider this prime sieve in which an array of numbers of size N is created (i.e....
Consider this prime sieve in which an array of numbers of size N is created (i.e. int nums[10] = {2,3,4,5,6,7,8,9,10,11};) and initialized to contain counting numbers starting with 2. The sieve works by taking each number in the array, and “crossing out” (perhaps via a similar array of Booleans) multiples of that number. So beginning with 2, we would “cross out” 4, 6, 8, 10. Then repeat the process for 3, and so on. Numbers that are already “crossed out”...
Here is the algorithm: 1. Assume that all numbers are prime from 2..n, so put them...
Here is the algorithm: 1. Assume that all numbers are prime from 2..n, so put them in the list (2 is the smallest number, so if n is less than 2, the list should be empty). 2. Make the first item be current. 3. Leave in the current item. Then, take out all of its multiples (since any multiple will have that number as a factor). 4. Make current be the next element in primes. 5. If there are no...
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
Find the biggest “*prime gap” (see definition) from the prime numbers between 1 and 1,000,000. *Prime...
Find the biggest “*prime gap” (see definition) from the prime numbers between 1 and 1,000,000. *Prime gap = the difference between two consecutive primes. The exercise can be completed manually or with a computer program. Whichever seems easiest.
Given below is a fictitious Excel output (numbers might be all made up) Answer the questions...
Given below is a fictitious Excel output (numbers might be all made up) Answer the questions that follow. You are being tested for your ability to read the output and your skill in sensitivity analysis as taught in class.) Variable Cells Final Reduced Objective Allowable Allowable Cell Name Value Cost Coefficient Increase Decrease $B$6 X1 45 0 12 10 8 $C$6 X2 0 -6 8 15 12 $D$6 X3 10 0 5 20 15 $E$6 X4 35 0 6 8...
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT