Algorithm Implementation/Mathematics/Prime number generation

Haskell
For each number, check whether it is divisible by any of the primes not above its square root. This is an optimal trial division algorithm: Note that this code utilizes Haskell's lazy evaluation, to allow  and   to be defined in terms of each other.

The Sieve of Erastosthenes has better theoretical complexity than trial division: Were a  operation to have a complexity of $$O(|b|)$$, as it indeed is so on mutable arrays in imperative languages, this code would achieve the theoretical complexity of the S. of E. Unfortunately it is $$O(|a \cup b|)$$ with linked lists.

Recast as unbounded definition, yet faster and with better complexity due to the tree-shaped folding: This replaces the linear left-leaning nested chain of  operations in   function, with an unbounded right-deepening tree of progressively growing balanced trees of   s. A sizable constant-factor speedup can be further achieved with wheel factorization. The utility functions used above (also found in  package's   module) are:

The removal of composites is easy with arrays. Processing is naturally broken into segments between squares of consecutive primes:

Java
simple naive algorithm

Sieve of Erastosthenes, from here

Phix
You can get exactly the same output (the first 168 primes) from

Ruby
Note that Ruby has a built-in Sieve of Erastosthenes prime number generator:

C (Fast bitwise)
Fast implementation of bitwise Sieve of Eratosthenes. Little bit cheating, because it's counts only odd primes (read as: you need to add first prime - "2" manually).

There is also OpenMP #pragma's along the way, so you can try compile it with -fopenmp