June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
48
Task/Hailstone-sequence/Elena/hailstone-sequence.elena
Normal file
48
Task/Hailstone-sequence/Elena/hailstone-sequence.elena
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import system'collections.
|
||||
import extensions.
|
||||
|
||||
int const maxNumber = 100000.
|
||||
|
||||
Hailstone = (:n:lengths)
|
||||
[
|
||||
if (n == 1)
|
||||
[
|
||||
^ 1
|
||||
].
|
||||
|
||||
while (true)
|
||||
[
|
||||
if (lengths containsKey(n))
|
||||
[
|
||||
^ lengths[n]
|
||||
];
|
||||
[
|
||||
if (n int; isEven)
|
||||
[
|
||||
lengths[n] := 1 + Hailstone(n/2, lengths)
|
||||
];
|
||||
[
|
||||
lengths[n] := 1 + Hailstone((3*n) + 1, lengths)
|
||||
]
|
||||
]
|
||||
]
|
||||
].
|
||||
|
||||
program =
|
||||
[
|
||||
int longestChain := 0.
|
||||
int longestNumber := 0.
|
||||
var recursiveLengths := Dictionary new.
|
||||
|
||||
1 till(maxNumber) do(:i)<int>
|
||||
[
|
||||
var chainLength := Hailstone(i, recursiveLengths).
|
||||
if (longestChain < chainLength)
|
||||
[
|
||||
longestChain := chainLength.
|
||||
longestNumber := i.
|
||||
]
|
||||
].
|
||||
|
||||
console printFormatted("max below {0}: {1} ({2} steps)", maxNumber, longestNumber, longestChain).
|
||||
].
|
||||
Loading…
Add table
Add a link
Reference in a new issue