Last active
April 16, 2016 14:37
-
-
Save kijuky/8e0fa42670f9aae4301d968157f8c483 to your computer and use it in GitHub Desktop.
エラトステネスの篩を Scala で実装してみました。どうしてもメモリ関連のエラーが取れないので、有識者の方コメントいただけると幸いです。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def primes(s: Stream[Int] = Stream.from(2)): Stream[Int] = s.head #:: primes(s.tail.filter(_ % s.head != 0)) | |
primes: (s: Stream[Int])Stream[Int] | |
scala> primes().foreach(println) | |
2 | |
3 | |
5 | |
...省略... | |
33827 | |
33829 | |
java.lang.OutOfMemoryError: GC overhead limit exceeded |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> def sieve(p: Int)(x: Int) = x % p != 0 | |
sieve: (p: Int)(x: Int)Boolean | |
scala> def primes(s: Stream[Int] = Stream.from(2)): Stream[Int] = s.head #:: primes(s.tail.filter(sieve(s.head))) | |
primes: (s: Stream[Int])Stream[Int] | |
scala> primes().foreach(println) | |
2 | |
3 | |
5 | |
...省略... | |
160813 | |
160817 | |
java.lang.StackOverflowError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment