I have to find number of prime numbers less than or equal to a given number n
. I am using Sieve of Eratosthenes for finding primes.
This is the code I've written:
static int find_prime(int n) {
boolean[] arr = new boolean[n+1];
Arrays.fill(arr, true);
for(int i=2;i<=n;i++) {
if(arr[i]==true) {
for(int j=2*i; j<=n; j+=i)
arr[j] = false;
}
}
int count=0;
for(int i=2;i<=n;i++) {
//System.out.print(arr[i]+" ");
if (arr[i] == true)
count++;
}
return count;
}
This code works well for integer
values, but I have to implement it for long
numbers. And Java doesn't allow creating long
size array so boolean[] arr = new boolean[n+1];
doesn't work.
Can someone suggest me a way to solve this?