|
|
|
|
|
by ColinWright
4783 days ago
|
|
I'm confused by this. Both the algorithms mentioned seem to give the wrong result when N is a power of 2. For example, N=4. Set all the lower bits to 1, giving 7. Increment, giving 8, return 8. But the power of two no less than 4 is 4. Am I missing something? Also, the alleged faster solution is only faster for larger numbers (on a log scale). Sure, there are more of them, but what if your function is called more often for small numbers than large ones? |
|
The real problem is that we're doing all of this in the context of primitive integers, not lists of integers making up a BigInt. Moufid might be on the right track for optimizing for such lists; rather than:
which performs 3 operations which involve each element of the list of the BigInt, we're replacing it with: This passes through the list once if the list is a power of two, and somewhere between once and twice if the list is not.