Prime number generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mjslaugh
    New Member
    • Feb 2008
    • 4

    Prime number generator

    I am new to Java and programming, I have an assignment that I am a little stuck on and I was hoping to get a little help if anyone is willing. I have read a few posts on the topic and it seems most people on here want to see where you've gotten and then give a little help to get past roadblocks, so here goes.


    My program should accept an integer and then print out the primes between 0 and that number, so my idea is to start a for loop that increments one at a time and then checks it for primality and prints it if its prime, until it gets to the input integer the problem is that I can't find a test for primality that doesn't have a built in list. any suggestions??
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    What is the definition of a prime number? In other words, what is a simple test that will immediately disqualify any number as prime?

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      Was there any discussion or hints given with your assignment? There's a classic algorithm to find all the primes less than a given limit N, but I don't want imply you have to solve it that way.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by mjslaugh
        ... the problem is that I can't find a test for primality that doesn't have a built in list. any suggestions??
        I'm sorry but I don't understand the part above.
        You could have a look at BigInteger.isPr obablePrime but perhaps your teachers won't accept it.

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by r035198x
          I'm sorry but I don't understand the part above.
          You could have a look at BigInteger.isPr obablePrime but perhaps your teachers won't accept it.
          Ah, industry-grade primes! No, I think the teacher wouldn't be impressed. It seems to be the point of the exercise.

          Comment

          • mjslaugh
            New Member
            • Feb 2008
            • 4

            #6
            Well, I was just going with my math class difinition, the assignment was a little vauge, but it did say no built in methods or arrays, so the conditions that I uderstand for a prime are that a number is prime if and only if it is divisible by one and itself. I had thought about trying to inverse that to create a limiting conditon but how??

            Comment

            • nomad
              Recognized Expert Contributor
              • Mar 2007
              • 664

              #7
              Originally posted by mjslaugh
              Well, I was just going with my math class difinition, the assignment was a little vauge, but it did say no built in methods or arrays, so the conditions that I uderstand for a prime are that a number is prime if and only if it is divisible by one and itself. I had thought about trying to inverse that to create a limiting conditon but how??
              I would not inverse it just use a + counter.
              I would use a for loop with a
              Integer.parseIn t();

              nomad

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by mjslaugh
                Well, I was just going with my math class difinition, the assignment was a little vauge, but it did say no built in methods or arrays, so the conditions that I uderstand for a prime are that a number is prime if and only if it is divisible by one and itself. I had thought about trying to inverse that to create a limiting conditon but how??
                No arrays? There goes my choice: the Sieve of Eratosthenes. There's a very cool animated gif of the algorithm on that page, by the way.

                I don't know what you mean by "trying to inverse that to create a limiting conditon". I think you are over-thinking. Why not try the old "understand how you do it manually" approach? How would you determine whether or not 91 was prime? Do this in deliberate steps.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by BigDaddyLH
                  No arrays? There goes my choice: the Sieve of Eratosthenes. There's a very cool animated gif of the algorithm on that page, by the way.

                  I don't know what you mean by "trying to inverse that to create a limiting conditon". I think you are over-thinking. Why not try the old "understand how you do it manually" approach? How would you determine whether or not 91 was prime? Do this in deliberate steps.
                  Ah, that sieve again. I was hoping no one would bring that one up.
                  @SmallDaddy: Check your PMs please.

                  Comment

                  • nomad
                    Recognized Expert Contributor
                    • Mar 2007
                    • 664

                    #10
                    Originally posted by BigDaddyLH
                    No arrays? There goes my choice: the Sieve of Eratosthenes. There's a very cool animated gif of the algorithm on that page, by the way.

                    I don't know what you mean by "trying to inverse that to create a limiting conditon". I think you are over-thinking. Why not try the old "understand how you do it manually" approach? How would you determine whether or not 91 was prime? Do this in deliberate steps.
                    was mjslaugh thinking of a negative counter?
                    nomad

                    Comment

                    • BigDaddyLH
                      Recognized Expert Top Contributor
                      • Dec 2007
                      • 1216

                      #11
                      Originally posted by r035198x
                      @SmallDaddy: Check your PMs please.
                      Done. And done. And done.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by nomad
                        was mjslaugh thinking of a negative counter?
                        nomad
                        No he was talking about reversing the "divisible by 1 and itself part" to finding any number other that 1 and itself that divides into the given number. Which will work for him.

                        Comment

                        • jkmyoung
                          Recognized Expert Top Contributor
                          • Mar 2006
                          • 2057

                          #13
                          Are you allowed to store your results as you get them?

                          It would be a dynamic array created as your program ran, not a 'built-in array'.
                          Then you could check whether your current number is divisible by any of the smaller primes, up to it's square root.

                          Comment

                          • BigDaddyLH
                            Recognized Expert Top Contributor
                            • Dec 2007
                            • 1216

                            #14
                            Originally posted by jkmyoung
                            Are you allowed to store your results as you get them?

                            It would be a dynamic array created as your program ran, not a 'built-in array'.
                            Then you could check whether your current number is divisible by any of the smaller primes, up to it's square root.
                            I'm not a mind reader, but if the teacher forbade arrays, I can't imagine in my wildest dreams that he'd be glad to see a list in its place!

                            Comment

                            • mjslaugh
                              New Member
                              • Feb 2008
                              • 4

                              #15
                              This is the structure that I have so far. You can see what boolean condition that I am missing.

                              inInt = kb.nextInt();

                              for(int x = 0; x < inInt; x++){

                              if( )\\not prime
                              else
                              System.out.prin t(x + " ");

                              The problem is that I can't figure out how to rule out if a number is prime other than checking divisibility of several numbers. Or should I embrace that idea and write a method that returns the boolean condition based on dividing several numbers?? I really apreciate the help everyone.

                              Comment

                              Working...