물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Twisted Sum

BEstyle 2022. 11. 8. 14:43

DESCRIPTION:

Find the sum of the digits of all the numbers from 1 to N (both ends included).

Examples

# N = 4
1 + 2 + 3 + 4 = 10

# N = 10
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + (1 + 0) = 46

# N = 12
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + (1 + 0) + (1 + 1) + (1 + 2) = 51

def compute_sum(n):
    ans=0
    for i in range(n+1):
        ans+=digitSum(i)
    return ans


def digitSum(n):
    ans=0
    for i in str(n):
        ans+=int(i)
    return ans

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Binary to Text (ASCII) Conversion  (0) 2022.11.09
Square Matrix Multiplication  (0) 2022.11.08
Number Zoo Patrol  (0) 2022.11.07
Loose Change  (0) 2022.11.07
Triangle Type  (0) 2022.11.07