Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 34294b5

Browse files
realDuYuanChaogithub-actionscclauss
authoredAug 16, 2020
Update sum_of_digits.py (TheAlgorithms#2319)
* * support negative number * add different version * fixup! Format Python code with psf/black push * sum(int(c) for c in str(abs(n))) * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 7e4176c commit 34294b5

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed
 

‎DIRECTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@
297297
* [Power Iteration](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/power_iteration.py)
298298
* [Rayleigh Quotient](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/rayleigh_quotient.py)
299299
* [Test Linear Algebra](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/test_linear_algebra.py)
300+
* [Transformations 2D](https://github.com/TheAlgorithms/Python/blob/master/linear_algebra/src/transformations_2d.py)
300301

301302
## Machine Learning
302303
* [Astar](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/astar.py)
@@ -580,6 +581,10 @@
580581
* [Sol32](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_32/sol32.py)
581582
* Problem 33
582583
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_33/sol1.py)
584+
* Problem 34
585+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_34/sol1.py)
586+
* Problem 35
587+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_35/sol1.py)
583588
* Problem 36
584589
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_36/sol1.py)
585590
* Problem 40

‎maths/sum_of_digits.py

+132-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from timeit import timeit
2+
3+
14
def sum_of_digits(n: int) -> int:
25
"""
36
Find the sum of digits of a number.
@@ -6,13 +9,141 @@ def sum_of_digits(n: int) -> int:
69
15
710
>>> sum_of_digits(123)
811
6
12+
>>> sum_of_digits(-123)
13+
6
14+
>>> sum_of_digits(0)
15+
0
916
"""
17+
n = -n if n < 0 else n
1018
res = 0
1119
while n > 0:
1220
res += n % 10
1321
n = n // 10
1422
return res
1523

1624

25+
def sum_of_digits_recursion(n: int) -> int:
26+
"""
27+
Find the sum of digits of a number using recursion
28+
29+
>>> sum_of_digits_recursion(12345)
30+
15
31+
>>> sum_of_digits_recursion(123)
32+
6
33+
>>> sum_of_digits_recursion(-123)
34+
6
35+
>>> sum_of_digits_recursion(0)
36+
0
37+
"""
38+
n = -n if n < 0 else n
39+
return n if n < 10 else n % 10 + sum_of_digits(n // 10)
40+
41+
42+
def sum_of_digits_compact(n: int) -> int:
43+
"""
44+
Find the sum of digits of a number
45+
46+
>>> sum_of_digits_compact(12345)
47+
15
48+
>>> sum_of_digits_compact(123)
49+
6
50+
>>> sum_of_digits_compact(-123)
51+
6
52+
>>> sum_of_digits_compact(0)
53+
0
54+
"""
55+
return sum(int(c) for c in str(abs(n)))
56+
57+
58+
def benchmark() -> None:
59+
"""
60+
Benchmark code for comparing 3 functions,
61+
with 3 different length int values.
62+
"""
63+
print("\nFor small_num = ", small_num, ":")
64+
print(
65+
"> sum_of_digits()",
66+
"\t\tans =",
67+
sum_of_digits(small_num),
68+
"\ttime =",
69+
timeit("z.sum_of_digits(z.small_num)", setup="import __main__ as z"),
70+
"seconds",
71+
)
72+
print(
73+
"> sum_of_digits_recursion()",
74+
"\tans =",
75+
sum_of_digits_recursion(small_num),
76+
"\ttime =",
77+
timeit("z.sum_of_digits_recursion(z.small_num)", setup="import __main__ as z"),
78+
"seconds",
79+
)
80+
print(
81+
"> sum_of_digits_compact()",
82+
"\tans =",
83+
sum_of_digits_compact(small_num),
84+
"\ttime =",
85+
timeit("z.sum_of_digits_compact(z.small_num)", setup="import __main__ as z"),
86+
"seconds",
87+
)
88+
89+
print("\nFor medium_num = ", medium_num, ":")
90+
print(
91+
"> sum_of_digits()",
92+
"\t\tans =",
93+
sum_of_digits(medium_num),
94+
"\ttime =",
95+
timeit("z.sum_of_digits(z.medium_num)", setup="import __main__ as z"),
96+
"seconds",
97+
)
98+
print(
99+
"> sum_of_digits_recursion()",
100+
"\tans =",
101+
sum_of_digits_recursion(medium_num),
102+
"\ttime =",
103+
timeit("z.sum_of_digits_recursion(z.medium_num)", setup="import __main__ as z"),
104+
"seconds",
105+
)
106+
print(
107+
"> sum_of_digits_compact()",
108+
"\tans =",
109+
sum_of_digits_compact(medium_num),
110+
"\ttime =",
111+
timeit("z.sum_of_digits_compact(z.medium_num)", setup="import __main__ as z"),
112+
"seconds",
113+
)
114+
115+
print("\nFor large_num = ", large_num, ":")
116+
print(
117+
"> sum_of_digits()",
118+
"\t\tans =",
119+
sum_of_digits(large_num),
120+
"\ttime =",
121+
timeit("z.sum_of_digits(z.large_num)", setup="import __main__ as z"),
122+
"seconds",
123+
)
124+
print(
125+
"> sum_of_digits_recursion()",
126+
"\tans =",
127+
sum_of_digits_recursion(large_num),
128+
"\ttime =",
129+
timeit("z.sum_of_digits_recursion(z.large_num)", setup="import __main__ as z"),
130+
"seconds",
131+
)
132+
print(
133+
"> sum_of_digits_compact()",
134+
"\tans =",
135+
sum_of_digits_compact(large_num),
136+
"\ttime =",
137+
timeit("z.sum_of_digits_compact(z.large_num)", setup="import __main__ as z"),
138+
"seconds",
139+
)
140+
141+
17142
if __name__ == "__main__":
18-
print(sum_of_digits(12345)) # ===> 15
143+
small_num = 262144
144+
medium_num = 1125899906842624
145+
large_num = 1267650600228229401496703205376
146+
benchmark()
147+
import doctest
148+
149+
doctest.testmod()

0 commit comments

Comments
 (0)
Please sign in to comment.