Recursive digit sum hackerrank solution python

Hackerrank Solution: Recursive Digit Sum

Original Problem

We define super digit of an integer \(x\) using the following rules:

Given an integer, we need to find the super digit of the integer.

  • 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 sum of the digits of \(x\).

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  

You are given two numbers \(n\) and \(k\). The number \(p\) is created by concatenating the string \(n\) \(k\) times. Continuing the above example where \(n=9875\), assume your value \(k=4\). Your initial \(p= 9875\, 9875\, 9875\, 9875\)(spaces added for clarity).

    superDigit(p) = superDigit(9875987598759875)
                  5+7+8+9+5+7+8+9+5+7+8+9+5+7+8+9 = 116
    superDigit(p) = superDigit(116)
                  1+1+6 = 8
    superDigit(p) = superDigit(8)

All of the digits of \(p\) sum to \(116\). The digits of \(116\) sum to \(8\). \(8\) is only one digit, so it's the super digit.

Function Description

Complete the function superDigit in the editor below. It must return the calculated super digit as an integer.

superDigit has the following parameters:

  • n: a string representation of an integer
  • k: an integer, the times to concatenate \(n\) to make \(p\)

Input Format

The first line contains two space separated integers, \(n\) and \(k\).

Constraints

  • \(1\leq n<10^{100000}\)
  • \(1\leq k\leq 10^5\)

Output Format

Return the super digit of \(p\), where \(p\) is created as described above.

Solution

What is called super digit here, is practically the digital root of a number, which can be implemented recursively, or easier using modulo. We derived the digital root function alreay:

\[\text{dr}(n) =1+ (n - 1\pmod{9})\]

Using it for our constructed number \(p=k\times n\), where \(\times\) denotes concatinations, we can say:

\[\begin{array}{rl} \text{dr}(p) &= \text{dr}(k\times n)\\ &= \text{dr}(n\cdot 10^{0\cdot L(n)} + n\cdot 10^{1\cdot L(n)} + \dots +n\cdot 10^{k\cdot L(n)})\\ &= 1+ ((n\cdot 10^{0\cdot L(n)} + n\cdot 10^{1\cdot L(n)} + \dots +n\cdot 10^{(k-1)\cdot L(n)}) - 1\pmod{9})\\ &= 1+ (k\cdot n - 1\pmod{9}) \end{array}\]

With \(L(n)\) denoting the number of digits of numbr \(n\). Since in our problem \(n\) is a string with many many digits, we don't use the number directly, but sum over the digits of \(n\) as \(t=\sum\limits_{i=0}n_i\) first and state

\[\text{dr}(p) = \text{dr}(k\times n) = \text{dr}(k\cdot n) = \text{dr}(k\cdot t)\]

Implementing this algorithm in Python then is pretty straightforward and bound to \(O(\log n)\) and does not even look at \(p\)!:

def superDigit(n, k):
    return 1 + (k * sum(int(x) for x in n) - 1) % 9

« Back to problem overview

Sort 720 Discussions, By:

Please Login in order to post a comment

  • Recursive digit sum hackerrank solution python

    5 days ago+ 0 comments

    def superDigit(n, k):
        if(len(n)==1 and k==1):
            return int(n)
        else:
            suma = sum(list(map(int,n)))
            return superDigit(str(suma*k),1)
    

  • Recursive digit sum hackerrank solution python

    6 days ago+ 0 comments

    def superDigit(n, k):
        digit=str(n)
        li=list(map(int,digit))
        if sum(li)*k<10:
            return sum(li)
        else:
            return superDigit(sum(li)*k,1)
    

  • Recursive digit sum hackerrank solution python

    1 week ago+ 0 comments

    Someone please help why 3 large testcases are failing for below code:

    public static int superDigit(String n, int k) {
            long total=0;
            int sum=0;
            if(n.length()==1){
                return Integer.valueOf(n);
            }else{
            for(int i=0;i<n.length();i++){
               sum += Integer.parseInt(String.valueOf(n.charAt(i)));
            }
            total=sum*k;
            
            return superDigit(String.valueOf(total),1);
        }
    

  • Recursive digit sum hackerrank solution python

    3 weeks ago+ 1 comment

    Hey everyone, here is a very optimized code for a recursive code for this challenge using python :-- :-- :--

    def superDigit(n, k):
        summ = str(eval("+".join(list(n)))*k)
        if len(summ) == 1:
            return summ
        else:
            return superDigit(summ, 1)
        # Write your code here
    

  • Recursive digit sum hackerrank solution python

    3 weeks ago+ 0 comments

    my solution in Java , you need to declare sum as long because test case inputs are damn big numbers some test cases will fail if you don't.

    public static int superDigit(String n, int k) {
        // Write your code here
          if(n.length()==1){
             return Integer.valueOf(n);
          }
             long sum =0;
             for(int i=0;i<n.length();i++){
                sum += n.charAt(i)-'0';
             }
             if(k!=1) sum = sum *k;
             return superDigit(""+sum, 1);
        
        }