How many digits are in the base-10 number n? Well, if there are k digits in the number, then we'd have that the biggest digit is some multiple of 10k. The largest k-digit number is 999...9, k times, and this is equal to 10k + 1 - 1. Consequently, if we know that n has k digits in it, then we know that the value of n is at most 10k + 1 - 1. If we want to solve for k in terms of n, we get n ≤ 10k+1 - 1 n + 1 ≤ 10k+1 log10 (n + 1) ≤ k + 1 (log10 (n + 1)) - 1 ≤ k From which we get that k is approximately the base-10 logarithm of n. In other words, the number of digits in n is O(log n). |
+ 1
, so correct is n <= ( 10^k ) - 1
10^k is a one followed by k zeros. That is the smallest number with k+1 digits, Thus, the k-digit numbers are precisely those in the half-open interval [10^(k-1), 10^k ) For all of these numbers their base-10 logarithm is in [k-1,k), and thus the floor of the base-10 logarithm is k-1. Hence, the exact formula that gives you the number of digits in any positive integer n is 1+floor(log10(n)) |
How many digits are in the base-10 number n? Well, if there are k digits in the number, then we'd have that the biggest digit is some multiple of 10k. The largest k-digit number is 999...9, k times, and this is equal to (10^k + 1) - 1. Consequently, if we know that n has k digits in it, then we know that the value of n is at most (10^k + 1) - 1. If we want to solve for k in terms of n, we get |
Well, if there are k digits in the number, then we'd have that the biggest digit is some multiple of 10k |
so lets take a 3 digit number n, k = 2 |
Why? Read the text you quoted. "if there are k digits in the number". That means k being the number of digits in the representation is a basic assumption. |
n ≤ (10^k+1) - 1 n + 1 ≤ 10^k+1 |
n ≤ (10^3+1) - 1 n + 1 ≤ 10^3+1 |
Let k be a natural greater than 1 and n be a non-negative integer, then k^n = (k - 1) * k^(n - 1) + (k - 1) * k^(n - 2) + ... + (k - 1) * k^0 + 1 |