Results 1 to 5 of 5
  1. #1
    Sulfuri is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Trying to generate prime numbers

    I started learning java yesterday and I've already gotten into trouble;) I'm trying to make a simple program that generates all prime numbers below 10

    My problem is that for some reason the code at the bottom keeps printing out the wrong numbers. If i run the program
    it will give me the numbers: 3, 5 , 7 , 9

    I'm not sure if this is correct but I think it gives me the 9 because
    Java Code:
     if(i%n != 0)
    Since 9%2 = 1 or in other words not 0 the program thinks "oh ok i'm done here adding a number to "i" and continuing"

    Question is how do write the program so that when i = x it checks every number from 2 to x if NONE of these numbers has a remainder that does != 0 it prints it out as a prime number?
    So for example if we set i = 5 then it would look like this:

    5%2 = 1
    5%3 = 2
    5%4 = 1

    5%x != 0 print as prime

    Java Code:
    	while(i  < 10)
    	  {
    		
    
    		for(int n = 2; n < i; n++)
    		{	
    			if(i%n != 0) // is prime
    			{
    				System.out.println(i + " is a prime");
    				break;
    				
    			}
    			if(i%n == 0)
    				break;
    	
    		
    	}
    	i++;
      }

  2. #2
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Trying to generate prime numbers

    The issue you are having with the loop is that no matter what the loop will break after the first iteration. To fix this issue you will need to do something along the lines of

    Java Code:
    boolean isPrime = true;
    while(i  < 10)
    {
          isPrime = true;
          for(int n = 2; n < i; n++)
          {  
              if(i%n == 0)
              {
                  isPrime = false;
                  break;
              }
          }
       
       if(isPrime)
          System.out.println(i + " is a prime");  
     }
     i++;

  3. #3
    Sulfuri is offline Member
    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Default Re: Trying to generate prime numbers

    Quote Originally Posted by jhuber151 View Post
    The issue you are having with the loop is that no matter what the loop will break after the first iteration. To fix this issue you will need to do something along the lines of

    Java Code:
    boolean isPrime = true;
    while(i  < 10)
    {
          isPrime = true;
          for(int n = 2; n < i; n++)
          {  
              if(i%n == 0)
              {
                  isPrime = false;
                  break;
              }
          }
       
       if(isPrime)
          System.out.println(i + " is a prime");  
     }
     i++;

    I tried to just copy paste this code into my program and it just keeps printing "2 is a prime"


    EDIT: I rewrote the code but i tried with the true and false statements that you suggested, it works now ;)
    thanks for help, code below if someones intressted.

    Java Code:
        boolean IsPrime = true;
      while(x < 99)
      {
    	  IsPrime = true;
    	  for(Counter = 2;Counter < x;Counter++)
    	  {
    		  if(x%Counter == 0 && Counter > 1)
    		  {
    			  //not a prime
    			  IsPrime = false;
    		  }
    	  }
    	  
    	  if(IsPrime)
    	  {
    		  IsPrime = true;
    		  System.out.println(x + " is a prime");
    	  }
    	  
      x++;
      }
    Last edited by Sulfuri; 01-17-2013 at 08:24 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,996
    Blog Entries
    7
    Rep Power
    19

    Default Re: Trying to generate prime numbers

    FYI: Counter i always larger than 1 (because you have said so in the header of your for-loop) so you can scratch that test in you if-statement.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    jhuber151 is offline Member
    Join Date
    Jul 2012
    Posts
    55
    Rep Power
    0

    Default Re: Trying to generate prime numbers

    That was my fault with it being an infinite loop. I left the i++ outside the brackets of the while loop. If you place the i++ inside the last brackets the code should work fine. ie.
    Java Code:
    boolean isPrime = true;
    while(i  < 10)
    {
          isPrime = true;
          for(int n = 2; n < i; n++)
          { 
              if(i%n == 0)
              {
                  isPrime = false;
                  break;
              }
          }
        
       if(isPrime)
          System.out.println(i + " is a prime"); 
    
     i++;
    }

Similar Threads

  1. prime numbers
    By Daniel93 in forum New To Java
    Replies: 8
    Last Post: 01-06-2013, 07:33 PM
  2. Generate prime numbers within fiboncacci series?
    By Manish87 in forum New To Java
    Replies: 9
    Last Post: 08-08-2010, 04:07 AM
  3. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 06:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •