mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
feat: added log code for counting digits (#2234)
* added log code for counting digits * one space removed * fix: CI warnings (hopefully) * fix: revert for now * decoupled test functions * Update math/finding_number_of_digits_in_a_number.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/finding_number_of_digits_in_a_number.cpp typo changes Co-authored-by: David Leal <halfpacho@gmail.com> * uint64_t changed to double to resolve clang-tidy warnings * clang-format and clang-tidy fixes for61bb2dea
* comment improvement * clang-format and clang-tidy fixes for39ebc33d
* guarded required function --------- Co-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
This commit is contained in:
parent
65efd47bc1
commit
fddbf08f5a
@ -9,18 +9,21 @@
|
|||||||
* number i.e, we can use it by inputting values whether it can be a
|
* number i.e, we can use it by inputting values whether it can be a
|
||||||
* positive/negative value, let's say: an integer. There is also a second
|
* positive/negative value, let's say: an integer. There is also a second
|
||||||
* method: by using "K = floor(log10(N) + 1)", but it's only applicable for
|
* method: by using "K = floor(log10(N) + 1)", but it's only applicable for
|
||||||
* numbers (not integers).
|
* numbers (not integers). The code for that is also included
|
||||||
* For more details, refer to the
|
* (finding_number_of_digits_in_a_number_using_log). For more details, refer to
|
||||||
|
* the
|
||||||
* [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Basic%20Math/Finding
|
* [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Basic%20Math/Finding
|
||||||
* the number of digits in a number.md) repository.
|
* the number of digits in a number.md) repository.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <cassert> /// for assert
|
#include <cassert> /// for assert
|
||||||
|
#include <cmath> /// for log calculation
|
||||||
#include <iostream> /// for IO operations
|
#include <iostream> /// for IO operations
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The main function that checks
|
* @brief The main function that checks
|
||||||
* the number of digits in a number.
|
* the number of digits in a number.
|
||||||
|
* TC : O(number of digits)
|
||||||
* @param n the number to check its digits
|
* @param n the number to check its digits
|
||||||
* @returns the digits count
|
* @returns the digits count
|
||||||
*/
|
*/
|
||||||
@ -40,11 +43,34 @@ uint64_t finding_number_of_digits_in_a_number(uint64_t n) {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief This function finds the number of digits
|
||||||
|
* in constant time using logarithmic function
|
||||||
|
* TC: O(1)
|
||||||
|
* @param n the number to check its digits
|
||||||
|
* @returns the digits count
|
||||||
|
*/
|
||||||
|
double finding_number_of_digits_in_a_number_using_log(double n) {
|
||||||
|
// log(0) is undefined
|
||||||
|
if (n == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// to handle the negative numbers
|
||||||
|
if (n < 0) {
|
||||||
|
n = -n;
|
||||||
|
}
|
||||||
|
|
||||||
|
double count = floor(log10(n) + 1);
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Self-test implementations
|
* @brief Self-test implementations
|
||||||
* @returns void
|
* @returns void
|
||||||
*/
|
*/
|
||||||
static void test() {
|
static void first_test() {
|
||||||
assert(finding_number_of_digits_in_a_number(5492) == 4);
|
assert(finding_number_of_digits_in_a_number(5492) == 4);
|
||||||
assert(finding_number_of_digits_in_a_number(-0) == 0);
|
assert(finding_number_of_digits_in_a_number(-0) == 0);
|
||||||
assert(finding_number_of_digits_in_a_number(10000) == 5);
|
assert(finding_number_of_digits_in_a_number(10000) == 5);
|
||||||
@ -52,15 +78,25 @@ static void test() {
|
|||||||
assert(finding_number_of_digits_in_a_number(100000) == 6);
|
assert(finding_number_of_digits_in_a_number(100000) == 6);
|
||||||
assert(finding_number_of_digits_in_a_number(13) == 2);
|
assert(finding_number_of_digits_in_a_number(13) == 2);
|
||||||
assert(finding_number_of_digits_in_a_number(564) == 3);
|
assert(finding_number_of_digits_in_a_number(564) == 3);
|
||||||
|
|
||||||
std::cout << "All tests have successfully passed!\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void second_test() {
|
||||||
|
assert(finding_number_of_digits_in_a_number_using_log(5492) == 4);
|
||||||
|
assert(finding_number_of_digits_in_a_number_using_log(-0) == 0);
|
||||||
|
assert(finding_number_of_digits_in_a_number_using_log(10000) == 5);
|
||||||
|
assert(finding_number_of_digits_in_a_number_using_log(9) == 1);
|
||||||
|
assert(finding_number_of_digits_in_a_number_using_log(100000) == 6);
|
||||||
|
assert(finding_number_of_digits_in_a_number_using_log(13) == 2);
|
||||||
|
assert(finding_number_of_digits_in_a_number_using_log(564) == 3);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @brief Main function
|
* @brief Main function
|
||||||
* @returns 0 on exit
|
* @returns 0 on exit
|
||||||
*/
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
test(); // run self-test implementations
|
// run self-test implementations
|
||||||
|
first_test();
|
||||||
|
second_test();
|
||||||
|
std::cout << "All tests have successfully passed!\n";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user