/* Prime100 written by Keith Fenske Monday, 22 April 2002 Copyright (c) 2002 by Keith Fenske. All rights reserved. This is a simple Java application to compute all prime numbers less than 100 using the algorithm on page 22 of "Discrete Mathematical Structures". The algorithm is not smart because it tests all numbers from 2 to 99, and all possible odd divisors less than the square root of n. */ public class Prime100 { public static void main(String[] args) { boolean flag; // true if n is prime int d; // divisor to test int k; // largest integer less than square root int n; // number we are checking if prime System.out.println("Prime numbers less than 100:"); for (n = 2; n < 100; n ++) // try all numbers from 2 to 99 { if (n == 2) flag = true; // 2 is prime else if ((n % 2) == 0) flag = false; // even numbers are not prime else { flag = true; // assume n is prime k = (int) Math.sqrt((double) n); for (d = 3; d <= k; d = d + 2) { if ((n % d) == 0) // does d divide n? flag = false; // yes, n is not prime } } if (flag) // is this number prime? System.out.print(n + " "); } } }