Finding prime numbers

I'm so close to done I can taste it.

Page 1 of 1

4 Replies - 41438 Views - Last Post: 19 March 2010 - 08:01 AM Rate Topic: -----

#1 dr_worm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 144
  • Joined: 11-September 06

Finding prime numbers

Posted 20 September 2006 - 12:23 PM

I feel like I'm this close <----> to being done. I just need a hint.

public class primeTwin {
	public static void main(String[] args) {
		//Declare variables.
		int n;					//Iteration count.
		int i = 2;				//Number for prime check.
		boolean a = false;		//Prime check return.
		boolean b = false;		//Prime twin check return.
		//Begin for loop to count iterations.
		for (n = 0; n < 1000; n++) {
			//While loop to find a prime twin.
			while (!a && !b) {
				//Call isPrime method to check for primes.
				a = isPrime(i);
				if (a) {
					//Call isPrime method to check for prime twin for i.
					b = isPrime(i + 2);
					if (b) {
						System.out.printf("%d: %d  %d\n", n + 1, i, i + 2);
					}
				}
				i++;
			}
		}
	}
	public static boolean isPrime(int i) {
		/** isPrime method */
		//Declare variables.
		int c;
		//For loop for prime test.
		for (c = 2; c < i; c++) {
			if (i % c == 0) {
				return false;
			}	
		}
		return true;
	}
}


BTW, in case you don't want to deduce what I'm trying to do from the code, I'm trying to find prime twins, prime numbers within 2 of each other (3 and 5, 5 and 7).

Is This A Good Question/Topic? 1

Replies To: Finding prime numbers

#2 dr_worm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 144
  • Joined: 11-September 06

Re: Finding prime numbers

Posted 20 September 2006 - 12:42 PM

Got it. I reinitialized the boolean variables after a successful pass so the while loop would terminate after finding a prime twin:
for (n = 0; n < 1000; n++) {
			//While loop to find a prime twin.
			while (!a || !b) {
				//Call isPrime method to check for primes.
				a = isPrime(i);
				if (a) {
					//Call isPrime method to check for prime twin for i.
					b = isPrime(i + 2);
					if (b) {
						System.out.printf("%d: %d  %d\n", n + 1, i, i + 2);
					}
				}
				i++;
			}
			a = false;  //New code
			b = false;  //New code
		}

Silly me
Was This Post Helpful? 0
  • +
  • -

#3 ProGraM  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 150
  • Joined: 28-September 05

Re: Finding prime numbers

Posted 20 September 2006 - 07:12 PM

ok i fixed your program. now when u run your program your program will run in a loop and keep outputting numbers it is your job to find where your loop is wrong if you need help i can respond later but heres your new working code.

public class primeTwin 
{
	public static void main(String[] args) 
	{
	   
		int n;					
		int i = 2;				
		boolean a = false;		
		boolean b = false;		
		
		for (n = 0; n < 1000; n++) {
			
		while (!a || !b) 
		{
				
				a = Prime(i);
		   if (a) 
		   {
	 				b = Prime(i + 2);
					if (b) 
					{
						System.out.printf("%d: %d  %d\n", n + 1, i, i + 2);
					}
			}
				i++;
		}
			a = false;  
			b = false; 
	  }
}
public static boolean Prime(int i) 
{
		int c;
		
		for (c = 2; c < i; c++) 
		{
			if (i % c == 0) 
			{
				return false;
			}	
		}
		return true;
		
}
}


Was This Post Helpful? 0
  • +
  • -

#4 Guest_amitut*


Reputation:

Re: Finding prime numbers

Posted 19 March 2010 - 03:35 AM

if you want to be more efficient- try this:
public static boolean isPrime(int a){
		if(a%2==0)return false;
		double c=Math.sqrt(a);
		for(int i=3;i<c;i+=2){
			if(a%i==0)return false;
		}
		return true;
}

you cut the program from doing a times to sqrt(a)/2 times. when you reach big numbers, it'll become very noticeable.
Was This Post Helpful? 1

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Finding prime numbers

Posted 19 March 2010 - 08:01 AM

Hey! Thanks amitut! However, this post is 4 years old and I really doubt the OP is going to come back!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1