Jump to content

Hailstone sequence: Difference between revisions

(Hailstone sequence / Collact conjecture for Onyx (wasm))
Line 6,007:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">
fun hailstone(start: Int) = generateSequence(start) { n ->
if (n == 1) null else if (n % 2 == 0) n / 2 else n * 3 + 1
}
 
fun main() {
val hail27 = hailstone(27).toList()
println("The hailstone sequence for 27 has ${hail27.size} elements:\n$hail27")
val (n, length) = (1..100000).asSequence().map { it to hailstone(it).count() }.maxBy { it.second }
println("The number between 1 and 100000 with the longest hailstone sequence is $n, of length $length")
}
</syntaxhighlight>
 
 
Alternative, doing it manually:
 
<syntaxhighlight lang="kotlin">import java.util.ArrayDeque
 
47

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.