Last active
March 24, 2022 15:06
-
-
Save cfax/beeb1cb97d96e6d1566f8ba26e09c542 to your computer and use it in GitHub Desktop.
Sum all Armstrong numbers within a range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import Decimal | |
def is_armstrong_number(number): | |
digits = Decimal(number).as_tuple.digits() | |
exp = len(digits) | |
return sum(d**exp for d in digits) == number | |
def sum_all_armstrong_numbers(start, end): | |
start_, end_ = sorted([start, end]) | |
return sum(number for number in range(start_, end_+1) | |
if is_armstrong_number(number)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment