Recursive Digit Sum

Sort by

recency

|

576 Discussions

|

  • + 0 comments

    Python solution. Another terrible formulated problem, look at the line:

    n = str(sum(int(digit) for digit in n))k

    This is not what the problem asks, but works, because when working with big numbers you cannot simply multiply the string, here throws a Memory Error, you need to reduce first, something you wouldn't do if was a normal work, you NEED the exception that was throw to investigate, that is how a developer work, ffs!

    Also who knows the math that multiply the sum of digits is the same of sum the multiply of the original number?

    Where are those genius?

    def superDigit(n, k):
        # Apply k at the beginning
        n = str(sum(int(digit) for digit in n))*k
        # Reduce until single digit
        while len(n) > 1:
            n = str(sum(int(digit) for digit in n))
        return int(n)
    
  • + 0 comments

    Finally solved this one in Java.

    What was tripping me up was some of the tests use really long strings and my first (few) solution timed out.

    My working hack was to test if the input string was longer than an integer. If not run normally, and if so, break the string into integer sized bites...

    it passes now but could be rewritten to be shorter likely.

  • + 1 comment

    My code is in C# and is performative. It fails 2 hidden test cases with wrong answer which is really frustrating because I cannot see the input. Any idea how to get to see the Inputs?

  • + 0 comments
    public static int superDigit(String n, int k) {
        // Write your code here
            if (n.length() == 1) return Integer.parseInt(n);
            
            long count=0;
            for (int i = 0; i < n.length(); i++) {
                count += Integer.parseInt(n.substring(i, i + 1));
            }
            
            count *= k;
            
            return superDigit(Long.toString(count), 1);
        }
    
  • + 0 comments

    I can't do it because of optimization problems and I don't know how to further improve the 3-line code. so i give up