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
Recursive Digit Sum
Sort by
recency
|
814 Discussions
|
Please Login in order to post a comment
The RUNTIME ERROR COMES FOR SOME TEST CASES SINCE THE THE INPUT SIZE OF THE STRING WHEN MULTIPLIED WITH K IT BECOMES HUGE STRING WHICH IS TOUGH PROCESS. SO AT FIRST JUST SUM ALL THE DIGITS IN NUMBER STRING AND MULTIPLY WITH K. FOR EXMP: n='11' k=2 p=n*k p='1111' # this could our naive approach s=4 # sum of all digits. now,
p= (sum of all digits in n which is equal to 2) * k
=> p=4 #same as our naive approach.
!/bin/python3
import math import os import random import re import sys
def superDigit(n, k): # Write your code here #base condition if len(n)==1: return int(n) s=0 for i in n: s=s+int(i) p=s*k if k>1 else s
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')
I interpreted the restriction of n < 10^100000 as we would definitely have an input containing at least 10 ^19 characters which would extrapolate 64 bit integers.
In these circumstances, this solution that passes all the tests would fail:
A solution that would work for n > 10^19 would require perorm operations with bignum:
c# solution with recursion :
Python one-liner with recursion:
return n if k == 1 and n < 10 else superDigit(k * sum(int(d) for d in list(str(n))), 1)