This discussion is archived
1 2 3 4 5 8 111 Replies Latest reply: Sep 4, 2008 2:41 PM by 807589 Go to original post
• 30. Re: listing prime numbers 1-100
Currently Being Moderated
why is there no output?
Because n is always 1, and therefore n-1 is 0. m starts at 2. Therefore m <= n-1 is always false, so the body of the loop is never executed.
its probably just a simple mistake that i overlooked
but i suck at debugging right now.
Chuck it away, start from scratch. You still need to do the "hard" bit, which is work out what you want your code to actually do. Write down in plain English (here, online, so we can see your reasoning) the steps you would use to work out if the number 23 is prime. If you can't do that you can't write the code to do it either, so you need to get that bit clear first.
• 31. Re: listing prime numbers 1-100
Currently Being Moderated
By definition, a prime number is a natural number that has exactly two distinct natural number divisors, which are 1 and the prime number itself.
Consequently, 1 is not a prime number (as well as zero and negative numbers, which are not natural numbers)
Therefore, your code should first check that the number to test is greater than 1.
• 32. Re: listing prime numbers 1-100
Currently Being Moderated
• 33. Re: listing prime numbers 1-100
Currently Being Moderated
x^2+x+41 is a prime number for every integral value of x in [ 0 ... (41-2) ]

kind regards,

Jos (aka Supplier Of Useless Facts ;-)
• 34. Re: listing prime numbers 1-100
Currently Being Moderated
A efficient form of calculate N first primes is storing in a list if a number is prime and not storing otherwise.
``````List<Integer> primes=new LinkedList<Integer>();
int num=3;
do{
boolean prime=true;
for(int prime:primes){
if(num%prime==0){
prime=false;
break;
}
if(prime)
num+=2;
}while(num<100);  // You can substitute 100 by any integer.
• 35. Re: listing prime numbers 1-100
Currently Being Moderated
An efficient way of being a knob is to provide homework solutions. How's the OP supposed to learn how to solve programming problems if a fool like you hands it to him on a plate?

I am optimistic that the OP has the sense to ignore your solution and write his own, but sheesh, don't be such a pratt.
• 36. Re: listing prime numbers 1-100
Currently Being Moderated
> That's always true for any value of count.

No so. It's ArithmeticException for count==0.

except that he starts the loop with count == 1, thus
avoiding the Exception.

~Tim

Message was edited by:
SomeoneElse
Actually you will find that the loop will keep executing till you reach Integer.MAX_VALUE and then it will wrap around till it reaches zero and then throw an Arithmetic exception...
• 37. Re: listing prime numbers 1-100
Currently Being Moderated
Actually you will find that the loop will keep
executing till you reach Integer.MAX_VALUE and then
it will wrap around till it reaches zero and then
throw an Arithmetic exception...
• 38. Re: listing prime numbers 1-100
Currently Being Moderated
Write down in plain English
(here, online, so we can see your reasoning) the
steps you would use to work out if the number 23 is
prime. If you can't do that you can't write the code
to do it either, so you need to get that bit clear
first.
to see if 23 is prime, i would find out if it is divisble (evenly) by 2,3 or 5.
23/2 = 11.5
23/3 = 7.6666...
23/6 = 4.6

neither 2,3 nor 5 evenly go into 23.
23 is prime.

if i did that wrong, i better go back and consult Mr.Flunk about my future.
• 39. Re: listing prime numbers 1-100
Currently Being Moderated
Are all numbers not evenly divisible by 2, 3, or 5 prime? I don't think so! You've definitely got some more numbers to check.

D.
• 40. Re: listing prime numbers 1-100
Currently Being Moderated
okay, any number greater than 2,3 or 5.
they can divide by themselves and 1, making them prime. they cannot be divided by anything else....
numbers above five which can be divided by 2,3 or 5 are not prime.
• 41. Re: listing prime numbers 1-100
Currently Being Moderated
to see if 23 is prime, i would find out if it is
divisble (evenly) by 2,3 or 5.
23/2 = 11.5
23/3 = 7.6666...
23/6 = 4.6

either 2,3 nor 5 evenly go into 23.
23 is prime.

if i did that wrong, i better go back and consult
; )
That is correct. You only need to check upto 5 because 5*5 > 23. And you need not check 4 because 2 already divides 4.
No need to see Mr Flunk.
• 42. Re: listing prime numbers 1-100
Currently Being Moderated
Are all numbers not evenly divisible by 2, 3, or 5
prime? I don't think so! You've definitely got some
more numbers to check.

D.
I asumed he said 5 because 5*5 > 23.
• 43. Re: listing prime numbers 1-100
Currently Being Moderated
Huh? By your logic 21 is prime. One of us is smoking crack.
• 44. Re: listing prime numbers 1-100
Currently Being Moderated
21 can be divided by 3, to get an even 7.

how did you get 21 as prime?
1 2 3 4 5 8