0

I am using Scala Stream to find out prime numbers between two numbers, but it is throwing Java OutofMemoryError. Can someone tell me why is it happening so?

def sieve(nums: Stream[Int]): Stream[Int] = {
  nums.head #:: sieve(nums.tail filter (_ % nums.head != 0))
}  

def listPrimesinRange(start: Int, end: Int): List[Int] = {
  sieve(Stream.from(2)).filter(x => (x >= start && x <= end)).toList
}
Lkopo
  • 4,798
  • 8
  • 35
  • 60
pradeepchhetri
  • 2,899
  • 6
  • 28
  • 50
  • 2
    possible duplicate of [Why is this scala prime generation so slow/memory intensive?](http://stackoverflow.com/questions/6802112/why-is-this-scala-prime-generation-so-slow-memory-intensive) – 4e6 Sep 13 '14 at 18:25
  • see [this comment](http://stackoverflow.com/questions/20985539/scala-erastothenes-is-there-a-straightforward-way-to-replace-a-stream-with-an/20991776#comment31606932_20986428). – Will Ness Sep 13 '14 at 18:57

0 Answers0