MULTIPLICATIVE PERSISTENCE

the description

A number's persistence is the number of steps required to reduce it to a single digit by multiplying all its digits to obtain a second number, then multiplying all the digits of that number to obtain a third number, and so on until a one-digit number is obtained. For example, 77 has a persistence of four because it requires four steps to reduce it to one digit: 77-49-36-18-8. The smallest number of persistence one is 10, the smallest of persistence two is 25, the smallest of persistence three is 39, and the smaller of persistence four is 77. What is the smallest number of persistence five?

Martin Gardner, via Gizmodo

the solution

Persistence Lowest Number
1 10
2 25
3 39
4 77
5 679
6 6788
7 68889

the code

<?php

function persistence($num, $count=0) {
    $num_len = strlen($num);
    
    if($num_len == 1) {
        return $count;
    }
    $count++;
    
    $digits = str_split($num);
    $product = array_product($digits);
    $product_len = strlen($num);
    
    if( $product_len > 1 ) {
        $count = persistence($product, $count);
    }
    
    return $count;
}


$lowest_nums_array = array();
for($i=10; $i<1000000; $i++) {
    $num_persistence = persistence($i);
    if( ! isset($lowest_nums_array[$num_persistence]) ) {
        $lowest_nums_array[$num_persistence] = $i;
    }
}

?>