Here is the code:
int sumDigits(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
I understand this code, and that the code will take the ones place digit, add that digit to sum, and remove that digit. It keeps doing this until n is equal to 0, at which point it will return sum. Intuitively the runtime will be the number of digits in number N. But I do not understand why this time complexity is O(logN). I thought it was O(N).
Even with explanation like: "A number with d digits can have a value up to 10^d. If n = 10^d, then d = log n. Therefore runtime is O(logN)." does not totally click.
I follow the first part that if d is say 3, then value < 10^d == value < 1000. Meaning max value is 999 with a number of length 3, which I agree with. But after that, when they make the connection that if n = 10^d, I do not understand how 1) they knew to make that equality and 2) how this makes the complexity O(logN) rather than O(N).
That is, we've upper-bounded and lower-bounded the number of digits d by terms that are O(log(N)) . The above shows that the number of digits is O(log(N)) , or more precisely Theta(log(N)) . The time complexity is the same since it's proportional to the number of digits.
Logarithmic Time Complexity O(Log n): The time Complexity of a loop is considered as O(Logn) if the loop variables are divided/multiplied by a constant amount. And also for recursive calls in the recursive function, the Time Complexity is considered as O(Logn).
The time complexity is the same as the time complexity of the function. We are using for loop in our function, and we already saw that using for loop, N operations are performed to calculate the sum of the first N natural numbers. Thus, the time complexity is O ( N ) O(N) O(N).
A simple solution for this problem is to read number in string form and one by one check divisibility by each digit which appears in N. Time complexity for this approach will be O(N2).
The complexity is proportional to the number of digits. After all, if there are 2,351 digits in the number, the while
loop will iterate 2,351 times.
So the question boils down to, "how many digits are there in N
, asymptotically?". A number with d
digits is between 10^(d-1)
inclusive and 10^d
exclusive. In other words, let d
be the number of digits in N
, and we have the inequalities 10^(d-1) <= N < 10^d
. Taking a logarithm, we have d-1 <= log(N) < d
. (We can maintain the inequalities because logarithms are monotonic.) Adding 1
to the left inequality gives d <= log(N) + 1
, and combining with the previous result, we have log(N) < d <= log(N) + 1
. That is, we've upper-bounded and lower-bounded the number of digits d
by terms that are O(log(N))
.
The above shows that the number of digits is O(log(N))
, or more precisely Theta(log(N))
. The time complexity is the same since it's proportional to the number of digits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With