Skip to content

Commit 6c402a3

Browse files
VarunArora24pre-commit-ci[bot]cclauss
authored
Add Python function to check Disarium numbers (#13603)
* Add Python function to check Disarium numbers * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating DIRECTORY.md * Add documentation link for Disarium number check Added a link to Rosetta Code for Disarium numbers. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent ccc4b19 commit 6c402a3

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@
760760
* [Decimal To Fraction](maths/decimal_to_fraction.py)
761761
* [Derangement](maths/derangement.py)
762762
* [Digital Root](maths/digital_root.py)
763+
* [Disarium](maths/disarium.py)
763764
* [Dodecahedron](maths/dodecahedron.py)
764765
* [Double Factorial](maths/double_factorial.py)
765766
* [Dual Number Automatic Differentiation](maths/dual_number_automatic_differentiation.py)

maths/disarium.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def is_disarium(num: int) -> bool:
2+
"""
3+
Check if a number is Disarium.
4+
5+
https://rosettacode.org/wiki/Disarium_numbers
6+
7+
>>> is_disarium(89)
8+
True
9+
>>> is_disarium(75)
10+
False
11+
>>> is_disarium(135)
12+
True
13+
"""
14+
digits = list(str(num))
15+
total = 0
16+
for i in range(len(digits)):
17+
total += int(digits[i]) ** (i + 1)
18+
return total == num
19+
20+
21+
if __name__ == "__main__":
22+
import doctest
23+
24+
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)