Create a function in any programming language to display the first 10 natural numbers then compute its time complexity.
Answers
Answered by
6
Explanation:
The recursive function is, in JavaScript, something like:
const sumDigits = n => n === 0 ?
0 : n % 10 + sumDigits(Math.floor(n / 10));
The number of calls is proportional to the number of digits of n. The number of digits is basically log10(n) , proportional to log(n) , so the algorithm is O(log(n)) .
Answered by
0
Explanation:
The recursive function is, in JavaScript, something like: const sumDigits = n => n === 0 ? 0:n % 10 + sumDigits(Math.floor(n / 10)); The number of calls is proportional to the number of digits of n. The number of digits is basically log10(n), proportional to log(n), so the algorithm is O(log(n)).
Similar questions