zlacker

[return to "My Favorite Programming Problem to Teach: Digit Length"]
1. tzs+ng[view] [source] 2019-11-11 01:32:57
>>jstrie+(OP)

  def digitLength(n):
      dlen = 1
      high = 9
      while n > high:
          dlen += 1
          high = 10*high + 9
      return dlen
◧◩
2. userbi+8x[view] [source] 2019-11-11 05:51:15
>>tzs+ng
When I learned computing, on the machines of the time division was very slow and multiplication was slow (early microprocessors didn't even have instructions for them), so I would certainly choose this solution over all the other ones that involve division. Modern PCs are much faster but the operations still have the same relative ranking of speeds.
[go to top]