sexy prime pairs that were displayed between the lower and upper boundaries on a separate line.
I have a driver program separate that looks like this:
public class SieveTest
{
public static void main ( String args[] )
{
Sieve foo = new Sieve();
foo.processSieve(); // run the Sieve algorithm
foo.showPrimes(); // shows entire set of sexy
//pairs, 1419 of them
foo.getBoundaries(); // get lower and upper //
//boundaries
foo.showPrimes(); // now shows sexy pairs
//between //lower and upper
}
}
For example: Please enter the lower boundary (between 1 and 50000): 8997
Please enter the upper boundary (between 1 and 50000): 9200
Here are all of the sexy prime pairs in the range 8997 to 9200, one
pair per line:
9001 and 9007
9007 and 9013
9043 and 9049
9103 and 9109
9127 and 9133
9151 and 9157
9181 and 9187
There were 7 sexy prime pairs displayed between 8997 and 9200
public class Sieve
{
private boolean primes[] = new boolean [5001];
private int upper;
private int lower;
Scanner input = new Scanner ( System.in );
public Sieve()
{
primes[0] = false;
primes[1] = false;
upper = 50000;
lower = 1;
for( int x = 2; x < primes.length; x++ )
{
primes[x] = true;
}
getBoundaries();
processSieve();
showPrimes();
}
public void processSieve()
{
for( int a = 2; ( a * a ) <= upper; a++ )
{
for( int b = ( a * a ); b <= upper; b += a )
{
primes[b] = false;
}
}
}
public void getBoundaries()
{
System.out.print( "Please enter a lower boundary and an upper boundary and I will print" );
System.out.print( "all of the sexy prime pairs between those boundaries.\n" );
System.out.println( "Please enter the lower boundary (between 1 and 50000) : " );
lower = input.nextInt();
while(( lower <= 1 ) || ( lower >= 50000))
{
System.out.println( "Please enter a lower boundary (between 1 and 50000) : " );
lower = input.nextInt();
}
System.out.println( "Please enter the upper boundary (between 1 and 50000) : " );
upper = input.nextInt();
while(( upper <= 1 ) || ( upper >= 50000 ))
{
System.out.println( "Please enter the upper boundary (between 1 and 50000) : " );
upper = input.nextInt();
}
while( lower <= upper )
{
while(( lower <= 1 ) || ( lower >= 50000 ))
{
System.out.println( "Please enter the lower boundary (between 1 and 50000) : " );
lower = input.nextInt();
}
while(( upper <= 1 ) || ( upper >= 50000 ))
{
System.out.println( "Please enter the upper boundary (between 1 and 50000) : " );
upper = input.nextInt();
}
}
}
public void showPrimes()
{
for( int i = 2; i < upper; i++ )
{
if ( primes[c] == true )
{
System.out.println( "Here are all of the sexy prime pairs in the range 8997 to 9200, one" );
System.out.println( "pair per line: " );
System.out.println( c+ "and" +c+ "\n" );
}
}
}
}

New Topic/Question
Reply



MultiQuote






|