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 13 of 13

Thread: I dont get it....please help

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I dont get it....please help

    I really dont understand why:

    First, my program is loading every number possible before prompting my user for a number between boundaries.

    Second, why my program keeps listing off way more numbers then needed.

    PLEASE HELP! IM SO CONFUSED! Here is my code:

    import java.util.Scanner;
     
    public class user_Program
    {
        private boolean primes[] = new boolean [50001];
     
        private int upper = 50000;
     
        private int lower = 1;
     
        public void processSieve()
        {
            primes[0] = false;
            primes[1] = false;
            for (int a = 2; a < 50000; a++)
            {
                primes[a] = true;
            }
     
            for (int a = 2; a <= 223; a++)
            {
                if (primes[a])
                {
                                  for (int b = a; b <= 223; b++)
                    {
                        int c = (a * b);
                        primes[c] = false;
                    }
                }
            }
        }
     
        public void getBoundaries()
        {
            System.out.println ("Please enter a lower boundary and an upper boundary and I will print sexy prime pairs between those boundaries.");
     
            Scanner i = new Scanner (System.in);
     
            System.out.print("Please enter the lower boundary (between 1 and 50000): ");
            lower = i.nextInt();
            while ((lower <= 0) || (lower > 50000))
            {
                System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                                                  lower = i.nextInt();
            }
     
            System.out.print("Please enter the upper boundary (between 1 and 50000): ");
            upper = i.nextInt();
            while ((upper <= 0) || (upper > 50000))
            {
                System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                upper = i.nextInt();
            }
     
            while (lower > upper)
            {
                System.out.println ("Your upper boundary cannot be smaller than your lower boundary.");
     
                System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                lower = i.nextInt();
                while ((lower <= 0) || (lower > 50000))
                {
                                     System.out.print("Please enter the lower boundary (between 1 and 50000): ");
                    lower = i.nextInt();
                }
     
                System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                upper = i.nextInt();
                while ((upper <= 0) || (upper > 50000))
                {
                    System.out.print("Please enter the upper boundary (between 1 and 50000): ");
                    upper = i.nextInt();
                }
     
            }
     
        }
     
        public void showPrimes()
        {
            System.out.printf("Here are all of the sexy prime pairs in the range %d to %d, one pair per line:\n", lower, upper);
     
            int counter = 0;
            for (int a = lower; a <= upper; a++)
            {
        if (primes[a] == true)
                {
                    int b = (a + 6);
                    if (b <= 50000)
                    {
                        if (primes[b] == true)
                        {
                            System.out.printf("%d and %d\n", a, b);
                            counter++;
                        }
                    }
                }
            }
     
            System.out.printf("There were %d sexy prime pairs displayed between %d and %d\n", counter, lower, upper);
        }
    }


  2. #2
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: I dont get it....please help

    Could you give us more detail say give a sample of what you expect to happen for example what you expect the output to be if you set the boundries to 2 and 50 and what output you actually get. Also you might want to explain what prime pairs are I know but alot of people wouldn't(I know google is there but they are taking their time to help you already)

  3. #3
    Junior Member
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I dont get it....please help

    A sample of what i am suppose to get:

    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


    Defintion of Prime Pairs (from Wikipedia:

    In mathematics, a prime number (or a prime) is a natural number greater than one whose only positive divisors are one and itself. A natural number that is greater than one and is not a prime is called a composite number. The numbers zero and one are neither prime nor composite. The property of being a prime is called primality. Since 2 is the only even prime number, the term odd prime refers to all prime numbers greater than 2


    In mathematics, a sexy prime is a pair (p, p + 6) of prime numbers that differ by six. The name "sexy prime" stems from the Latin word for six: sex

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I dont get it....please help

    Now could you show us what you do get?

  5. #5
    Junior Member
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I dont get it....please help

    first before it prompts the user for a lower number it goes through all the primes between 1 and 50000

    This is what it reads:


    There were 40731 sexy prime pairs displayed between 1 and 50000
    Please enter a lower boundary and an upper boundary and I will print sexy prime pairs between those boundaries.
    Please enter the lower boundary (between 1 and 50000): 89978997
    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:
    8997 and 9003
    8998 and 9004
    8999 and 9005
    9001 and 9007
    9002 and 9008
    9003 and 9009
    9005 and 9011
    9007 and 9013
    9008 and 9014
    9009 and 9015
    9012 and 9018
    9013 and 9019
    9015 and 9021
    9016 and 9022
    9019 and 9025
    9023 and 9029
    9025 and 9031
    9026 and 9032
    9029 and 9035
    9031 and 9037
    9032 and 9038
    9033 and 9039
    9034 and 9040
    9035 and 9041
    9036 and 9042
    9037 and 9043
    9038 and 9044
    9040 and 9046
    9042 and 9048
    9043 and 9049
    9044 and 9050
    9048 and 9054
    9049 and 9055
    9050 and 9056
    9051 and 9057
    9053 and 9059
    9054 and 9060
    9056 and 9062
    9058 and 9064
    9059 and 9065
    9060 and 9066
    9062 and 9068
    9064 and 9070
    9066 and 9072
    9068 and 9074
    9069 and 9075
    9070 and 9076
    9074 and 9080
    9075 and 9081
    9076 and 9082
    9077 and 9083
    9081 and 9087
    9084 and 9090
    9087 and 9093
    9090 and 9096
    9091 and 9097
    9092 and 9098
    9093 and 9099
    9094 and 9100
    9095 and 9101
    9097 and 9103
    9098 and 9104
    9099 and 9105
    9100 and 9106
    9101 and 9107
    9103 and 9109
    9104 and 9110
    9105 and 9111
    9107 and 9113
    9108 and 9114
    9109 and 9115
    9111 and 9117
    9113 and 9119
    9114 and 9120
    9115 and 9121
    9117 and 9123
    9120 and 9126
    9121 and 9127
    9122 and 9128
    9123 and 9129
    9126 and 9132
    9127 and 9133
    9128 and 9134
    9129 and 9135
    9131 and 9137
    9132 and 9138
    9133 and 9139
    9134 and 9140
    9135 and 9141
    9136 and 9142
    9138 and 9144
    9140 and 9146
    9141 and 9147
    9142 and 9148
    9146 and 9152
    9147 and 9153
    9148 and 9154
    9149 and 9155
    9151 and 9157
    9152 and 9158
    9154 and 9160
    9155 and 9161
    9156 and 9162
    9157 and 9163
    9160 and 9166
    9162 and 9168
    9166 and 9172
    9168 and 9174
    9170 and 9176
    9171 and 9177
    9172 and 9178
    9174 and 9180
    9175 and 9181
    9176 and 9182
    9177 and 9183
    9178 and 9184
    9180 and 9186
    9181 and 9187
    9182 and 9188
    9183 and 9189
    9184 and 9190
    9185 and 9191
    9186 and 9192
    9187 and 9193
    9188 and 9194
    9189 and 9195
    9190 and 9196
    9191 and 9197
    9193 and 9199
    9194 and 9200
    9195 and 9201
    9197 and 9203
    9199 and 9205
    9200 and 9206
    There were 134 sexy prime pairs displayed between 8997 and 9200

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,559
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: I dont get it....please help

    That would indicate that your tests for the "sexy prime pairs" needs work.

    It looks like all the numbers in that range pass the test.
    What is the test you use to see if a number is prime?

    Why is the value 223 in the following statement?
    for (int a = 2; a <= 223; a++)

  7. #7
    Junior Member
    Join Date
    Dec 2008
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I dont get it....please help

    Test:

    public class user_ProgramTest
    {
        public static void main ( String args[] )
        {
            user_Program foo = new user_Program();
            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
        }
    }
    Last edited by Json; June 29th, 2010 at 04:14 AM.

  8. #8
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: I dont get it....please help

    for (int a = 2; a <= 223; a++)
            {
                if (primes[a])
                {
                                  for (int b = a; b <= 223; b++)
                    {
                        [B][I]int c = (a * b);[/I][/B]
                        primes[c] = false;
                    }
                }
            }

    Notice how youre declaring the variables type every time you set it. Try to do that outside this codeblock (int c and then just do c= (a*b);.

    EDIT:

    Found the same mistak further down too:

     if (primes[a] == true)
                {
                   [B][I] int b = (a + 6);[/I][/B]
                    if (b <= 50000)
                    {
                        if (primes[b] == true)
                        {
                            System.out.printf("%d and %d\n", a, b);
                            counter++;
                        }
                    }
                }

    This should be where your code goes wrong, seeing as you have an array full of trues.

  9. #9
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: I dont get it....please help

    Quote Originally Posted by Charlie View Post
    for (int a = 2; a <= 223; a++)
            {
                if (primes[a])
                {
                                  for (int b = a; b <= 223; b++)
                    {
                        [B][I]int c = (a * b);[/I][/B]
                        primes[c] = false;
                    }
                }
            }

    Notice how youre declaring the variables type every time you set it. Try to do that outside this codeblock (int c and then just do c= (a*b);.

    EDIT:

    Found the same mistak further down too:

     if (primes[a] == true)
                {
                   [B][I] int b = (a + 6);[/I][/B]
                    if (b <= 50000)
                    {
                        if (primes[b] == true)
                        {
                            System.out.printf("%d and %d\n", a, b);
                            counter++;
                        }
                    }
                }

    This should be where your code goes wrong, seeing as you have an array full of trues.
    I don't think that would make a difference. Norm is right the logic you are using to check if a number is prime is flawed I just ran a similar logic in VBA in excel to show this.

    9000
    9006
    9009
    9010
    9016
    9017
    9018
    9020
    9021
    9024
    9025
    9027
    9028
    9030
    9035
    9039
    9040
    9042
    9044
    9045
    9047
    9048
    9050
    9052
    9060
    9061
    9062
    9063
    9064
    9065
    9071
    9072
    9073
    9075
    9078
    9085
    9086
    9088
    9089
    9090
    9095
    9100
    9102
    9106
    9108
    9112
    9114
    9116
    9118
    9120
    9125
    9126
    9128
    9129
    9130
    9135
    9143
    9144
    9145
    9150
    9152
    9153
    9154
    9156
    9159
    9163
    9164
    9165
    9167
    9168
    9169
    9170
    9174
    9176
    9177
    9179
    9180
    9184
    9185
    9191
    9196
    9198
    9200

    Those are the only numbers your "sieve" is setting to not be prime in that range. You'll notice 9010 and 9016 (among others) are there which your sieve did catch and they aren't shown in the output.

    I see your logic as 223 * 223 is near 50000(theres still quite a few missing out) but your missing a lot of others. say 4447 * 2 or whatever but basically your missing out on a lot of even numbers because you are only going to 446 in eliminating all even numbers(i.e. divisible by 2).

    I wrote a handy little PrimeChecker but I doubt I'll get a chance to post it for you any time soon and I think it would be better if you wrote it yourself anyway.

    Mine was based on Integer factorization(<---thats a link to wikipedia but a quick google search will probably get better explainations) it can be pretty slow but I think maybe 50000 is low enough to be fast enough.

    EDIT: Or you could use a simpler one there are some shorter algorithims you could use that aren't as accurate but might be better to use I think I needed that calculator as I was checking all primes up to 2 million.
    Last edited by Faz; June 29th, 2010 at 03:24 AM.

  10. #10
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: I dont get it....please help

    Why are we checking 223 multiples btw? If were only trying to avoid the small numbers like 2 etc, why dont we just set them all to true and falsify em if they dont pass a check, starting with 2 iterating to the number itself?

  11. #11
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: I dont get it....please help

    Quote Originally Posted by Charlie View Post
    Why are we checking 223 multiples btw? If were only trying to avoid the small numbers like 2 etc, why dont we just set them all to true and falsify em if they dont pass a check, starting with 2 iterating to the number itself?
    That would be a simple but not foolproof way of doing it. If you went through and set all mulitples of 2, 3,5,7,9,11 and 13 you would get most primes but there would be some missing.(EDIT: sorry I may have misunderstood your post I thought you meant just check for low numbers did you mean check all up to 50,000?)

    EDIT However you did remind me of quite a nice way of checking numbers called the Sieve of Eratoshenes check the info here:
    Sieve of Eratosthenes - Wikipedia, the free encyclopedia

    That should work really well for an array of booleans actually.

    This is the part you should be interested in

    To find all the prime numbers less than or equal to a given integer n by Eratosthenes' method:

    Create a list of consecutive integers from two to n: (2, 3, 4, ..., n). (n being 50000 in your case)
    Initially, let p equal 2, the first prime number.
    Strike from the list all multiples of p less than or equal to n. (2p, 3p, 4p, etc.)
    Find the first number remaining on the list after p (this number is the next prime); replace p with this number.
    Repeat steps 3 and 4 until p2 is greater than n.
    All the remaining numbers in the list are prime.

    Actually check Eulers sieve as well I'm not familiar with that one it's further down the same page.
    Last edited by Faz; June 29th, 2010 at 08:40 AM.

  12. #12
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: I dont get it....please help

    Aye, that was kind of what I was thinking. Going to 50k is useless tho since a number can never be divided by more than half its value (if we want an answer bigger than, or equal to 2).

    Got this idea of how to do this another way I think. How bout this?:

    1. set a max number.
    2. Create an arraylist to skip those pesky size-issues that normal arrays have.
    3.
    import java.util.ArrayList;
     
     
    /**
     *
     * @author Charlie
     */
    public class NewClass {
     
        ArrayList primes = new ArrayList();
     
        public void primes(int maxnum){
     
    	for(int i = 3; i < maxnum ;i++){
    		primes.add(i); // add it to the arraylist
     
    		for(int i2=2; i2 <= (i/2); i2++){
    			//If we manage to divide it without getting strange numbers, we remove it from our arraylist
     
                            if(i%i2 == 0){
     
                                  primes.remove(primes.size() - 1);
                                  break;
     
    			}
    		}
     
    	}
     
        }
     
        public void printPrimes(){
            for(int i = 0; i<primes.size();i++){
                System.out.println(primes.get(i));
            }
        }
     
        public static void main(String[] args){
            System.out.println("Hi, starting to count primes: \n\n");
            NewClass sven = new NewClass();
     
            sven.primes(50000);
            sven.printPrimes();
        }
     
    }

    4. Profit.

    Checking the lists values for "sexy" primes should now be a piece of cake

    Ps. First time working with primes, so feel free to rage if I've missunderstood it.

  13. #13
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: I dont get it....please help

    Actually you only need to go to the square root of 50,000 so I think 224 (so that's where they 223 came from)
    Repeat steps 3 and 4 until p2 is greater than n.
    That came out as p2 but it's actually p squared.

Similar Threads

  1. Dont laugh at me
    By Neblin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 3rd, 2009, 08:18 AM