diff --git a/README.md b/README.md
index 182d36a8d905..a964798cfca7 100644
--- a/README.md
+++ b/README.md
@@ -34,7 +34,7 @@
-
All algorithms implemented in Python - for education 📚
+ All algorithms implemented in Python - for educational purposes 📚
Implementations are for learning purposes only. They may be less efficient than the implementations in the Python standard library. Use them at your discretion.
diff --git a/maths/number_of_digits.py b/maths/number_of_digits.py
index bb9c0d248fd1..63a49b0a4ad2 100644
--- a/maths/number_of_digits.py
+++ b/maths/number_of_digits.py
@@ -1,10 +1,11 @@
import math
from timeit import timeit
+from typing import Callable
def num_digits(n: int) -> int:
"""
- Find the number of digits in a number.
+ Find the number of digits in an integer.
>>> num_digits(12345)
5
@@ -21,7 +22,6 @@ def num_digits(n: int) -> int:
...
TypeError: Input must be an integer
"""
-
if not isinstance(n, int):
raise TypeError("Input must be an integer")
@@ -37,8 +37,7 @@ def num_digits(n: int) -> int:
def num_digits_fast(n: int) -> int:
"""
- Find the number of digits in a number.
- abs() is used as logarithm for negative numbers is not defined.
+ Find the number of digits using logarithm.
>>> num_digits_fast(12345)
5
@@ -50,22 +49,19 @@ def num_digits_fast(n: int) -> int:
1
>>> num_digits_fast(-123456)
6
- >>> num_digits('123') # Raises a TypeError for non-integer input
+ >>> num_digits_fast('123') # Raises a TypeError for non-integer input
Traceback (most recent call last):
...
TypeError: Input must be an integer
"""
-
if not isinstance(n, int):
raise TypeError("Input must be an integer")
-
- return 1 if n == 0 else math.floor(math.log(abs(n), 10) + 1)
+ return 1 if n == 0 else math.floor(math.log10(abs(n)) + 1)
def num_digits_faster(n: int) -> int:
"""
- Find the number of digits in a number.
- abs() is used for negative numbers
+ Find the number of digits using string conversion.
>>> num_digits_faster(12345)
5
@@ -77,30 +73,32 @@ def num_digits_faster(n: int) -> int:
1
>>> num_digits_faster(-123456)
6
- >>> num_digits('123') # Raises a TypeError for non-integer input
+ >>> num_digits_faster('123') # Raises a TypeError for non-integer input
Traceback (most recent call last):
...
TypeError: Input must be an integer
"""
-
if not isinstance(n, int):
raise TypeError("Input must be an integer")
-
return len(str(abs(n)))
def benchmark() -> None:
"""
- Benchmark multiple functions, with three different length int values.
+ Benchmark multiple functions with three different length integers.
"""
- from collections.abc import Callable
-
def benchmark_a_function(func: Callable, value: int) -> None:
call = f"{func.__name__}({value})"
- timing = timeit(f"__main__.{call}", setup="import __main__")
- print(f"{call}: {func(value)} -- {timing} seconds")
+ timing = timeit(f"__main__.{call}", setup="import __main__", number=10000)
+ print(f"{call}: Result={func(value)}, Time={timing:.6f} sec")
- for value in (262144, 1125899906842624, 1267650600228229401496703205376):
+ test_values = [
+ 262144,
+ 1125899906842624,
+ 1267650600228229401496703205376,
+ ]
+
+ for value in test_values:
for func in (num_digits, num_digits_fast, num_digits_faster):
benchmark_a_function(func, value)
print()
@@ -111,3 +109,4 @@ def benchmark_a_function(func: Callable, value: int) -> None:
doctest.testmod()
benchmark()
+