Permalink
Browse files

Problem 3 updated

  • Loading branch information...
1 parent 536c136 commit 767e10b5a89a508302b875e05481b391f6c10b9e @pavelfatin committed Jul 3, 2013
Showing with 5 additions and 8 deletions.
  1. +5 −8 p03.scala
View
@@ -1,10 +1,7 @@
-lazy val ps: Stream[Int] = 2 #:: Stream.from(3).filter(i =>
- ps.takeWhile(j => j * j <= i).forall(i % _ > 0))
+def factors(n: Long): List[Long] =
+ (2 to math.sqrt(n).toInt).find(n % _ == 0)
+ .map(i => i.toLong :: factors(n / i)).getOrElse(List(n))
-val n = 600851475143L
+val r = factors(600851475143L).last
-val limit = math.sqrt(n)
-
-val r = ps.view.takeWhile(_ < limit).filter(n % _ == 0).last
-
-assert(r == 6857) // 265 ms
+assert(r == 6857) // 1 ms

0 comments on commit 767e10b

Please sign in to comment.