Stream Sieve Algorithm
As a mixture of molecules migrate through the stationary bed of porous, semi-solid substance referred to as a sieve (or matrix), the components of highest molecular weight (which are unable to pass into the molecular pores) leave the bed first, followed by successively smaller molecules. According to IUPAC notation, microporous materials have pore diameters of less than 2 nm (20 Å) and macroporous materials have pore diameters of greater than 50 nm (500 Å); the mesoporous category thus lies in the center with pore diameters between 2 and 50 nm (20–500 Å).
package Mathematics
object StreamSieve {
private val allPrimes: Stream[Int] = 2 #:: Stream.from(3).filter { c =>
val primesUptoSqrt = allPrimes.takeWhile(p => p <= math.sqrt(c))
!primesUptoSqrt.exists(p => c % p == 0)
}
/**
* Method to use the allPrimes stream to take the first n prime numbers
* Using streams is both an easy and efficient way to generate fibonacci numbers (streams are memoized)
* Using streams as opposed to the classic sieve ensures that we stay following functional principles
* and not change states
* @param total
* @return
*/
def getPrimeNumbers(n: Int): Seq[Int] = allPrimes.take(n)
}