-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathget_recursive_digit_sum.py
More file actions
30 lines (24 loc) · 964 Bytes
/
get_recursive_digit_sum.py
File metadata and controls
30 lines (24 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""
We define super digit of an integer x using the following rules:
- If x has only 1 digit, then its super digit is x.
- Otherwise, the super digit of x is equal to the super digit of the digit-sum of x.
Here, digit-sum of a number is defined as the sum of its digits.
You are given two numbers n and k. You have to calculate the super digit of P.
P is created when number n is concatenated k times.
"""
def get_recursive_digit_sum(number, repeats):
"""
Returns the super digit of a number that is created
by concatenating number repeats times
"""
number_superdigit = __get_recursive_digit_sum(number)
return __get_recursive_digit_sum(str(number_superdigit) * repeats)
def __get_recursive_digit_sum(number):
if len(number) < 1:
return 0
if len(number) < 2:
return int(number)
digit_sum = 0
for digit in number:
digit_sum += int(digit)
return __get_recursive_digit_sum(str(digit_sum))