// Prints all prime numbers up to the given max.
public static void printPrimes(int max) {
    if (max >= 2) {
        System.out.print("2");
        for (int i = 3; i <= max; i++) {
          if (isPrime(i)) {
            System.out.print(", " + i);
          }
        }
        System.out.println();
    }
}

/* returns true if x is prime or false otherwise */
public static boolean isPrime(int x) {
  return countFactors(x) == 2;
}

// Returns how many factors the given number has.
public static int countFactors(int number) {
    int count = 0;
    for (int i = 1; i <= number; i++) {
      if (divisible(number, i)) {
        count++;   // i is a factor of number
      }
    }
    return count;
}

/* returns true if x is divisible by y or false otherwise */
public static boolean divisible(int x, int y) {
  return x % y == 0;
}
