Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Hailstone-sequence/Scala/hailstone-sequence.scala
Normal file
15
Task/Hailstone-sequence/Scala/hailstone-sequence.scala
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
object HailstoneSequence extends App {
|
||||
def hailstone(n: Int): Stream[Int] =
|
||||
n #:: (if (n == 1) Stream.empty else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1))
|
||||
|
||||
val nr = args.headOption.map(_.toInt).getOrElse(27)
|
||||
val collatz = hailstone(nr)
|
||||
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
|
||||
println(collatz.toList)
|
||||
println(s"It has ${collatz.length} elements.")
|
||||
println
|
||||
println(
|
||||
"Compute the number < 100,000, which has the longest hailstone sequence with that sequence's length.")
|
||||
val (n, len) = (1 until 100000).map(n => (n, hailstone(n).length)).maxBy(_._2)
|
||||
println(s"Longest hailstone sequence length= $len occurring with number $n.")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue