DEV Community

loading...

Discussion on: Daily Challenge #214 - Persistent Bugger

Collapse
vidit1999 profile image
Vidit Sarkar

C++ solution

/*
Recursion :
persistence(num) = 0 if num < 10
                 = [1 + persistence(multiplication of digits of num)] if num >= 10
*/

int persistence(int num){
    // Given number is positive, so no need to check for negative numbers

    // if number is already single digit return 0
    if(num < 10)
        return 0;

    // get the multiplication of digits
    int mul = 1;
    while(num > 0){
        mul *= (num%10);
        num /= 10;
    }

    // return 1 + persistence(mul)
    return 1 + persistence(mul);
}