Skip to content

Commit

Permalink
Update Problem3.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
ditekunov authored May 26, 2018
1 parent d85c16e commit 35d4c71
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions scala/Problem3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ object Main extends App {
private def generateEratosthenesPrimesLogic(input: Stream[Int]): Stream[Int] =
input.head #:: generateEratosthenesPrimesLogic(input.tail.filter(_ % input.head != 0))

/**
* Improved algorithm, has O(n*log(n)) complexity.
*/
val maxPrime = 5000.generateEratosthenesPrimes.filter { x =>
600851475143L % x == 0
}.max
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0)*0.000000001 + "s")
result
}


print(maxPrime)
time {
/**
* Improved algorithm, has O(n*log(n)) complexity.
*/
val maxPrime = 5000.generateEratosthenesPrimes.filter { x =>
600851475143L % x == 0
}.max

print(maxPrime)
}
}

0 comments on commit 35d4c71

Please sign in to comment.