Thursday, March 12, 2015

Problem 3 in Scala

/**
  * Largest prime factor: https://projecteuler.net/problem=3
  *
  * The prime factors of 13195 are 5, 7, 13 and 29.
  *
  * What is the largest prime factor of the number 600851475143?
  */

import scala.math._

def isPrime(n: Int) = (2 to math.sqrt(n).toInt).forall(n%_ != 0)

val n = 600851475143L
val answer = 1.to(sqrt(n).toInt).filter(isPrime).filter(n % _ == 0).sortWith(_ > _).head

No comments:

Post a Comment