-
Notifications
You must be signed in to change notification settings - Fork 5
/
ScalaEuler.scala
executable file
·100 lines (87 loc) · 3.13 KB
/
ScalaEuler.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import org.apfloat.Apint
import org.apfloat.ApintMath
object ScalaEuler {
// Really? not built in?
def inject(arr: Collection[Int], initial: Int, operation: (Int, Int) => Int) : Int = {
var carryOver = initial
arr.foreach(element => carryOver = operation(carryOver, element) )
carryOver
}
// Really? not built in?
def sum(list:Collection[Int]):Int = inject(list, 0, (carryOver, elem) => carryOver + elem)
/**
* Euler #1
* Answer: 233168
*
* If we list all the natural numbers below 10 that are multiples of 3 or 5,
* we get 3, 5, 6 and 9. The sum of these multiples is 23.
*
* Find the sum of all the multiples of 3 or 5 below 1000.
*/
def euler1():Int = sum((3 until 1000).map(n => if (n % 3 == 0 || n % 5 == 0) n else 0))
/**
* Euler #2
* Answer: 4613732
*
* Each new term in the Fibonacci sequence is generated by adding the previous
* two terms. By starting with 1 and 2, the first 10 terms will be:
*
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
*
* Find the sum of all the even-valued terms in the sequence which do not
* exceed four million.
*/
def next_fib(a:Int, b:Int):Int = a + b
def e2search(a:Int, b:Int, sum:Int):Int = if (b > 4000000) sum else e2search(b, next_fib(a, b), if (b % 2 == 0) sum + b else sum)
def euler2():Int = e2search(1, 2, 0)
/**
* Euler #3:
* Answer: 6857
*
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* What is the largest prime factor of the number 600851475143 ?
*/
def is_prime(n: Int):Boolean = {
if (n != 2) {
val maxFactor = Math.ceil(Math.sqrt(n)).toInt
var start:Double = 2
for (i <- 2 to maxFactor) {
if (n % i == 0) {
return false
}
}
}
true
}
def euler3():Int = {
val TARGET = new Apint("600851475143")
val APZERO = new Apint(0)
val xs = ApintMath.sqrt(TARGET)
val (integer_portion, fractional_portion) = (xs(0), xs(1)) // There's gotta be a way to combine this with the line above in one shot
val maxFactor = if (fractional_portion.compareTo(APZERO) > 0) integer_portion.add(new Apint("1")) else integer_portion
for (i <- maxFactor.intValue() to 2) {
if (TARGET.mod(new Apint(i)).compareTo(APZERO) == 0 && is_prime(i)) {
return i
}
}
-1
}
/** "Main" stuff **/
val eulers = List(euler1, euler2, euler3)
def show_results(prob_number:Int, result:Int):Unit = println("#" + prob_number + ": " + result)
def main(args: Array[String]) = {
if (args.length > 0) {
for (arg <- args) {
val problem_number = Integer.parseInt(arg)
val euler = eulers(problem_number-1)
show_results(problem_number, euler)
}
} else {
for ((euler, index) <- eulers.zipWithIndex) {
val problem_number = index + 1
show_results(problem_number, euler)
}
}
}
}