Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
50
Task/Hailstone-sequence/Elena/hailstone-sequence.elena
Normal file
50
Task/Hailstone-sequence/Elena/hailstone-sequence.elena
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import system'collections;
|
||||
import extensions;
|
||||
|
||||
const int maxNumber = 100000;
|
||||
|
||||
Hailstone(int n,Map<int,int> lengths)
|
||||
{
|
||||
if (n == 1)
|
||||
{
|
||||
^ 1
|
||||
};
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (lengths.containsKey(n))
|
||||
{
|
||||
^ lengths[n]
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n.isEven())
|
||||
{
|
||||
lengths[n] := 1 + Hailstone(n/2, lengths)
|
||||
}
|
||||
else
|
||||
{
|
||||
lengths[n] := 1 + Hailstone(3*n + 1, lengths)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
int longestChain := 0;
|
||||
int longestNumber := 0;
|
||||
auto recursiveLengths := new Map<int,int>(4096,4096);
|
||||
|
||||
for(int i := 1, i < maxNumber, i+=1)
|
||||
{
|
||||
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