Skip to content

Instantly share code, notes, and snippets.

@yanns
Created January 6, 2016 22:10
Show Gist options
  • Save yanns/620f74981b28b22d682e to your computer and use it in GitHub Desktop.
Save yanns/620f74981b28b22d682e to your computer and use it in GitHub Desktop.
case class Pyramide(a1: Int, a2: Int, a3: Int, a4: Int) {
val sum11 = a1 + a2
val sum12 = a2 + a3
val sum13 = a3 + a4
val sum21 = sum11 + sum12
val sum22 = sum12 + sum13
val sum = sum21 + sum22
override def toString: String =
s"""
> | $sum |
> | $sum21 | $sum22 |
> | $sum11 | $sum12 | $sum13 |
>| $a1 | $a2 | $a3 | $a4 |""".stripMargin('>')
}
val allPyramides = List(75, 134, 182, 215).permutations.map(l ⇒ Pyramide(l(0), l(1), l(2), l(3))).toList
println("all pyramides:\n" + allPyramides.mkString("\n"))
println(allPyramides.size + " possible pyramides")
println(allPyramides.groupBy(_.sum).keys.size + " different sum at the end")
for (depart ← 1 to 250) {
val pyramides = List(depart, depart + 1, depart + 2, depart + 3).permutations.map(l ⇒ Pyramide(l(0), l(1), l(2), l(3))).toList
pyramides.filter(_.sum == 1000).foreach(println)
}
for (sum ← 1000 to 1100 ; depart ← 1 to 250) {
val pyramide = Pyramide(depart, depart + 1, depart + 2, depart + 3)
if (pyramide.sum == sum) println(pyramide)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment