JAVA Program (using basic for-loops, arrays...): For this program, you are first
ID: 650377 • Letter: J
Question
JAVA Program (using basic for-loops, arrays...):
For this program, you are first going to calculate the prime numbers between 1 and 50000 using an algorithm known as the Sieve of Eratosthenes and then display the sexy prime pairs within a range specified by the user.
In addition to the main() method (which is given), write 4 supporting methods (see below) to complete the program.
public class_Sieve {
public static void main ( String args[] ) {
final int HOWMANY = 50000; // The range of numbers
boolean[] sieve = new boolean[HOWMANY+1]; // Boolean array of true/false
int lower = 1, upper = HOWMANY; // Setting initial boundaries
processSieve( sieve );
showPrimes( sieve, lower, upper );
lower = getLower( HOWMANY );
upper = getUpper( lower, HOWMANY );
showPrimes( sieve, lower, upper );
}
// implement method processSieve here
// implement method showPrimes here
// implement method getLower here
// implement method getUpper here
}
1st Method: processSieve()
Here is an overview of the algorithm for implementing the Sieve of Eratosthenes in method processSieve():
1. Work through the entire array, from beginning to end, and set all elements to true.
2. Then set elements 0 and 1 to false, since those positions are not prime numbers.
3. Set up a loop starting at position 2 through the end of the array, incrementing by 1 each time.
a. Set up an inner loop that starts at twice the value of the outer loop
Explanation / Answer
import java.util.Arrays;
import java.util.Scanner;
public class Sieve {
public static void main ( String args[] )
{
final int HOWMANY = 50000; // The range of numbers
boolean[] sieve = new boolean[HOWMANY+1]; // Boolean array of true/false
int lower = 1, upper = HOWMANY; // Setting initial boundaries
processSieve(sieve);
showPrimes( sieve, lower, upper );
lower = getLower( HOWMANY );
upper = getUpper( lower, HOWMANY );
showPrimes( sieve, lower, upper );
}
public static void processSieve(boolean[] sieve)
{
int n=sieve.length;
for(int i=0;i<n;i++)
{
sieve[i]=true;
}
sieve[1]=sieve[0]=false;
for(int i=2;i<n;i++)
{
if(sieve[i]==true)
{
for(int j=2*i;j<n;j+=i)
{
sieve[j]=false;
}
}
}
}
// implement method processSieve here
// implement method showPrimes here
public static void showPrimes(boolean[] sieve,int lowerBound,int upperBound)
{
System.out.println("All sexy pairs between "+Integer.toString(lowerBound)+" and "+Integer.toString(upperBound)+" are :");
int counter=0;
for(int i=lowerBound;i<=upperBound-6;i++)
{
if(sieve[i]==true && sieve[i+6]==true)
{
counter+=1;
System.out.println(Integer.toString(i)+" "+Integer.toString(i+6));
}
}
System.out.println("Total number of sexy prime pairs are: "+Integer.toString(counter));
}
// implement method getLower here
public static int getLower(int upperBound)
{
Scanner in = new Scanner(System.in);
while(true)
{
System.out.println("Please enter a lower bound between 1 and "+Integer.toString(upperBound));
int a = in.nextInt();
if(a>=1 && a<=upperBound)
return a;
System.out.println("The number you entered is out of range ");
}
}
// implement method getUpper here
public static int getUpper(int lowerBound,int upperBound)
{
Scanner in = new Scanner(System.in);
while(true)
{
System.out.println("Please enter a upper boundary between "+Integer.toString(lowerBound)+" and "+Integer.toString(upperBound));
int a = in.nextInt();
if(a>=lowerBound && a<=upperBound)
return a;
System.out.println("The number you entered is out of range ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.