Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132944 views
License: OTHER
1
import scala.math.pow
2
3
object ExactlyAthrid {
4
def main(arg: Array[String]) {
5
val digits = List(1, 2, 3, 4, 5, 6, 7, 8, 9);
6
for (c <- digits.combinations(4)) {
7
for(d <- c.permutations) {
8
// Get the numerator
9
var numerator = 0;
10
for((digit, place) <- d.zipWithIndex) {
11
numerator += digit
12
* pow(10, place).toInt;
13
}
14
15
// Get the denominator
16
var denominator = 3 * numerator;
17
18
// Check if all digits appear
19
// exactly once
20
var cdigits = numerator.toString
21
+ denominator.toString;
22
var cdigits_list = cdigits.toCharArray.
23
distinct;
24
25
// Print solution
26
if (cdigits_list.length == 9 &&
27
!cdigits_list.contains('0')){
28
println("%d / %d = 1/3".
29
format(numerator, denominator));
30
}
31
}
32
}
33
}
34
}
35