I have written a function to work on prime numbers using the sieve of Eratosthenes method. The function works fine using integers but I am now trying to implement support for long so that I can work with large numbers.
I cannot seem to get the function working with longs and can't see an obvious reason why.
Errors refer to the typical precision warning from typecasting etc but I cannot work out what's causing them:
./com/wkilgour/lang/Maths.java:21: error: possible loss of precision
boolean[] isPrime = new boolean[n + 1];
^
required: int
found: long
./com/wkilgour/lang/Maths.java:24: error: possible loss of precision
isPrime[i] = true;
^
required: int
found: long
./com/wkilgour/lang/Maths.java:27: error: possible loss of precision
if (isPrime[i])
^
required: int
found: long
./com/wkilgour/lang/Maths.java:29: error: possible loss of precision
isPrime[i * j] = false;
^
required: int
found: long
4 errors
Here is the function:
public static boolean[] primeSieve(long n)
{
boolean[] isPrime = new boolean[n + 1];
for (long i = 2L; i <= n; i++)
isPrime[i] = true;
for (long i = 2L; i*i <= n; i++)
if (isPrime[i])
for (long j = i; i*j <= n; j++)
isPrime[i * j] = false;
return isPrime;
}
Any help would be greatly appreciated!