Recursive Digit Sum

  • + 0 comments

    Another solution, using the same logic but different structure. I was getting stuck with the type of sum variable, where I was using int. Then I come here and I saw this solution, tks to share it.

    if(n.length() < 2) { return Character.getNumericValue(n.charAt(0)); }

        int count = 0;
        long sum = 0;
        while(count < n.length()) {
            sum += Character.getNumericValue(n.charAt(count));
            count++;
        }
    
        return superDigit(Long.toString(sum * k), 1);