wood burning stoves*
The moose likes Java in General and the fly likes Java Prime Number Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Prime Number Program" Watch "Java Prime Number Program" New topic
Author

Java Prime Number Program

Brijesh Shah
Ranch Hand

Joined: Aug 08, 2006
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.
Could you please help me how to create this program to calculate prime numbers?

Thanks in advance!!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
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.


Ping & DNS - my free Android networking tools app
Brijesh Shah
Ranch Hand

Joined: Aug 08, 2006
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

Joined: Sep 18, 2000
Posts: 557
    
    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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
It does not display the prime numbers between 1 to 32.

Now we're getting somewhere. What DOES it display?
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
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.


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

Joined: Sep 18, 2000
Posts: 557
    
    7

Additionally, this really should be moved to JIG(beginning)
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
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
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....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38031
    
  22
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
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: 38031
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Prime Number Program
 
Similar Threads
Returning if a number is prime. (Chapter on Objects and classes. OOP)
WA #1.....word association
project euler
Prime Numbers
prime numbers