Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need help correcting my code for calculating sexy prime pairs

1. ## Need help correcting my code for calculating sexy prime pairs

So I have been given a problem in class stating that I had to calculate the sexy prime pairs. I have to use the sieve of eratosthenes alogorithm and a array to calculate the prime numbers and then use that same array to display the sexy prime pairs in between boundaries that are specified by the user. Much help will be appreciated thanks.

This is the sample run.

Please enter a lower boundary and an upper boundary and I will print all of the
sexy prime pairs between those boundaries.
Please enter the lower boundary (between 1 and 50000): 0
Please enter the lower boundary (between 1 and 50000): 50001
Please enter the lower boundary (between 1 and 50000): 150
Please enter the upper boundary (between 1 and 50000): 0
Please enter the upper boundary (between 1 and 50000): 50001
Please enter the upper boundary (between 1 and 50000): 100
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.

2. ## Re: Need help correcting my code for calculating sexy prime pairs

Any chance you could explain what your problem is and ask a specific question?

3. ## Re: Need help correcting my code for calculating sexy prime pairs

```for (int e = 2; e <= 223; e++)
{
if (primes[e])
{
for (int f = e; f <= 223; f++)
{
int g = (e * f);
primes[g] = true;
}

}

}```

And
```                    int g = (e * f);
primes[g] = true;```

What do you expecting it to do?
`int g = e*f;`
Let's say e=5 and f = 10, so?

I have looked on your code. A real immature approach. May be with some fixes it will lead you to the solution but in the end, not efficient enough.

4. ## Re: Need help correcting my code for calculating sexy prime pairs

My major problem is calculating the prime numbers using the sieve of eratosthenes algorithm. How do I Loop through the factors and mark them like and unmark them if they are prime?

5. ## Re: Need help correcting my code for calculating sexy prime pairs

My problems are the prime number algorithm and my sexy prime pairs im a little unsure as to how to calculate them and I understand my attempts to do so have not been very good.

6. ## Re: Need help correcting my code for calculating sexy prime pairs

I will recommend you to take a pencil and a paper and draw, how will you do it manually.
Write all the steps and then code them.