We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
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.
Recursive Digit Sum
You are viewing a single comment's thread. Return to all 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)
PHP Solution
return bcmod(bcmul($n,$k),'9')?:9;
very nice!!
Thanx for contribution . Why echo 9;
If the remainder is zero( when we perform (n*k)%9) it means the sum of digits is 9.
function superDigit(k) { n,res ? $res : '9'; }
Why this code doesn't work in Javascript? I rewrite it to JS with the same logic
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; }
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.
Because n here in String.
thanx bro!!!
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
If we take n = 88, k = 2, then
8 + 8 + 8 + 8 = 16 + 16
But (n*k) gives 88*2 = 176
16 + 16 = 32 => superdigit(32) = 5 88 * 2 = 176 => 176 mod 9 = 5
it's a arithmetic trick to get the result
the trick is that 88*2 = 16+160 and superdigit(160)=superdigit(16)
Can you please provide any refernces for math like this. Thank you in Advance:)