Recursive Digit Sum

  • + 5 comments

    consider this example, where n = 23, k = 3

    accoring to problem we can do it as 2+3+2+3+2+3 = 6+9 or (n*k) means (23*3) = 69 eventually which leads to 6+9 then we use modulus 9, according to decimal number system here is some interesting fact with proof being provided in the following link key to your curiosity

    cheers (y)

    • + 4 comments

      PHP Solution

      return bcmod(bcmul($n,$k),'9')?:9;
      
      • + 1 comment

        very nice!!

      • + 1 comment

        Thanx for contribution . Why echo 9;

        • + 1 comment

          If the remainder is zero( when we perform (n*k)%9) it means the sum of digits is 9.

      • + 0 comments

        function superDigit(k) { n,res ? $res : '9'; }

    • + 4 comments

      Why this code doesn't work in Javascript? I rewrite it to JS with the same logic

      • + 0 comments

        The test cases get too large for JS to represent as a 'Number'. Instead, use 'BigInt'. https://javascript.info/bigint

        function superDigit(n, k) { var value = Number((BigInt(n) * BigInt(k)) % BigInt(9)); return value ? value : 9; }

      • + 0 comments

        If it works in Python, it's slow on big numbers and a half of test cases are really big numbers. You shoud make the first pass and sum all the digits and then do the trick. You have a limit of 100.000.000.000 there which suits long type.

      • + 0 comments

        Because n here in String.

    • + 1 comment

      thanx bro!!!

      • + 0 comments

        Here is the detail explanation of this problem

        Check out the video explanations below (Telugu Videos)

        https://www.youtube.com/watch?v=mfGxzwRtFGQ - Implementation https://www.youtube.com/watch?v=Tzipp9yucwM&t=4s - Debugging https://www.youtube.com/watch?v=yncwkZ1mmPY - Hackerrank Validation

    • + 2 comments

      If we take n = 88, k = 2, then

      8 + 8 + 8 + 8 = 16 + 16

      But (n*k) gives 88*2 = 176

      • + 0 comments

        16 + 16 = 32 => superdigit(32) = 5 88 * 2 = 176 => 176 mod 9 = 5

        it's a arithmetic trick to get the result

      • + 0 comments

        the trick is that 88*2 = 16+160 and superdigit(160)=superdigit(16)

    • + 0 comments

      Can you please provide any refernces for math like this. Thank you in Advance:)