# Finding prime numbers

Page 1 of 1

## 4 Replies - 42283 Views - Last Post: 19 March 2010 - 08:01 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=18791&amp;s=182170ba6ece383eeff66e2885ffa099&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 dr_worm

Reputation: 2
• 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

Reputation: 2
• 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

### #3 ProGraM

Reputation: 10
• 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;

}
}

```

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.

### #5 Dogstopper

Reputation: 2920
• Posts: 11,194
• 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!