Created
August 17, 2022 21:11
-
-
Save piusayowale/7ffa29087a6d4cd7ce9be5056909b72c to your computer and use it in GitHub Desktop.
get the recursive digit sum, still in review
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
long long getsuperDigit(string num, int n){ | |
long long sum = 0; | |
string temp; | |
temp.push_back(num[n]); | |
sum = stoll(temp); | |
if(n == 0){ | |
return sum; | |
} | |
return sum + getsuperDigit(num, n - 1); | |
} | |
long long getsuperHelper(long long res){ | |
string temp = to_string(res); | |
long long r = res; | |
cout << temp.length(); | |
while (temp.length() > 1) { | |
r = getsuperDigit(temp, temp.length()- 1); | |
temp = to_string(r); | |
} | |
return r; | |
} | |
int superDigit(string n, int k) { | |
int r = getsuperDigit(n, n.size()- 1) * k; | |
return getsuperHelper(r); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment