4 ms·
You can avoid allocating so much memory by changing a::Int64 = ((a%2==0) ? a : 3*a+1)/2 to a = ((a%2 == 0) ? a : 3a+1) >> 1 or a = div((a%2 == 0) ? a
by mhath 13y ago
You can avoid allocating so much memory by changing
a::Int64 = ((a%2==0) ? a : 3*a+1)/2
to
a = ((a%2 == 0) ? a : 3a+1) >> 1
or
a = div((a%2 == 0) ? a : 3a+1, 2)
for me this gives
(329, 837799)
elapsed time: 0.449473338 seconds (544 bytes allocated)
For reference the C version gives
./a.out 1000000 0.40s user 0.00s system 99% cpu 0.407 total
- toolslive 13y agowhat about: a = ((a & 0x01)? (a*3)+1 : a)>>1; anyway, when you think about it, you can skip everything between 0 and max_a0/2 because for any value there there is one with a longer chain (its double). that's a factor 2 in all languages.
- mhath 13y agoOf course halving the range checked would also be an improvement. My own improvement was meant only to reduce the memory usage of antimora's code.