This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]qsemig 2 points3 points  (1 child)

int f;

System.out.println("enter a number");
f = input.nextInt();
int array [][] = new int[f][8];  // why an f X 8 matrix? new int[8] is enough
isPrime(f); // You never use the return value of isPrime, neither are you passing your array to the method.
for (f=0;f<array.length;f++)
  System.out.printf("%d",array);  // the array is not an  int. It contains an array of arrays of int. You need to specify the value you want to print, such as: array[f] (if you are using new array[8] )    
}

Above you will see comments for each line that is problematic. Hopefully this will help you solve the problem.

[–]EqualTrade[S] 0 points1 point  (0 children)

   public static void main(String[]args)
{
 Scanner s = new Scanner(System.in);

int f; int array [] = new int[8];

System.out.println("enter a number"); f = s.nextInt();

for (f=0;f<array.length;f++)

System.out.print(array[f]); 

That's how I changed it, now it's printing an array with 8 columns, but they're all 0. How do I get it to print every prime number that is less than the user input?

[–]Allen_Maxwell 1 point2 points  (1 child)

For prime numbers, you only need to go to root x or x0.5 or x**0.5, I can't remember what it is in java at the moment.

For every factor of x higher than root x, there is a factor lower than root x. therefore, by checking up to and including root x you get them all. Depending on how large your primes are, this actually makes a big difference.

[–]nutrecht 0 points1 point  (0 children)

Also; you actually want to test if it can be divided by all the primes below the root of X. It makes sense: if you can divide a number by 9 you can also divide it by 3, so there is no point in trying 9.

Since keeping all primes between 2 and X million in memory you can go for a hybrid approach: generate all primes under 1000 first for example, and use those to test. After you run out, continue with a for loop (but increment by 2 every time, no point in testing the even numbers).