2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-26 06:48:04 +02:00

Cut (Prolog) hinzugefügt; weitere Scala-Beispiele hinzugefügt

This commit is contained in:
Martin Thoma 2014-09-17 15:28:17 +02:00
parent 6829f6cf97
commit 20994f6cc3
7 changed files with 101 additions and 1 deletions

View file

@ -0,0 +1,12 @@
object TwoBases {
def test(x: Int, y: Int, z: Int) =
(100*x + 10*y + z == math.pow(9,2)*z + 9*y + z)
def main(args: Array[String]) {
for(x <- 0 to 9; y <- 0 to 9; z <- 0 to 9) {
if(test(x, y, z)){
println("%d%d%d".format(x, y, z));
}
}
}
}

View file

@ -0,0 +1,34 @@
import scala.math.pow
object ExactlyAthrid {
def main(arg: Array[String]) {
val digits = List(1, 2, 3, 4, 5, 6, 7, 8, 9);
for (c <- digits.combinations(4)) {
for(d <- c.permutations) {
// Get the numerator
var numerator = 0;
for((digit, place) <- d.zipWithIndex) {
numerator += digit
* pow(10, place).toInt;
}
// Get the denominator
var denominator = 3 * numerator;
// Check if all digits appear
// exactly once
var cdigits = numerator.toString
+ denominator.toString;
var cdigits_list = cdigits.toCharArray.
distinct;
// Print solution
if (cdigits_list.length == 9 &&
!cdigits_list.contains('0')){
println("%d / %d = 1/3".
format(numerator, denominator));
}
}
}
}
}

View file

@ -0,0 +1,13 @@
object ThreeDice {
def main(arg: Array[String]) {
val dice_results = List(1, 2, 3, 4, 5, 6);
var outcomes = for(a <- dice_results;
b <- dice_results;
c <- dice_results)
yield a*b*c;
println("%d / %d".
format(
outcomes.filter(x => x % 2 == 1).length,
outcomes.length));
}
}