Skip to content

Instantly share code, notes, and snippets.

@piusayowale
Created August 17, 2022 21:11
Show Gist options
  • Save piusayowale/7ffa29087a6d4cd7ce9be5056909b72c to your computer and use it in GitHub Desktop.
Save piusayowale/7ffa29087a6d4cd7ce9be5056909b72c to your computer and use it in GitHub Desktop.
get the recursive digit sum, still in review
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