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.
In this assignment you will be implementing a function that will find all single-word anagrams given...
In this assignment you will be implementing a function that will find all single-word anagrams given a single word and a list of all words. In the starter code you will the starting source file anagram.cpp and the list of words wordlist.txt. You will need to implement the anagram() function which takes a string for the word to find anagrams of as the first parameter and a vector of strings containing a list of all words as the second parameter....
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT