9
9
* number i.e, we can use it by inputting values whether it can be a
10
10
* positive/negative value, let's say: an integer. There is also a second
11
11
* 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
14
15
* [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Basic%20Math/Finding
15
16
* the number of digits in a number.md) repository.
16
17
*/
17
18
18
19
#include < cassert> // / for assert
20
+ #include < cmath> // / for log calculation
19
21
#include < iostream> // / for IO operations
20
22
21
23
/* *
22
24
* @brief The main function that checks
23
25
* the number of digits in a number.
26
+ * TC : O(number of digits)
24
27
* @param n the number to check its digits
25
28
* @returns the digits count
26
29
*/
@@ -40,27 +43,60 @@ uint64_t finding_number_of_digits_in_a_number(uint64_t n) {
40
43
return count;
41
44
}
42
45
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
+
43
69
/* *
44
70
* @brief Self-test implementations
45
71
* @returns void
46
72
*/
47
- static void test () {
73
+ static void first_test () {
48
74
assert (finding_number_of_digits_in_a_number (5492 ) == 4 );
49
75
assert (finding_number_of_digits_in_a_number (-0 ) == 0 );
50
76
assert (finding_number_of_digits_in_a_number (10000 ) == 5 );
51
77
assert (finding_number_of_digits_in_a_number (9 ) == 1 );
52
78
assert (finding_number_of_digits_in_a_number (100000 ) == 6 );
53
79
assert (finding_number_of_digits_in_a_number (13 ) == 2 );
54
80
assert (finding_number_of_digits_in_a_number (564 ) == 3 );
55
-
56
- std::cout << " All tests have successfully passed!\n " ;
57
81
}
58
82
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
+ }
59
92
/* *
60
93
* @brief Main function
61
94
* @returns 0 on exit
62
95
*/
63
96
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 " ;
65
101
return 0 ;
66
102
}
0 commit comments