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

OVERVIEW OF PRIME NUMBERS In mathematics, a prime number (or a prime) is a natur

ID: 3568050 • Letter: O

Question

OVERVIEW OF PRIME NUMBERS
In mathematics, a prime number (or a prime) is a natural number greater than one whose only positive divisors are one and itself.
A natural number that is greater than one and is not a prime is called a composite number. The numbers zero and one are neither
prime nor composite. The property of being a prime is called primality. Since 2 is the only even prime number, the term odd prime
refers to all prime numbers greater than 2. -- Definition taken from http://en.wikipedia.org/wiki/Prime_number.
In mathematics, a sexy prime is a pair (p, p + 6) of prime numbers that differ by six. The name "sexy prime" stems from the Latin
word for six: sex. -- Definition taken from http://en.wikipedia.org/wiki/Sexy_prime.
Some examples of sexy prime pairs are 5 and 11, 7 and 13, and 461 and 467.
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.
TEMPLATE TO START YOUR PROGRAM: NetID_Sieve.java
To get you started for this assignment, you will be given a main() method. You will need to type this in as you see it here, without
modifications other than to put in your on NetID and leave out the comments if you don

Explanation / Answer

Kidndly IGNORE my previous solution (I have made changes in showPrimes Function)
import java.io.*;
import java.util.*;
public class NetID_Sieve
{
    static final int HOWMANY = 50000;
    public static void main (String args[])throws Exception
    {
      
        boolean sieve[] = new boolean[HOWMANY+1];
        int lower = 1, upper = HOWMANY;
        sieve[0]=false;// Since '0'(zero) is not Prime,
        sieve[1]=false;// Since '1'(one) is not Prime,
      
       
// Intialise All array elements to 'true' initially
        for (int i=2;i<=HOWMANY;i++)
            sieve[i]=true;
     
   // the following method call will implement the Sieve algorithm
        processSieve(sieve);
      
// the following method call will print all 1419 sexy pairs of primes
        showPrimes( sieve, 1, HOWMANY );
      
     
   // the following method call gets the lower boundary for printing
        lower = getLower(HOWMANY);
      
    
    // the following method call gets the upper boundary for printing
        upper = getUpper(lower,HOWMANY);
      
      
// the following method call prints sexy pairs in the new lower-upper range
        showPrimes( sieve, lower, upper );
      
    }
    public static void processSieve(boolean seive[])
    {
        for(int i=2;i<=Math.sqrt(HOWMANY);i++)
        {
            if(seive[i]==true)
            {
                 for(int j= i*i;j<=HOWMANY;j+=i)
                {
                    seive[j]=false;
                }              
            }
        }
    }//processSieve
    public static void showPrimes(boolean seive[],int lower,int upper)
    {
        for(int i=lower;i<=(upper-6);i++)
        {
            if(seive[i]==true&&seive[i+6]==true)
            {
                System.out.print("Sexy Prime No: "+i+" ");
                System.out.println("Sexy Prime No: "+(i+6));
            }
        }// for loop
    }
    public static int getUpper(int lower,int HOWMANY)throws Exception
    {
        int upper;
        Scanner input = new Scanner(System.in);
        do
        {
            System.out.print ("Please enter the upper boundary (between "+lower+" and 50000): " );
            upper = input.nextInt ();
        }while ( upper < lower || upper > HOWMANY );
        return upper;
    }
    public static int getLower(int HOWMANY)throws Exception
    {
        int lower;
        Scanner input = new Scanner(System.in);
        do
        {
            System.out.print ("Please enter the lower boundary (between 1 and 50000): " );
            lower = input.nextInt ();
        }while ( lower < 1 || lower > HOWMANY );
        return lower;
    }
}