From 93d31b6ae4a78763b8fded7fcd93d4b20a1965cd Mon Sep 17 00:00:00 2001
From: Abhiranjan Kumar <2400030581@kluniversity.in>
Date: Mon, 3 Nov 2025 12:38:30 +0530
Subject: [PATCH 1/2] corrected grammar in README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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.
From 9716b36e69b44f42d5fadb0fcb70faf5c3c1c883 Mon Sep 17 00:00:00 2001
From: ABHIRANJAN-KUMAR1
Date: Thu, 6 Nov 2025 19:50:26 +0530
Subject: [PATCH 2/2] Fix num_digits functions: correct doctests and improve
benchmark
---
maths/number_of_digits.py | 37 ++++++++++++++++++-------------------
1 file changed, 18 insertions(+), 19 deletions(-)
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()
+