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?