|
-
Mar 31st, 2005, 03:46 PM
#1
Thread Starter
Frenzied Member
prime numbers[Resolved by manavo11]
I was trying to create a program that printed all the prime numbers within a given limit. My program is printing out all of the numbers! See if you can figure out where I'm going wrong:
Code:
import java.io.*;
class PrimeCheck
{
public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Limit --> ");
int limit = Integer.parseInt(br.readLine());
System.out.println("");
System.out.print("Primes --> ");
int count = 0;
for (int i=2; i<=limit; i++)
{
for (int j=1; j<=i; j++)
{
if (j * i == i)
{
count++;
}
if (i * j == i)
{
count++;
}
if (count == 2)
{
System.out.print(i + " ");
}
count = 0;
}
}
}
}
Last edited by manavo11; Apr 3rd, 2005 at 02:58 PM.
-
Mar 31st, 2005, 04:13 PM
#2
-
Mar 31st, 2005, 05:29 PM
#3
Thread Starter
Frenzied Member
Re: prime numbers
 Originally Posted by manavo11
I'm sure it can be optimized but it works 
Thank you. This is the way I wanted it, because I already had a one line approach that looked something like this:
if ((i < 8 && i % 2 != 0) || (i > 9 && i % 2 != 0 && i % 3 != 0 && i % 5 != 0))
but of course I didn't like that!
So thank you for helping me.
-
Apr 2nd, 2005, 06:18 PM
#4
-
Apr 2nd, 2005, 08:55 PM
#5
Thread Starter
Frenzied Member
Re: prime numbers
 Originally Posted by manavo11
You're welcome  How about "Resolving" the thread? 
Ok, I'll even add your name in the title
-
Apr 3rd, 2005, 08:50 AM
#6
-
Apr 3rd, 2005, 10:18 AM
#7
Thread Starter
Frenzied Member
Re: prime numbers
 Originally Posted by manavo11
It's actually manavo and not manova, but it's a nice gesture 
What? That's how I spelled it?
-
Apr 3rd, 2005, 10:19 AM
#8
Re: prime numbers[Resolved by manova]
I both added the green check mark and changed the name for you....
-
Apr 3rd, 2005, 10:27 AM
#9
Thread Starter
Frenzied Member
Re: prime numbers[Resolved by manova]
 Originally Posted by NoteMe
I both added the green check mark and changed the name for you.... 
\
No I did it before you.
-
Apr 3rd, 2005, 10:56 AM
#10
Re: prime numbers[Resolved by manavo]
You added [Resolved] Then I added the green check mark. Then I changed the name to Manavo. Was it you that changed it back to manova? Well I have changed it back to Manavo now at least...
-
Apr 3rd, 2005, 02:56 PM
#11
-
Apr 3rd, 2005, 03:07 PM
#12
Thread Starter
Frenzied Member
Re: prime numbers[Resolved by manova12]
 Originally Posted by manavo11
You're killing me. manova12?  I'll change it myself 
Noteme is such a bad person.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|