Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion maths/number_of_digits.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def num_digits_fast(n: int) -> int:
4
>>> num_digits_fast(10**15)
16
>>> num_digits_fast(10**16 - 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new doctests look good, including 1050 - 1 and the negative case. One more worth adding is 1015 - 1 (expected 15): in my check the plain floor(log10(n) + 1) formula is already wrong from that value onward, so it is the first value the correction step actually has to fix. Also 10**15 itself is covered by the existing doctest, which is a good neighbour for it.

16
>>> num_digits_fast(10**16)
17
>>> num_digits_fast(10**50 - 1)
50
>>> num_digits_fast(-(10**16 - 1))
16
>>> num_digits_fast(0)
1
>>> num_digits_fast(-1)
Expand All @@ -60,7 +68,16 @@ def num_digits_fast(n: int) -> int:
"""
if not isinstance(n, int):
raise TypeError("Input must be an integer")
return 1 if n == 0 else math.floor(math.log10(abs(n)) + 1)
if n == 0:
return 1

abs_n = abs(n)
digits = math.floor(math.log10(abs_n) + 1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correction below makes this safe: I compared the function with len(str(abs(n))) for 10k - 1, 10k and 10**k + 1 for every k up to 599, and for 200,000 random values with up to 300 digits, with no mismatches. One small thought: floor(log10(abs_n)) + 1 and floor(log10(abs_n) + 1) can differ by float rounding right before a power of ten, so the code relies entirely on the correction. A one-line comment saying so would help readers.

if abs_n < 10 ** (digits - 1):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a short comment above the correction explaining why it exists? For example: "log10 on a float can be off by one near powers of ten, since ints above 2**53 lose precision when converted to float; correct it exactly with integer comparisons." The explanation is easy to lose later, and without it the next reader may remove the check as redundant.

digits -= 1
elif abs_n >= 10**digits:
digits += 1
return digits


def num_digits_faster(n: int) -> int:
Expand Down
Loading