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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

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

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Your upper boundary cannot be smaller than your lower boundary
    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.
    Last edited by BinaryPenguin14; December 7th, 2011 at 09:54 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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?
    Improving the world one idiot at a time!

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default 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.

Similar Threads

  1. Need help correcting a JFrame program for class!
    By AlexAndAHalf in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 23rd, 2011, 04:03 AM
  2. Matching Pairs game - something's not right?
    By sambar89 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 02:54 PM
  3. Calculating Percentile
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 7th, 2011, 08:26 PM
  4. Java error correcting, testing for a square issue i think
    By djl1990 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2011, 03:17 AM
  5. Need Help - Factoring & Prime Finding Code
    By prodigytoast in forum Algorithms & Recursion
    Replies: 5
    Last Post: November 5th, 2009, 07:38 AM