Sexy Primes

using methods and arrays

Page 1 of 1

8 Replies - 3349 Views - Last Post: 02 December 2009 - 08:33 PM Rate Topic: -----

#1 bigskers76   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-November 09

Sexy Primes

Post icon  Posted 30 November 2009 - 09:31 PM

I am having problems finding what is called a sexy pair. It is suppose to be in the last method and print out all of the sexy prime pairs that fall between (and including) the lower and upper boundaries the user has selected. Do not print any pairs that contain numbers that are out of bounds. Print each pair on a separate line the smaller number of the pair first. After printing the prime numbers, print out a count of the number of
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" );
						}
				}
		}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Sexy Primes

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Sexy Primes

Posted 30 November 2009 - 09:46 PM

boolean prime[] is false or true if it is a prime ?

			   for( int x = 2; x < primes.length; x++ )
				{
						primes[x] = true;
				}


so shouldn't be prime[x] false ?

all your other loops starting with 2 are wrong do:
for(int i = 3; i < whatever; i += 2)

I wrote a Code Snippet about using a file as a sieve array if you want to play with really big prime numbers

And what are sexy pairs ?
Was This Post Helpful? 0
  • +
  • -

#3 bigskers76   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-November 09

Re: Sexy Primes

Posted 01 December 2009 - 11:01 AM

sexy primes are a pair (p, p + 6) of prime numbers that differ by six.

our prime numbers are 1 to 50,000

This post has been edited by bigskers76: 01 December 2009 - 11:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Sexy Primes

Posted 01 December 2009 - 01:21 PM

I would store your primes in an ArrayList or LinkedList, that way you can use the contains() method. From there, it should be a simple matter of a for loop.
Was This Post Helpful? 0
  • +
  • -

#5 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Sexy Primes

Posted 01 December 2009 - 05:16 PM

Sexy Primes!
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Sexy Primes

Posted 01 December 2009 - 05:27 PM

A lot easier just to use a BitSet (or an array of boolean) and test one a new prime is found if -6 is also a prime

import java.util.BitSet;

public class SexyPair {

	static final int MAX = 100000;
	public static void main(String[] args) {
		
		// create a BitSet for the primes
		BitSet bs = new BitSet(MAX);
		// set 2 as prime and as potential primes all odd numbers
		bs.set(2);
		for(int i = 1; i <= MAX; i += 2)
			bs.set(i);
		// start with 3 do determine prime
		for(int i = 3; i < MAX; i += 2) {
			// test if prime
			if(bs.get(i)) {
				// test if 6 before is also prime if we are at 7 or higher
				if(i >= 7) {
					if(bs.get(i-6))
						System.out.println("SexyPair: " + (i-6) + " - " + i);
				}
				// clear the multiples of this new found prime
				for(long j = i; j < MAX; j += 2) {
					long num = i * j;
					// break if we exceed the MAX prime to check
					if(num > MAX)
						break;
					bs.clear((int) num);
				}
			}
		}
		
	}
}



*Edited to make the multiplication into a long 100000 * 100000 might exceed

This post has been edited by pbl: 01 December 2009 - 08:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 bigskers76   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-November 09

Re: Sexy Primes

Posted 01 December 2009 - 08:26 PM

thank you all so much! i'll get to work on this thank you soo much :)
Was This Post Helpful? 0
  • +
  • -

#8 bigskers76   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 23-November 09

Re: Sexy Primes

Posted 02 December 2009 - 06:29 PM

okay so i've been working on this project and changed somethings...but now I am getting a bunch of values to just print out from 1 to 50000 instead of just the section I entered.

before I changed my validation in get boundaries I got the problem suggested above, but since I changed it this way i get these type of messages:

illegal start of expression
public void showPrimes()
^
Sieve.java:86: ')' expected
public void showPrimes()
^
Sieve.java:86: ';' expected
public void showPrimes()
^
Sieve.java:86: illegal start of expression
public void showPrimes()
^
Sieve.java:86: ';' expected
public void showPrimes()

here's my new code

public Sieve()
		{
				primes[0] = false;
				primes[1] = false;
				upper = 50000;
				lower = 1;

				for( int x = 2; x < primes.length; x++ )
				{
						primes[x] = true;
				}

				processSieve();
				showPrimes();
				getBoundaries();
				showPrimes();
		}

		public void processSieve()
		{
				for( int i = 2; i <= Math.sqrt(50000); i++ )
				{
 for( int a = ( i * i); a <= upper; a += i)
						{
								primes[i] = false;
						}
				}
		}

		public void getBoundaries()
		{
				Scanner input = new Scanner ( System.in );

				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" );
				do
				{
				System.out.println( "Please enter the lower boundary (between 1 and 50000) : " );
				lower = input.nextInt();
				if(( 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();

				if(( upper <= 1 ) || ( upper >= 50000 ))
				{
						System.out.println( "Please enter the upper boundary (between 1 and 50000) : " );
						upper = input.nextInt();
				}while ( upper <= lower );
		}

		public void showPrimes()
		{
				int counter = 0;
				System.out.printf( "Here are all of the sexy prime pairs in the range 8997 to 9200, one pair per line\n", lower, upper );
				{
for( int i = lower; i < upper; i++ )
				{
						if( primes[i] == true )
						{
								if( ((i+6) < 50001) && (primes [i+6] == true ))
								{
										System.out.printf ( "%d and %d\n", i, i+6);
										counter++;
								}
						}
				}

				System.out.printf( "There were %d sexy pairs displayed between %d and %d\n", counter, lower, upper);
				}
		}
}


Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Sexy Primes

Posted 02 December 2009 - 08:33 PM

Indent your code properly an the errors will be obvious
How many times will we have to repeat that ? :angry:
If you don't do it for you do it for the others (like us) that have to read back your code... especially when it is to find errors that this indentation will reveal obvious :angry: :angry: :angry:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1