6 Replies - 14186 Views - Last Post: 27 September 2010 - 03:04 PM Rate Topic: ***-- 2 Votes

#1 ali.zad  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-October 09

Printing prime numbers from 1 to 50

Posted 27 September 2010 - 12:44 PM

i am new to java and i have tried many times to make my code work can anybody tell me whats the problem? i use notepad, command prompt, javac and java command to run my code.

public class prime
{
 public static void main(String abc[])
 {
  int p;
  for (int i = 1; i<=50; i++)
  {
   for (int j = 2; j<i; j++)
   {
    if(i%j != 0)
    {p = 1;}
    else
    {p = 0;
     break;}
    
   }
    if(p == 1)//ERROR HERE
    System.out.println(i+" is Prime");
  }
 }
}

This post has been edited by ali.zad: 27 September 2010 - 12:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Printing prime numbers from 1 to 50

#2 milo lamar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-May 10

Re: Printing prime numbers from 1 to 50

Posted 27 September 2010 - 01:12 PM

I got an initialization error. Set int p=0; before your loops and it compiled (for me anyway). And the algorithm appears to work as it did print out primes lower than 50.
Was This Post Helpful? 0
  • +
  • -

#3 sean bean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-September 10

Re: Printing prime numbers from 1 to 50

Posted 27 September 2010 - 01:19 PM

I just done a quite similar question as yours, I can show you the method

public class Prime1000
{
  public static void main(String[] args)
  {
    boolean flag;                 // true if n is prime
    int d;                        // divisor to test
    int k;                        // largest integer less than square root
    int n;                        // number we are checking if prime
    int x;

    System.out.println("Printing prime numbers from 1 to 50 " );
    for (n = 2; n < 50; n ++)
    {
      if (n == 2)
        flag = true;              // 2 is prime
      else if ((n % 2) == 0)
        flag = false;             // even numbers are not prime
      else
      {
        flag = true;              // assume n is prime
        k = (int) Math.sqrt((double) n);
        for (d = 3; d <= k; d = d + 2)
        {
          if ((n % d) == 0)       // does d divide n?
            flag = false;         // yes, n is not prime
        }
      }
      if (flag)                   // is this number prime?
        System.out.print(n + "  ");
    }
  }
}



As I am new to java,too. Can U take a look my posted question, as user shows sean bean. let me know if you have any other questions.
Was This Post Helpful? 0
  • +
  • -

#4 milo lamar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 17-May 10

Re: Printing prime numbers from 1 to 50

Posted 27 September 2010 - 01:33 PM

Seans algorithm is more efficient because it doesn't check even numbers because they're already known to not be prime. For very large prime lists, you'd want to do more reductions like this, such as taking out multiples of 3, 5, 7, 11, ... nth prime
But that is very trivial if your end goal is only to print primes less than 50.
You wouldn't start seeing speed increases from those reductions until several orders higher (5*10^50, for example).
Was This Post Helpful? 0
  • +
  • -

#5 sean bean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-September 10

Re: Printing prime numbers from 1 to 50

Posted 27 September 2010 - 01:35 PM

yes, if we change this part: System.out.println("Printing prime numbers from 1 to 50 " );

12 for (n = 2; n < 50; n ++)


n<1000, it gonna come out more prime. that is speedy!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10658
  • View blog
  • Posts: 39,571
  • Joined: 27-December 08

Re: Printing prime numbers from 1 to 50

Posted 27 September 2010 - 02:36 PM

View Postsean bean, on 27 September 2010 - 04:19 PM, said:

I just done a quite similar question as yours, I can show you the method

public class Prime1000
{
  public static void main(String[] args)
  {
    boolean flag;                 // true if n is prime
    int d;                        // divisor to test
    int k;                        // largest integer less than square root
    int n;                        // number we are checking if prime
    int x;

    System.out.println("Printing prime numbers from 1 to 50 " );
    for (n = 2; n < 50; n ++)
    {
      if (n == 2)
        flag = true;              // 2 is prime
      else if ((n % 2) == 0)
        flag = false;             // even numbers are not prime
      else
      {
        flag = true;              // assume n is prime
        k = (int) Math.sqrt((double) n);
        for (d = 3; d <= k; d = d + 2)
        {
          if ((n % d) == 0)       // does d divide n?
            flag = false;         // yes, n is not prime
        }
      }
      if (flag)                   // is this number prime?
        System.out.print(n + "  ");
    }
  }
}



As I am new to java,too. Can U take a look my posted question, as user shows sean bean. let me know if you have any other questions.

sean bean, I appreciate your willingness to help. However, please keep rule #1 in mind. We are here to help others debug their code, not hand out code. If you would like to submit code you feel is useful, the snippets section is the place to do that. :)
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Printing prime numbers from 1 to 50

Posted 27 September 2010 - 03:04 PM

and no need to test the even numbers

for (n = 2; n < 50; n ++)

should be

for (n = 3; n < 50; n += 2)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1