zlacker

[parent] [thread] 5 comments
1. slavik+(OP)[view] [source] 2019-11-11 10:15:20
I wish they didn't phrase it as being about removing a loop. If math.log10 supported arbitrary integer inputs, there would be a loop inside.

They should consider the upper bound of their algorithm. Because it uses floating point, it doesn't work for all natural numbers, as required by their specification.

To address your point, counting digits in base 2 is much simpler than in base 10, because the internal representation of the number on the computer is already base 2. You can use numeric calculations, but you can also just look at your digits.

(Edited for fewer tangents and to be more positive.)

replies(1): >>gpdere+uq
2. gpdere+uq[view] [source] 2019-11-11 14:42:23
>>slavik+(OP)
Once you compute your log2, computing log10 is just a single multiplication though, so, it is in fact as most as easy.
replies(1): >>Mauran+LA
◧◩
3. Mauran+LA[view] [source] [discussion] 2019-11-11 15:56:05
>>gpdere+uq
It's a single multiplication - with an irrational (transcendental, actually) number. That is most certainly not "easy" for a computer, and elsewhere in the comments you can see how much of a problem that actually poses.

Also, counting digits in base 2 is not the log2. The former gives you the latter but not the reverse. Finding the number of decimal digits in a number given in base 2 is not a simplification.

replies(1): >>gpdere+bJ
◧◩◪
4. gpdere+bJ[view] [source] [discussion] 2019-11-11 16:50:41
>>Mauran+LA
Oh, agree that for arbitrary large or non integer inputs it might not work. But elsethread jepler has shown a solution off Hacker's Delight which which simply multiplies the index of the most significant bit with a magic constant.
replies(2): >>Mauran+Wa1 >>slavik+Wc1
◧◩◪◨
5. Mauran+Wa1[view] [source] [discussion] 2019-11-11 19:32:33
>>gpdere+bJ
> But elsethread jepler has shown a solution off Hacker's Delight which which simply multiplies the index of the most significant bit with a magic constant.

...and then checks for and corrects the potential off-by-one thus incurred.

◧◩◪◨
6. slavik+Wc1[view] [source] [discussion] 2019-11-11 19:46:47
>>gpdere+bJ
When N is fixed, all algorithms become O(1). That's why they appear the same.

If you do these calculations by hand, the complexity will be more obvious because each operation will be smaller.

[go to top]