Re:My Favorite Programming Problem to Teach: Digit Length
by gpderetta on Monday November 11, 2019 @09:42AM
(#21505170)
Once you compute your log2, computing log10 is just a single multiplication though, so, it is in fact as most as easy.
Re:My Favorite Programming Problem to Teach: Digit Length
by MauranKilom on Monday November 11, 2019 @10:56AM
(#21505807)
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.
Re:My Favorite Programming Problem to Teach: Digit Length
by gpderetta on Monday November 11, 2019 @11:50AM
(#21506329)
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.
Re:My Favorite Programming Problem to Teach: Digit Length
by MauranKilom on Monday November 11, 2019 @02:32PM
(#21508050)
> 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.
Re:My Favorite Programming Problem to Teach: Digit Length
by slavik81 on Monday November 11, 2019 @02:46PM
(#21508174)
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.
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.)