Question

In: Computer Science

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

Solutions

Expert Solution

Here is the Java code to the given question.

The given method is provided along with a main() to test the method.

Two sample outputs are added at the end.

Code:

import java.util.Scanner;

public class SieveOfEratosthenes {


    /*method to find prime numbers between 0 and n*/
    public static boolean[] sieve(int n){

        boolean[] booleanArray=new boolean[n];    /*creates a new boolean array*/

        for(int i=0;i<n;i++){         /*makes all elements of array as 'true'*/

            booleanArray[i]=true;

        }

        for(int i=2;i*i<n;i++){

            if(booleanArray[i]==true){    /*if the element is not marked false*/

                for(int j=i*i;j<n;j+=i){      /*marks all multiples of i as false*/

                    booleanArray[j]=false;

                }
            }
        }

        return booleanArray;    /*returns the boolean array*/
    }

    
    public static void main(String[] args){

        System.out.println("Enter a number: ");    /*asks user to enter a number*/

        Scanner scanner=new Scanner(System.in);    /*creates a new Scanner object*/

        int number=scanner.nextInt();    /*takes number from user*/

        System.out.println("The prime numbers from 0 to "+(number-1)+" are: ");

        boolean[] booleanArray=sieve(number);    /*calls sieve method that returns boolean array*/

        for(int i=2;i<number;i++){   /*runs loop from 2 to number-1*/

            if(booleanArray[i]==true){     /*if the number is prime*/
                System.out.print(i+" ");     /*print the number*/
            }
        }
    }
}

Sample Output-1:

Sample Output-2:


Related Solutions

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 Implement the isPrime() method of the SieveOfEratosthenes class that should return true if the number is prime, and false otherwise. 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...
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):...
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.
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.
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...
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:...
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
Write a program that prints the count of all prime numbers between A and B (inclusive),...
Write a program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = The 5 digit unique number you had picked at the beginning of the semester B = A + 5000 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,...
Consider the statement, “For all natural numbers n,n, if nn is prime, then nn is solitary.”...
Consider the statement, “For all natural numbers n,n, if nn is prime, then nn is solitary.” You do not need to know what solitary means for this problem, just that it is a property that some numbers have and others do not. Write the converse and the contrapositive of the statement, saying which is which. Note: the original statement claims that an implication is true for all n,n, and it is that implication that we are taking the converse and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT