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
Economy,
1 month ago
Sociology,
1 month ago
History,
2 months ago
English,
2 months ago
Social Sciences,
9 months ago
Biology,
9 months ago
Social Sciences,
9 months ago