2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,29 +1,24 @@
import java.util.ArrayDeque
fun hailstone(n : Int) : ArrayDeque<Int> {
fun hailstone(n: Int): ArrayDeque<Int> {
val hails = when {
n == 1 -> ArrayDeque<Int>()
n % 2 == 0 -> hailstone(n / 2)
else -> hailstone(3 * n + 1)
}
hails addFirst(n)
hails.addFirst(n)
return hails
}
fun main(args : Array<String>) {
fun main(args: Array<String>) {
val hail27 = hailstone(27)
fun showSeq(s : List<Int>) = s map {it.toString()} reduce {a, b -> a + ", " + b}
System.out.println(
"Hailstone sequence for 27 is " +
showSeq(hail27 take(3)) + " ... " + showSeq(hail27 drop(hail27.size - 3)) +
" with length ${hail27.size}."
)
fun showSeq(s: List<Int>) = s.map { it.toString() }.reduce { a, b -> a + ", " + b }
println("Hailstone sequence for 27 is " + showSeq(hail27.take(3)) + " ... "
+ showSeq(hail27.drop(hail27.size - 3)) + " with length ${hail27.size}.")
var longestHail = hailstone(1)
for (x in 1 .. 99999)
longestHail = array(hailstone(x), longestHail) maxBy {it.size} ?: longestHail
System.out.println(
"${longestHail.getFirst()} is the number less than 100000 with " +
"the longest sequence, having length ${longestHail.size}."
)
for (x in 1..99999)
longestHail = arrayOf(hailstone(x), longestHail).maxBy { it.size } ?: longestHail
println("${longestHail.first} is the number less than 100000 with " +
"the longest sequence, having length ${longestHail.size}.")
}