Results 1 to 12 of 12

Thread: prime numbers[Resolved by manavo11]

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved 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.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: prime numbers

    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.println("Primes --> ");
    
    		boolean bIsPrime;
    
    		for (int i=2; i<=limit; i++)
    		{
    			bIsPrime=true;
    			for (int j=2; j<i; j++)
    			{
    				if (i%j==0)	{
    					bIsPrime=false;
    				}
    			}
    			if (bIsPrime==true) {
    				System.out.println(i);
    			}
    		}
    	}
    }
    I'm sure it can be optimized but it works


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: prime numbers

    Quote 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.

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: prime numbers

    Quote Originally Posted by System_Error
    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.
    You're welcome How about "Resolving" the thread?


    Has someone helped you? Then you can Rate their helpful post.

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: prime numbers

    Quote Originally Posted by manavo11
    You're welcome How about "Resolving" the thread?

    Ok, I'll even add your name in the title

  6. #6
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: prime numbers

    Quote Originally Posted by System_Error
    Ok, I'll even add your name in the title
    It's actually manavo and not manova, but it's a nice gesture


    Has someone helped you? Then you can Rate their helpful post.

  7. #7

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: prime numbers

    Quote Originally Posted by manavo11
    It's actually manavo and not manova, but it's a nice gesture
    What? That's how I spelled it?

  8. #8
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: prime numbers[Resolved by manova]

    I both added the green check mark and changed the name for you....

  9. #9

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: prime numbers[Resolved by manova]

    Quote Originally Posted by NoteMe
    I both added the green check mark and changed the name for you....
    \
    No I did it before you.

  10. #10
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    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...

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: prime numbers[Resolved by manova12]

    You're killing me. manova12? I'll change it myself


    Has someone helped you? Then you can Rate their helpful post.

  12. #12

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: prime numbers[Resolved by manova12]

    Quote 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
  •  



Click Here to Expand Forum to Full Width