N
N
New Developer2020-10-15 12:01:09
JavaScript
New Developer, 2020-10-15 12:01:09

Recursive multiplication of digits of an integer, how to find out the number of function calls?

Hello.
The program should implement bitwise multiplication of a positive integer until 1 bit remains and return the number of steps to this result. Example:
99999=>59049=>0===2 steps // 9*9*9*9*9=>5*9*0*4*9=>0;
13=>3===1 step // 1*3=>3;
999=>729=>126=>12=>2===4 steps // 9*9*9=>7*2*9=>1*2*6=>1*2=>2;
7=>7===0 steps //
Is it possible to insert a function call counter into the recursion?

let steps = 0;
function umnozhenie(x) {
    steps += 1;
    x = String(x).match(/[0-9]/g).map(Number).reduce((a, b) => a * b);
    if (x >= 10) { x = umnozhenie(x); };
    return steps;
}


The program shows incorrect results for large numbers. But if returnI put the function itself in, then I get the correct value of the final digit.
I can't work with cycles.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2020-10-15
@New-Developer

const multiply = num =>
  num > 9
    ? 1 + multiply([...`${num}`].reduce((acc, n) => acc * n))
    : 0;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question