I think my Python implementation avoided any such assumptions? viewtopic.php?p=1026459#p1026459Heater wrote:Looked at this way I would say the program as presented is buggy. It does not check for overflow of anything. It presumes you know what will happen before you start!

I would be happy to see you handle even 64 bits in JavaScript. (It uses double, getting 53 effective bits, but drops to 32 if you use any integer operators, even on 64 bit platforms.) Can you allocate more than 1GB per process yet?To be really wicked we should insist that this algorithm handle integers of arbitrary size, not just limited to 64 bits.

The only program I ever wrote in JS turned out slower than in Python. If you can make it run at all, then can you explain why changing "if (n in dict)" to "if (dict[n])" would make the whole thing twice as fast? Surely the latter version must test whether the item exists before it can test whether it zero, so it is doing

*more*work?

Code: Select all

```
var dict = {1:0, 2:1};
function func(n) {
if (n in dict) {
return dict[n];
} else if (n & 1) {
return dict[n] = 1+func(3*n+1);
} else {
return dict[n] = 1+func(n/2);
}
}
var maxsteps = 0, maxval = 0;
var x = 1;
while (x < 10000000) {
steps = func(x);
if (steps > maxsteps) {
maxsteps = steps;
maxval = x;
}
x += 1;
}
console.log("max", maxval, "steps", maxsteps);
```