Skip to content

digital root method #6140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 38 commits into from
Closed
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
00c22a3
Added censor function
meg-1 May 16, 2021
4194344
Added censor code
meg-1 May 17, 2021
e7d4102
Added comments to the code
meg-1 May 17, 2021
93bd4ac
modified censor function
meg-1 May 22, 2021
b714278
added decrypt function
meg-1 Aug 1, 2021
c879c7f
added cypher and decypher functions, deleted censor and decrypt funct…
meg-1 Aug 4, 2021
e60dd6f
Deleted decrypt.py
meg-1 Aug 4, 2021
b3e0cb8
Deleted censor.py
meg-1 Aug 4, 2021
e14b9ef
Merge branch 'TheAlgorithms:master' into master
meg-1 Aug 4, 2021
947f7d8
edited the crypt and decrypt files
meg-1 Aug 4, 2021
daf21ad
Update cypher_txt.py
meg-1 Aug 4, 2021
d3d8b29
Remove the endline in cypher.py
meg-1 Aug 4, 2021
752c197
Removed the print at the end of decypher.py
meg-1 Aug 4, 2021
9f44028
comitting changes
meg-1 Aug 18, 2021
f72431b
added 4 new algorithms
meg-1 Aug 18, 2021
4c891f4
added tests to the four files
meg-1 Aug 25, 2021
c600b10
added type hints for the function variables
meg-1 Aug 25, 2021
4286e40
Merge branch 'TheAlgorithms:master' into master
meg-1 Dec 11, 2021
7a909e2
Deleted decode message
meg-1 Dec 25, 2021
f5b5411
Deleted code message
meg-1 Dec 25, 2021
bc8388c
Welford average algorithm
meg-1 Jan 29, 2022
9be479e
added average welford algorithm
meg-1 Feb 9, 2022
5ebf785
indentation added
meg-1 Mar 26, 2022
0570692
switched print to return in the end
meg-1 Mar 26, 2022
b589177
added digital_root function
meg-1 May 7, 2022
16f087f
re-opened the digital root pr
meg-1 May 11, 2022
f5b37ac
deleting average_welford
meg-1 May 11, 2022
218f0de
deleting censor.py
meg-1 May 11, 2022
3ecf247
deleting decrypt.py
meg-1 May 11, 2022
669a12c
fixed mistakes sum_of_digits
meg-1 May 18, 2022
b49257b
fixed "sum_of_digits.py"
meg-1 Jun 1, 2022
8c4653c
fix black
poyea Jun 11, 2022
4e09780
fixing Value Error
meg-1 Jun 15, 2022
fbec04e
adding tests to digital_root
meg-1 Jun 22, 2022
901c4fa
Update maths/sum_of_digits.py
meg-1 Jul 9, 2022
c56f3f1
fixing return type
meg-1 Jul 9, 2022
bd8dd3f
fixing tests in digital_root
meg-1 Jul 9, 2022
67eca3a
enhancing maths/sum_of_digits.py
meg-1 Jul 13, 2022
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
49 changes: 48 additions & 1 deletion maths/sum_of_digits.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,32 @@ def sum_of_digits_compact(n: int) -> int:
return sum(int(c) for c in str(abs(n)))


def digital_root(n: int) -> int:
"""
finding the digital root of n
https://en.wikipedia.org/wiki/Digital_root

>>> digital_root(394328)
2
>>> digital_root(-394328)
-1
>>> digital_root(123)
6
>>> digital_root(0)
0
>>> digital_root("109820394")
-1
"""

try:
return n % 9 or n and 9 if n >= 0 else -1
except TypeError:
return -1


def benchmark() -> None:
"""
Benchmark code for comparing 3 functions,
Benchmark code for comparing 4 functions,
with 3 different length int values.
"""
print("\nFor small_num = ", small_num, ":")
Expand Down Expand Up @@ -85,6 +108,14 @@ def benchmark() -> None:
timeit("z.sum_of_digits_compact(z.small_num)", setup="import __main__ as z"),
"seconds",
)
print(
"> digital_root()",
"\tans =",
digital_root(small_num),
"\ttime =",
timeit("z.digital_root(z.small_num)", setup="import __main__ as z"),
"seconds",
)

print("\nFor medium_num = ", medium_num, ":")
print(
Expand All @@ -111,6 +142,14 @@ def benchmark() -> None:
timeit("z.sum_of_digits_compact(z.medium_num)", setup="import __main__ as z"),
"seconds",
)
print(
"> digital_root()",
"\t\tans =",
digital_root(medium_num),
"\ttime =",
timeit("z.digital_root(z.medium_num)", setup="import __main__ as z"),
"seconds",
)

print("\nFor large_num = ", large_num, ":")
print(
Expand All @@ -137,6 +176,14 @@ def benchmark() -> None:
timeit("z.sum_of_digits_compact(z.large_num)", setup="import __main__ as z"),
"seconds",
)
print(
"> digital_root()",
"\tans =",
digital_root(large_num),
"\ttime =",
timeit("z.digital_root(z.large_num)", setup="import __main__ as z"),
"seconds",
)


if __name__ == "__main__":
Expand Down