In mathematics, a prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. The first twenty-five prime numbers are:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97

An infinitude of prime numbers exists, as demonstrated by Euclid around 300 BC, though the density of prime numbers within natural numbers is 0. The number 1 is by definition not a prime number. The fundamental theorem of arithmetic establishes the central role of primes in number theory: any nonzero natural number n can be factored into primes, written as a product of primes or powers of different primes (including the empty product of factors for 1). Moreover, this factorization is unique except for a possible reordering of the factors.

## <!Prime Number Generator Program

The following program shows how to generate Prime Numbers up to a given number. It also prints how many prime numbers are being generated up to that given number.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.livrona.snippets.numbers; /** * A simple snippet that generates and prints the prime number up to a given max. * / public class PrimeNumberGenerator { /** * Test Method * * @param args */ public static void main(String[] args) { if (args.length != 1) { System.out .println("Usage: java com.livrona.snippets.numbers.PrimeNumberGenerator <Max Number>"); System.exit(0); } // store args String maxPrimeNo = args[0]; System.out.println("Generate Prime Numbers up to : " + maxPrimeNo); // invoke the method try { generatePrimes(new Integer(maxPrimeNo).intValue()); } catch (Exception e) { System.out.println("Failed to generate the Fabonacci: " + e.getMessage()); e.printStackTrace(); } } /** * Generate the Prime no, up to a max number. * @param maxNum */ private static void generatePrimes(int maxNum) { // http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes // initially assume all integers are prime boolean[] isPrime = new boolean[maxNum + 1]; for (int i = 2; i <= maxNum; i++) isPrime[i] = true; // mark non-primes <= N using Sieve of Eratosthenes for (int i = 2; i * i <= Math.sqrt(maxNum); i++) { // if i is prime, then mark multiples of i as nonprime if (isPrime[i]) { for (int j = i; i * j <= maxNum; j++) isPrime[i * j] = false; } } // count primes int primes = 0; for (int i = 2; i <= maxNum; i++) if (isPrime[i]) { System.out.println("Prime - " + i); primes++; } System.out.println("The number of primes <= " + maxNum + " is " + primes); } } |

## Program Output

Generate Prime Numbers up to : 100

Prime – 2

Prime – 3

Prime – 5

Prime – 7

Prime – 11

Prime – 13

Prime – 17

Prime – 19

Prime – 23

Prime – 25

Prime – 29

Prime – 31

Prime – 35

Prime – 37

Prime – 41

Prime – 43

Prime – 47

Prime – 49

Prime – 53

Prime – 55

Prime – 59

Prime – 61

Prime – 65

Prime – 67

Prime – 71

Prime – 73

Prime – 77

Prime – 79

Prime – 83

Prime – 85

Prime – 89

Prime – 91

Prime – 95

Prime – 97

The number of primes <= 100 is 34

[pb_builder]