# Printing prime numbers from 1 to 50

Page 1 of 1

## 6 Replies - 22960 Views - Last Post: 27 September 2010 - 03:04 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=192315&amp;s=4c14807921a5ed1a18a2cac392b8fd81&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• 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

Reputation: 0
• 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.

### #3 sean bean

Reputation: 0
• 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.

### #4 milo lamar

Reputation: 0
• 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).

### #5 sean bean

Reputation: 0
• 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!

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,170
• Joined: 27-December 08

## Re: Printing prime numbers from 1 to 50

Posted 27 September 2010 - 02:36 PM

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

### #7 pbl

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

Reputation: 8365
• Posts: 31,956
• 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)