Computer Science, asked by jardelizaladyprinces, 2 months ago

Create a function in any programming language to display the first 10 natural numbers then compute its time complexity.

Answers

Answered by KimBlink
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 KimBunny
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