|
| 1 | +# [Recursive Digit Sum](https://www.hackerrank.com/challenges/recursive-digit-sum) |
| 2 | + |
| 3 | +- Difficulty: `#medium` |
| 4 | +- Category: `#ProblemSolvingBasic` |
| 5 | + |
| 6 | +We define super digit of an integer `x` using the following rules: |
| 7 | + |
| 8 | +Given an integer, we need to find the super digit of the integer. |
| 9 | + |
| 10 | +- If `x` has only `1` digit, then its super digit is `x`. |
| 11 | +- Otherwise, the super digit of `x` is equal to the super digit of |
| 12 | + the sum of the digits of `x`. |
| 13 | + |
| 14 | +For example, the super digit of `9875` will be calculated as: |
| 15 | + |
| 16 | +```text |
| 17 | + super_digit(9875) 9+8+7+5 = 29 |
| 18 | + super_digit(29) 2 + 9 = 11 |
| 19 | + super_digit(11) 1 + 1 = 2 |
| 20 | + super_digit(2) = 2 |
| 21 | +``` |
| 22 | + |
| 23 | +## Example |
| 24 | + |
| 25 | +`n = 9875` |
| 26 | + |
| 27 | +`k = 4` |
| 28 | + |
| 29 | +The number `p` is created by concatenating the string `n` `k` |
| 30 | +times so the initial `p = 9875987598759875`. |
| 31 | + |
| 32 | +```text |
| 33 | + superDigit(p) = superDigit(9875987598759875) |
| 34 | + 9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116 |
| 35 | + superDigit(p) = superDigit(116) |
| 36 | + 1+1+6 = 8 |
| 37 | + superDigit(p) = superDigit(8) |
| 38 | +``` |
| 39 | + |
| 40 | +All of the digits of `p` sum to `116`. The digits of `116` sum to `8`. |
| 41 | +`8` is only one digit, so it is the super digit. |
| 42 | + |
| 43 | +## Function Description |
| 44 | + |
| 45 | +Complete the function superDigit in the editor below. |
| 46 | +It must return the calculated super digit as an integer. |
| 47 | + |
| 48 | +superDigit has the following parameter(s): |
| 49 | + |
| 50 | +- `string n`: a string representation of an integer |
| 51 | +- `int k`: the times to concatenate to make |
| 52 | + |
| 53 | +## Returns |
| 54 | + |
| 55 | +- `int`: the super digit of repeated times |
| 56 | + |
| 57 | +## Input Format |
| 58 | + |
| 59 | +The first line contains two space separated integers, `n` and `k`. |
| 60 | + |
| 61 | +## Constraints |
| 62 | + |
| 63 | +- $ 1 \leq n \leq 10^100000 $ |
| 64 | +- $ 1 \leq k \leq 10^5 $ |
| 65 | + |
| 66 | +## Sample Input 0 |
| 67 | + |
| 68 | +```text |
| 69 | +148 3 |
| 70 | +``` |
| 71 | + |
| 72 | +## Sample Output 0 |
| 73 | + |
| 74 | +```text |
| 75 | +3 |
| 76 | +``` |
| 77 | + |
| 78 | +## Explanation 0 |
| 79 | + |
| 80 | +Here `n = 148` and `k = 3`, so `p = 148148148`. |
| 81 | + |
| 82 | +```text |
| 83 | +super_digit(P) = super_digit(148148148) |
| 84 | + = super_digit(1+4+8+1+4+8+1+4+8) |
| 85 | + = super_digit(39) |
| 86 | + = super_digit(3+9) |
| 87 | + = super_digit(12) |
| 88 | + = super_digit(1+2) |
| 89 | + = super_digit(3) |
| 90 | + = 3 |
| 91 | +``` |
| 92 | + |
| 93 | +## Sample Input 1 |
| 94 | + |
| 95 | +```text |
| 96 | +9875 4 |
| 97 | +``` |
| 98 | + |
| 99 | +## Sample Output 1 |
| 100 | + |
| 101 | +```text |
| 102 | +8 |
| 103 | +``` |
| 104 | + |
| 105 | +## Sample Input 2 |
| 106 | + |
| 107 | +```text |
| 108 | +123 3 |
| 109 | +``` |
| 110 | + |
| 111 | +## Sample Output 2 |
| 112 | + |
| 113 | +```text |
| 114 | +9 |
| 115 | +``` |
| 116 | + |
| 117 | +## Explanation 2 |
| 118 | + |
| 119 | +Here `n = 123` and `k = 3`, so `p = 123123123`. |
| 120 | + |
| 121 | +```text |
| 122 | +super_digit(P) = super_digit(123123123) |
| 123 | + = super_digit(1+2+3+1+2+3+1+2+3) |
| 124 | + = super_digit(18) |
| 125 | + = super_digit(1+8) |
| 126 | + = super_digit(9) |
| 127 | + = 9 |
| 128 | +``` |
0 commit comments