I am just learning java and have an issue writing a program that uses the sieve of Eratosthenes. The compilation error is below the code. Any help with this problem is greatly appreciated. Thanks much.
public class Main {
public static void main(String[] args) {
int[] arr = new int[1000000];
for (int i = 0; i < 1000000; ++i)
{
arr[i] = i;
}
sieve(arr);
for (int elem :arr)
{
System.out.println(elem);
}
}
public static void sieve(int[] array)
{
int n = 0;
for (int i = 2; i < 1000; ++i)
{
if (arr[i] != 0)
{
for (int j = (int)Math.pow(i, 2) + n * i; j < 1000000; ++j)
{
do
{
arr[j] = 0;
n++;
}while ( n < 1000000);
}
}
}
}
}
Here is the compilation error javac project3.java
project3.java:23: error: cannot find symbol
if (arr[i] != 0) {
^
symbol: variable arr
location: class project3
project3.java:28: error: cannot find symbol
arr[j] = 0;
^
symbol: variable arr
location: class project3
2 errors
** I compiled this as class project3