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.
- Recursive Digit Sum
- Discussions
Recursive Digit Sum
Recursive Digit Sum
Sort by
recency
|
609 Discussions
|
Please Login in order to post a comment
Tests are not even correct.
I think the test are broken. I'm getting the results that match the sample, while the test results do not match the sample.
Scala Solution ` def superDigit(n: String, k: Int): Int = { var num = (n.map(.asDigit).sum.toLong * k).toString while (num.length > 1){ num = num.map(.asDigit).sum.toString } num.toInt
}
object Solution { def main(args: Array[String]): Unit = {
} `
You can solve this mathematically in Python with:
However, if
n
is a very long number,int(n)
may raise aValueError
due to the digit limit introduced in recent Python versions. You can bypass this by increasing the limit:Use with caution, especially when handling untrusted input, as increasing the limit can expose your code to denial-of-service risks.
Reference: Python Docs – Integer string conversion length limitation
If you're having trouble with timeouts or the last 3 cases:
len(n) * k
length or try to implement a loop over the string repeatedly - just calculate the sum of the digits ofn
then multiply by k