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. Could you please help me how to create this program to calculate prime numbers?

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

Joined: Aug 08, 2006
Posts: 34

posted

0

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

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.

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 ]

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.

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra

programming diversions more likely. Most of us write such programs as brainteasers after all

42

Brijesh Shah
Ranch Hand

Joined: Aug 08, 2006
Posts: 34

posted

0

Hi All,

So many comments!!! 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--; } }

Please help me why this code does not lead me to the end results??? Thanks in advance....

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.

And I agree about this being a "beginner's" question.

CR

Brijesh Shah
Ranch Hand

Joined: Aug 08, 2006
Posts: 34

posted

0

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 BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value of n:"); //reads the integers using readLine() and uses parseInt() to convert these strings //into their int equivalents n=Integer.parseInt(br.readLine()); 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

Joined: Oct 13, 2005
Posts: 43995

33

posted

0

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.