Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Java Prime Number Program

Brijesh Shah
Ranch Hand
Posts: 34
Hi,

I am getting stuck while creating the Prime number program to calculate between 1 to N where N is provided by the user through command line.

Ulf Dittmer
Rancher
Posts: 42967
73
Where exactly are you stuck? Passing command line parameters to a Java program, how to test for prime numbers, how to compile and run a Java program, ... ? Let us know what the exact problem is, and we'll get you going again.

Brijesh Shah
Ranch Hand
Posts: 34
Hi,

When I pass any number through Command line.
For example - 32
It does not display the prime numbers between 1 to 32.
If possible can you provide me the code that will return the prime numbers between 1 to N

Steve Fahlbusch
Bartender
Posts: 605
7
As requested prior, please provide (ie: post here) what you have done so far (ie: post your code), explain what it is that you expect to happen and show what is occuring. That way the great folks here can help you understand where you are "going wrong" as well as documenting learnings for others.

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15279
39
Originally posted by Vicky Shah:
If possible can you provide me the code that will return the prime numbers between 1 to N

In other words, are you asking somebody to do your homework for you? Please do your own homework - homework is for you to learn something, and you learn by doing it, not by asking other people to do it for you.

So write some code yourself. If there are specific issues that you get stuck with, feel free to ask for hints and tips here.
[ August 14, 2006: Message edited by: Jesper Young ]

Ulf Dittmer
Rancher
Posts: 42967
73
It does not display the prime numbers between 1 to 32.

Now we're getting somewhere. What DOES it display?

Rusty Shackleford
Ranch Hand
Posts: 490
If you haven't started, start with the basics. Use the definition of prime numbers as your starting point to build an algorithm. There are some optimizations to cut down on how many numbers you need to test for any N, but just get it working, and go from there.

Steve Fahlbusch
Bartender
Posts: 605
7
Additionally, this really should be moved to JIG(beginning)

Jeroen T Wenting
Ranch Hand
Posts: 1847
programming diversions more likely. Most of us write such programs as brainteasers after all

Brijesh Shah
Ranch Hand
Posts: 34
Hi All,

Ah...
Well I could not reply you guys as i was not connected to the net.

I have a program here:
/*Program to print prime numbers between 1 to n*/
while(i<=n-1)
{
if((n%i)==0)
{
break;
}
i++;
}
if(i==n)
{
System.out.println("The prime numbers between 1 and " +n+ " are:");
System.out.println(""+n);
}
n--;
}
}

Campbell Ritchie
Sheriff
Posts: 48940
60
Please use the code tags whenever you post code on screen; it makes it much easier to read and preserves the indentation.
Your algorithm only test one number, "n," and sees whether it is a prime number. So if you pass 32, which is not a prime number, it finds it is not a prime number, and doesn't print anything.

You are using an inefficient algorithm; any non-prime number which is not a perfect square of a prime number (eg 32, not 49) will have at least one factor which is smaller than its square root. So you only need to count as far as the square root of the largest number you are interested in.
You need to go back to ancient Greece; find the algorithm called the "sieve of Eratosthenes." I think that is the correct spelling.

CR

Brijesh Shah
Ranch Hand
Posts: 34
Hi Campbell,

I have his program to find the prime numbers between 1 to N

import java.io.*;
class Prime
{
//if error occurs during writing IOException is thrown
public static void main(String[] args) throws IOException
{
int n,i=2;
//reads a character from the input stream and returns it as an integer value
System.out.println("Enter the value of n:");
//reads the integers using readLine() and uses parseInt() to convert these strings
//into their int equivalents
while(i<=n-1)
{
if((n%i)==0)
{
break;
}
i++;
}
if(i==n)
{
System.out.println("The prime numbers between 1 and " +n+ " are:");
System.out.println(""+n);
}
n--;
}
}

If possiblem can you provide me the code for this program....

Campbell Ritchie
Sheriff
Posts: 48940
60
Your while loop only goes round until you find a divisor, then your i-- bit doesn't do anything.

You will have to look for the sieve of Eratosthenes, which is a more efficient algorithm for finding all the prime numbers in a range.