473,743 Members | 2,248 Online

# Prime number generator

4 New Member
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??
Feb 6 '08 #1
15 6624
Laharl
849 Recognized Expert Contributor
What is the definition of a prime number? In other words, what is a simple test that will immediately disqualify any number as prime?
Feb 6 '08 #2
1,216 Recognized Expert Top Contributor
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.
Feb 6 '08 #3
r035198x
13,262 MVP
... 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.
Feb 6 '08 #4
1,216 Recognized Expert Top Contributor
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.
Feb 6 '08 #5
mjslaugh
4 New Member
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??
Feb 6 '08 #6
664 Recognized Expert Contributor
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();

Feb 6 '08 #7
1,216 Recognized Expert Top Contributor
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.
Feb 6 '08 #8
r035198x
13,262 MVP
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.
Feb 6 '08 #9
664 Recognized Expert Contributor
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?