Skip to content

Commit 29535e6

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/leetcode-cpp' into leetcode-cpp
2 parents 2a6c418 + c994028 commit 29535e6

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

math/finding_number_of_digits_in_a_number.cpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99
* number i.e, we can use it by inputting values whether it can be a
1010
* positive/negative value, let's say: an integer. There is also a second
1111
* method: by using "K = floor(log10(N) + 1)", but it's only applicable for
12-
* numbers (not integers).
13-
* For more details, refer to the
12+
* numbers (not integers). The code for that is also included
13+
* (finding_number_of_digits_in_a_number_using_log). For more details, refer to
14+
* the
1415
* [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Basic%20Math/Finding
1516
* the number of digits in a number.md) repository.
1617
*/
1718

1819
#include <cassert> /// for assert
20+
#include <cmath> /// for log calculation
1921
#include <iostream> /// for IO operations
2022

2123
/**
2224
* @brief The main function that checks
2325
* the number of digits in a number.
26+
* TC : O(number of digits)
2427
* @param n the number to check its digits
2528
* @returns the digits count
2629
*/
@@ -40,27 +43,60 @@ uint64_t finding_number_of_digits_in_a_number(uint64_t n) {
4043
return count;
4144
}
4245

46+
/**
47+
* @brief This function finds the number of digits
48+
* in constant time using logarithmic function
49+
* TC: O(1)
50+
* @param n the number to check its digits
51+
* @returns the digits count
52+
*/
53+
double finding_number_of_digits_in_a_number_using_log(double n) {
54+
// log(0) is undefined
55+
if (n == 0) {
56+
return 0;
57+
}
58+
59+
// to handle the negative numbers
60+
if (n < 0) {
61+
n = -n;
62+
}
63+
64+
double count = floor(log10(n) + 1);
65+
66+
return count;
67+
}
68+
4369
/**
4470
* @brief Self-test implementations
4571
* @returns void
4672
*/
47-
static void test() {
73+
static void first_test() {
4874
assert(finding_number_of_digits_in_a_number(5492) == 4);
4975
assert(finding_number_of_digits_in_a_number(-0) == 0);
5076
assert(finding_number_of_digits_in_a_number(10000) == 5);
5177
assert(finding_number_of_digits_in_a_number(9) == 1);
5278
assert(finding_number_of_digits_in_a_number(100000) == 6);
5379
assert(finding_number_of_digits_in_a_number(13) == 2);
5480
assert(finding_number_of_digits_in_a_number(564) == 3);
55-
56-
std::cout << "All tests have successfully passed!\n";
5781
}
5882

83+
static void second_test() {
84+
assert(finding_number_of_digits_in_a_number_using_log(5492) == 4);
85+
assert(finding_number_of_digits_in_a_number_using_log(-0) == 0);
86+
assert(finding_number_of_digits_in_a_number_using_log(10000) == 5);
87+
assert(finding_number_of_digits_in_a_number_using_log(9) == 1);
88+
assert(finding_number_of_digits_in_a_number_using_log(100000) == 6);
89+
assert(finding_number_of_digits_in_a_number_using_log(13) == 2);
90+
assert(finding_number_of_digits_in_a_number_using_log(564) == 3);
91+
}
5992
/**
6093
* @brief Main function
6194
* @returns 0 on exit
6295
*/
6396
int main() {
64-
test(); // run self-test implementations
97+
// run self-test implementations
98+
first_test();
99+
second_test();
100+
std::cout << "All tests have successfully passed!\n";
65101
return 0;
66102
}

0 commit comments

Comments
 (0)