Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132937 views
License: OTHER
1
object HighProduct {
2
def main(arg: Array[String]) {
3
var max = List(BigInt(0), BigInt(0),
4
BigInt(0));
5
val digits = List(0, 1, 2, 3, 4, 5, 6,
6
7, 8, 9);
7
for (c <- digits.combinations(4)) {
8
// Sort the digits so that the
9
// highest number gets built
10
val a = c.sorted(Ordering[Int]);
11
val b = (digits filterNot a.contains).
12
sorted(Ordering[Int]);
13
// calculate number a
14
var anum = BigInt(0);
15
for ((digit, place) <- a.zipWithIndex) {
16
anum += digit *
17
scala.math.pow(10, place).
18
toInt;
19
}
20
// calculate number b
21
var bnum = BigInt(0);
22
for ((digit, place) <- b.zipWithIndex) {
23
bnum += digit *
24
scala.math.pow(10, place).
25
toInt;
26
}
27
28
// calculate number a
29
if (anum * bnum > max(0)) {
30
max = List(anum * bnum, anum, bnum);
31
}
32
}
33
println("%d • %d = %d".format(max(1),
34
max(2),
35
max(0)));
36
}
37
}
38