Created
November 6, 2016 20:50
-
-
Save mustafa-zidan/9075184a6f0ced5394dbf9566f093d06 to your computer and use it in GitHub Desktop.
problem #3 in projecteuler to find the largest prime factor using sieve segmentation and scala streams
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
def sieve(s: Stream[Long]): Stream[Long] = s.head #:: sieve(s.tail filter (_ % s.head != 0)) | |
def from(n: Long): Stream[Long] = n #:: from(n + 1) | |
def primes = sieve(from(2)) | |
def largest(x: Long, p: Stream[Long]):Long = { | |
if (x <= p.head) x | |
else if (x % p.head == 0) largest(x / p.head, p) | |
else largest(x,p.tail) | |
} | |
largest(600851475143L, primes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment