Skip to content

Instantly share code, notes, and snippets.

@mustafa-zidan
Created November 6, 2016 20:50
Show Gist options
  • Save mustafa-zidan/9075184a6f0ced5394dbf9566f093d06 to your computer and use it in GitHub Desktop.
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
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