-
Java programming with textpad.
Does anyone know how to create a program to print prime numbers?
Once compiled, the program can ask me to enter an integer, but once I enter the integer(Lets say.. 10), I need all the prime numbers under 10 to print. So... 1,2,3,5,7 should print.
Thanks.
-
To tell if a number is prime you need to loop through the numbers less than it and see if that number is divisable by it.
Obviously don't use the number 1 because all integers are divisible by one. Also, you only need to try the numbers up to the integer in question divided by 2.
eg if the user enters 10, you only need to test up to 5.
If the user enters 12, test up to 6.
To see if one number is divisble by another number use the modulus operator (%). This number works out the remainder, so if number A is divisible by number B the expression A%B will equal 0.
-
I was able to create this, but I have a small problem. How can I make it ask for the amount once and than I press any key to close the box? Every time I enter an integer, it gives me the results and asks the question again.
import java.io.*;
public class ListPrimes {
public static void main(String [] argv){
InputStream iStream = System.in;
BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
String bufStr;
try {
System.out.print("Please enter # to list primes up to> ");
while ((bufStr = bReader.readLine()) != null) {
try{
int stop = Integer.parseInt(bufStr);
for(int x=1;x<=stop;x+=2){
if(isPrime(x)){
System.out.print(x+" ");
}
}
System.out.print("\nPlease enter # to list primes up to> ");
}
catch(Exception e1){
System.out.println("("+bufStr+") caused an error: "+e1.toString());
}
}
} catch (Exception e) {
System.out.println("Error: "+e.toString());
}
}
public ListPrimes(){
}
public static boolean isPrime(long _p){
if (_p<2 || _p%2==0) return false;
long max=(long)(Math.sqrt(_p));
for (long i = 3; i <= max; i+=2) {
if (_p%i==0) return false;
}
return true;
}
}
-
Here is the solution, although I didn't quite get the problem:
Code:
import java.io.*;
public class ListPrimes {
public static void main(String [] argv){
InputStream iStream = System.in;
BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
String bufStr;
try {
System.out.print("Please enter # to list primes up to (zero to stop)> ");
while ((bufStr = bReader.readLine()) != null) {
try{
int stop = Integer.parseInt(bufStr);
if (stop==0) break;
for(int x=1;x<=stop;x+=2){
if(isPrime(x)){
System.out.print(x+" ");
}
}
System.out.print("\nPlease enter # to list primes up to (zero to stop)> ");
} catch(Exception e1){
System.out.println("("+bufStr+") caused an error: "+e1.getMessage());
}
}
} catch (Exception e) {
System.out.println("Error: "+e.toString());
}
}
public ListPrimes(){}
public static boolean isPrime(long _p){
if (_p<2 || _p%2==0) return false;
long max=(long)(Math.sqrt(_p));
for (long i = 3; i <= max; i+=2) {
if (_p%i==0) return false;
}
return true;
}
}
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks