/* * "Brute force" approach to tabulate pi(N) using nested for-loops * Pi(N) = the number of prime numbers less than or equal to N */ import java.util.Scanner; class Primes { public static void main (String[] args) { Scanner scan = new Scanner(System.in); int n, i, limit, step, pi = 0; boolean prime; System.out.print ("Enter the limit: "); limit = scan.nextInt(); System.out.print ("Enter the step: "); step = scan.nextInt(); for (n=2; n<=limit; n++) { prime = true; for (i=2; (i<=n/3) & prime; i++) if (n % i == 0) prime = false; if (prime) pi++; if (n % step == 0) System.out.println(n + "\t" + pi); } } }