Skip to content

Instantly share code, notes, and snippets.

@cfax
cfax / armstrong.py
Last active March 24, 2022 15:06
Sum all Armstrong numbers within a range
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)
@cfax
cfax / armstrong.hs
Last active March 24, 2022 15:06
Sum all Armstrong numbers within a range
digits :: (Integral n) => n -> [n]
digits = reverse . go
where go n
| n < 10 = [n]
| otherwise = r : digits q
where (q, r) = n `quotRem` 10
numberLength :: Int -> Int
numberLength = length . digits