Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Prime Number Checker--working, but won't take numbers long enough.

807588Jun 10 2009 — edited Jun 12 2009
Hi, all:

I've build a prime number checker. Its purpose is to find primes after 1 trillion. It works fine to a billion, but at 10 billion the compiler gives me this:
~/Documents/ProgAndDev/JavaPractice/Speculate : Yes, Supreme Empress? $ javac Prime.java
Prime.java:21: integer number too large: 10000000000
  public static long number = 10000000000;
                              ^
1 error
Here's the class:
import java.lang.Long;
import java.lang.Math;

class Prime {

  public static long number = 1000000000;
  public static boolean p;

  public static boolean isPrime (long n) {
    boolean prime = true;
    for (long i = 3; i <= Math.sqrt(n); i += 2){
      if (n % i == 0) {
	prime = false;
	p = false;
	break;
      }
      if (( n%2 !=0 && prime && n > 2) || n == 2) {
	p = true;
	return true;
      } 
      else {
	p = false;
	return false;
      }
    }
    return prime;
  }

  public static void main(String args[]) {
    isPrime(number);
    System.out.println("It is " + p + " that n is a prime number.");
  }
}
Do I need to wrap the long?

Comments

Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 10 2009
Added on Jun 10 2009
15 comments
514 views