la sua descrizione in questo modo:
persistence(39) == 3 // because 3*9 = 27, 2*7 = 14, 1*4=4 // and 4 has only one digit persistence(999) == 4 // because 9*9*9 = 729, 7*2*9 = 126, // 1*2*6 = 12, and finally 1*2 = 2 persistence(4) == 0 // because 4 is already a one-digit number
Posso solo fare così:
$arrays = str_split(39); foreach ($arrays as $key => $value) { echo $arrays[$key]*$arrays[$key+1]; }
Il prossimo sono confuso
Qualche soluzione per risolvere il mio problema?
$arrays = str_split('999'); //Your string $j=0; //Counter for counting the number of iteration while (count($arrays)>1){ //When more than 2 indexes in arrays for($i=0;$i<count($arrays);$i++){ //Iterate through all permutations $arrays = arrays_product($arrays); //Multiplies all numbers in arrays $arrays = str_split($arrays); //Split the arrays up again $j++; //Increment counter(as literal as I can sound) } } echo $j; //Print out the number of times
Puoi anche utilizzare il richiamo della function finché non ha solo una cifra …
$count = 0; persistence(2,$count); function persistence($i,&$c){ $v = str_split($i); if(count($v) > 1){ $total = arrays_product($v); $c++; persistence($total,$c); } } var_dump($count);