Recursive Digit Sum

  • + 5 comments

    Java 8 Solution (with Recursion)

    static int superDigit(String n, int k) {
        n = n.chars().mapToLong(Character::getNumericValue).sum() * k
                + "";
        return (n.length() > 1) ? superDigit(n, 1) :
                                    Character.getNumericValue(n.charAt(0));
    }
    
    • + 1 comment

      I use the same solution above in JS and it sadly times out--I guess the JVM beats node.js in runtime speeds

      • + 1 comment

        @drzaiusx11 This recursive JavaScript solution has passed all test cases in Node.js:

        JavaScript Solution (with Recursion)

        function superDigit(n, k) {
            n = n.split("").reduce((a, b) => +a + +b) * k + "";
            return (n.length > 1) ? superDigit(n, 1) : n.charAt(0);
        }
        
        • + 1 comment

          can you please explain why you wrote this +a + +b instead of a+b?. i tried running a+b, but it failed.

          • + 0 comments

            Late reply, but the reason this works is: ( +a + +b ) coerces each variable with a unary operator ( + ), to assure the addition of two integers and not the concatination of two strings. Then, the integer is being multiplied by the concat multiplier ( * k ) and converted back to a string ( + "" )

    • + 1 comment

      This seems awesome. I just wanted to know why you used n.chars().mapToLong(Character::getNumericValue).sum() * k + ""; I understand you are trying to sum all the characters and multiply by k and converting back to string but this notation i have never used. is this faster than say doing a for loop to find this out? doesnt this take O(x) time where x is the no of chars in string n?

      • + 1 comment

        @Utsav1998 Using Java 8 Stream may NOT be faster than a "for loop" but the Stream is more "fluent" in terms of Functional Programming, and the complexity of that stream is indeed O(x).

        • + 1 comment

          then why the hell we are getting time error when using for loop but not when using streams :(

          • + 0 comments

            @Singhalr31 For a simple "for" loop (without Collections or Maps), Java 8 Stream "could be" overkill. If you need to iterate over a "LARGE" Collection (or Map) , however, Java 8 may be faster because you could avoid creating LOTS of "intermediate" Objects by simply streaming data in the Collection from one operation to another.

    • + 1 comment

      This is the most elegant solution for Java I have seen

      • + 0 comments

        @rafaelcfreire You're welcome! And this is the true power of Java 8.

    • + 0 comments

      Wow! , this is really awesome. Even i used BigInteger for the problem but TLE occured in test case 8 and 9. Thank you so much @tat_lim for the solution!