コラッツ・角谷の問題
Posted feedbacks - D
f(837799) = 524
Processor: Athlon X2 3800+ / Memory: 1GB
Compiler: Digital Mars D Compiler v2.009
Process Time: 0:00:00.125
Processor: Athlon X2 3800+ / Memory: 1GB
Compiler: Digital Mars D Compiler v2.009
Process Time: 0:00:00.125
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import std.stdio;
void main() {
uint i_max, x_max;
foreach(i; 1 .. (1 << 20) + 1) {
uint x = f(i);
if(x_max < x) {
x_max = x;
i_max = i;
}
}
writefln("f(%s) = %s", i_max, x_max);
}
uint f(const ulong n) {
static uint[1 << 20] cache;
if(n == 1) return 0;
if(n < cache.length && cache[n])
return cache[n];
auto steps = (n % 2 == 0 ? f(n / 2) : f(n * 3 + 1)) + 1;
if(n < cache.length) cache[n] = steps;
return steps;
}
unittest {
assert(f(9) == 19);
}
|


ところてん
#4969()
Rating2/2=1.00
see: コラッツの問題の成り立つ範囲
[ reply ]