Recursive Digit Sum
Problem statement
We define super digit of a integer x
using the following rules:
Given an integer, we need to find the super digit of the integer.
- if
x
has only one digit, then its super digit isx
- Otherwise the super digit of
x
is equal to the super digit of the sum of the digits ofx
.
For example, the super digit of 9875
will be calculated as:
super_digit(9875) 9 + 8 + 7 + 5 = 29
super_digit(29) 2 + 9 = 11
super_digit(11) 1 + 1 = 2
super_digit(2) = 2
Before find the super digit of p
we need to calculate p
as a product of the given inputs n
and k
. Let’s illustrate this as follows:
n = '9875'
k = 4
The numer p is created by concatenating the n number k times.
Therefore p for this concreted example sill be:
p = '9875987598759875'
After calculating the p
number we proceed to find their super digit.
superDigit(p) = superDigit(9875987598759875)
9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116
superDigit(p) = superDigit(116)
1+1+6 = 8
superDigit(p) = superDigit(8)
Considerations
We are asked to find the super digit of a given number. The super digit can be calculated by repeatedly adding all the numbers that compounds the p
number until there is just one digit.
For starters, instead of concatenating the n
number k
times. Let’s calculate the sum(n)
and then multiply it by k.
The super digit, is an algorithm that calculates the digit sum of a number until it becomes a single digit.