/** * Listing prime numbers * Using a method to check if the number is prime * * @author gtowell * Created Oct 2023 */ public class Primer { public static void main(String[] args) { for (int i = 0; i < 110; i++) { if (isPrime(i)) { System.out.println(i + " is prime"); } } } /* * A function to determine is a number is prime without. * @return true if the number is prime, false, otherwise * @param number -- the number to check for primeness. */ public static boolean isPrime(int number) { boolean rtn = true; for (int j = 2; j < Math.sqrt(number); j++) { if (number % j == 0) { rtn = false; } } return rtn; } /* * A function to determine is a number is prime without. * This version is slightly faster than the other, but per * Knuth, the difference does not matter. * @return true if the number is prime, false, otherwise * @param number -- the number to check for primeness. */ public static boolean isPrimeB(int number) { int maxx = (int)Math.sqrt(number); for (int j = 2; j < maxx; j++) { if (number % j == 0) { return false; } } return true; } }