2019-09-12 10:33:56 -07:00
|
|
|
import system'collections;
|
|
|
|
|
import extensions;
|
2018-06-22 20:57:24 +00:00
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
const int maxNumber = 100000;
|
2018-06-22 20:57:24 +00:00
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
Hailstone(int n,Map<int,int> lengths)
|
|
|
|
|
{
|
2018-06-22 20:57:24 +00:00
|
|
|
if (n == 1)
|
2019-09-12 10:33:56 -07:00
|
|
|
{
|
2018-06-22 20:57:24 +00:00
|
|
|
^ 1
|
2019-09-12 10:33:56 -07:00
|
|
|
};
|
2018-06-22 20:57:24 +00:00
|
|
|
|
|
|
|
|
while (true)
|
2019-09-12 10:33:56 -07:00
|
|
|
{
|
|
|
|
|
if (lengths.containsKey(n))
|
|
|
|
|
{
|
2018-06-22 20:57:24 +00:00
|
|
|
^ lengths[n]
|
2019-09-12 10:33:56 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (n.isEven())
|
|
|
|
|
{
|
2018-06-22 20:57:24 +00:00
|
|
|
lengths[n] := 1 + Hailstone(n/2, lengths)
|
2019-09-12 10:33:56 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lengths[n] := 1 + Hailstone(3*n + 1, lengths)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-22 20:57:24 +00:00
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
public program()
|
|
|
|
|
{
|
|
|
|
|
int longestChain := 0;
|
|
|
|
|
int longestNumber := 0;
|
|
|
|
|
auto recursiveLengths := new Map<int,int>(4096,4096);
|
2018-06-22 20:57:24 +00:00
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
for(int i := 1, i < maxNumber, i+=1)
|
|
|
|
|
{
|
|
|
|
|
var chainLength := Hailstone(i, recursiveLengths);
|
2018-06-22 20:57:24 +00:00
|
|
|
if (longestChain < chainLength)
|
2019-09-12 10:33:56 -07:00
|
|
|
{
|
|
|
|
|
longestChain := chainLength;
|
|
|
|
|
longestNumber := i
|
|
|
|
|
}
|
|
|
|
|
};
|
2018-06-22 20:57:24 +00:00
|
|
|
|
2019-09-12 10:33:56 -07:00
|
|
|
console.printFormatted("max below {0}: {1} ({2} steps)", maxNumber, longestNumber, longestChain)
|
|
|
|
|
}
|