-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem3.scala
58 lines (49 loc) · 1.43 KB
/
Problem3.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
object Main extends App {
/**
* Basic algo with O(N) complexity
*/
def factors(n:Long):List[Long] = {
def divides(d:Long, n:Long) = (n % d) == 0
def ld(n:Long):Long = ldf(2, n)
def ldf(k:Long, n:Long):Long = {
if (divides(k, n)) k
else if ((k*k) > n) n
else ldf((k+1), n)
}
n match {
case 1 => Nil
case _ => val p = ld(n); p :: factors(n / p)
}
}
factors(600851475143L).max
println(factors(600851475143L).max)
/**
* Generates prime numbers using Sieve of Eratosthenes.
*/
def generateEratosthenesPrimes: List[Int] =
Try(generateEratosthenesPrimesLogic(Stream.from(2)).take(til)) match {
case Success(something) => something.toList
case Failure(ex) => throw new InputException(ex.toString)
}
/**
* Sub-function for generateEratosthenesPrimes
*/
private def generateEratosthenesPrimesLogic(input: Stream[Int]): Stream[Int] =
input.head #:: generateEratosthenesPrimesLogic(input.tail.filter(_ % input.head != 0))
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
}
time {
/**
* Improved algorithm, has O(n*log(n)) complexity.
*/
val maxPrime = 5000.generateEratosthenesPrimes.filter { x =>
600851475143L % x == 0
}.max
print(maxPrime)
}
}