Stream Sieve

package Mathematics

object StreamSieve {

  private val allPrimes: LazyList[Int] = 2 #:: LazyList.from(3).filter { c =>
    val primesUpToSqrt = allPrimes.takeWhile(p => p <= math.sqrt(c.toDouble))
    !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
    */
  def getPrimeNumbers(n: Int): Seq[Int] = allPrimes.take(n)
}
Algerlogo

Β© Alger 2022

About us

We are a group of programmers helping each other build new things, whether it be writing complex encryption programs, or simple ciphers. Our goal is to work together to document and model beautiful, helpful and interesting algorithms using code. We are an open-source community - anyone can contribute. We check each other's work, communicate and collaborate to solve problems. We strive to be welcoming, respectful, yet make sure that our code follows the latest programming guidelines.