Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 1.29 KB

13-Take-a-Number-And-Sum-Its-Digits.md

File metadata and controls

41 lines (30 loc) · 1.29 KB

Take a Number And Sum Its Digits Raised To The Consecutive Powers

The number 89 is the first integer with more than one digit that fulfills the property partially introduced in the title of this kata. What's the use of saying "Eureka"? Because this sum gives the same number.

In effect: 89 = 8^1 + 9^2

The next number in having this property is 135.

See this property again: 135 = 1^1 + 3^2 + 5^3

We need a function to collect these numbers, that may receive two integers a, b that defines the range [a, b] (inclusive) and outputs a list of the sorted numbers in the range that fulfills the property described above.

Let's see some cases:

sum_dig_pow(1, 10) == [1, 2, 3, 4, 5, 6, 7, 8, 9]
sum_dig_pow(1, 100) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 89]

If there are no numbers of this kind in the range [a, b] the function should output an empty list.

sum_dig_pow(90, 100) == []

Solution

def sum_dig_pow(a, b): # range(a, b + 1) will be studied by the function
    lst = []
    for i in range(a,b+1):
        if i > 9:
            if(sum_digits(i) == i):
                lst.append(i)
        else:
            lst.append(i)
    return lst

def sum_digits(num):
    n = 1
    total = 0
    for i in str(num):
        total += int(i)**n
        n+=1
    return total