From 341ed50da8593680c4791b02b1c5908fd52977c3 Mon Sep 17 00:00:00 2001 From: ERR ! <75872316+amino19@users.noreply.github.com> Date: Sat, 4 Sep 2021 01:19:37 +0530 Subject: [PATCH] feat: Finding no. of digits in a Number (#1497) * Finding no. of digits in a Number * Initialize n * Initialize n as int * Changes done * Changes done with codes by adding more comments * Changes done with codes by adding name as md * Modified comments * add void * remove void & update comments * Set some changes to pass Awesome CI Workflow * add return 0 & file name in lower case * Changes done.. * Update finding_number_of_Digits_in_a_Number.cpp * Update finding_number_of_Digits_in_a_Number.cpp * Update finding_number_of_Digits_in_a_Number.cpp * formatting filenames 0ec45e33 * updating DIRECTORY.md * clang-format and clang-tidy fixes for 0ec45e33 * clang-format and clang-tidy fixes for 9c0a437e * updating DIRECTORY.md * Wrote test, needs review * [fix/docs]: Fix tests/code and add documentation Co-authored-by: David Leal Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- DIRECTORY.md | 1 + math/finding_number_of_digits_in_a_number.cpp | 66 +++++++++++++++++++ search/floyd_cycle_detection_algo.cpp | 6 +- sorting/dnf_sort.cpp | 6 +- 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 math/finding_number_of_digits_in_a_number.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 0bd470d74..ec07bbf52 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -170,6 +170,7 @@ * [Fibonacci Large](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/fibonacci_large.cpp) * [Fibonacci Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/fibonacci_matrix_exponentiation.cpp) * [Fibonacci Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/fibonacci_sum.cpp) + * [Finding Number Of Digits In A Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/finding_number_of_digits_in_a_number.cpp) * [Gcd Iterative Euclidean](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/gcd_iterative_euclidean.cpp) * [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/gcd_of_n_numbers.cpp) * [Gcd Recursive Euclidean](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/gcd_recursive_euclidean.cpp) diff --git a/math/finding_number_of_digits_in_a_number.cpp b/math/finding_number_of_digits_in_a_number.cpp new file mode 100644 index 000000000..ecbd76b91 --- /dev/null +++ b/math/finding_number_of_digits_in_a_number.cpp @@ -0,0 +1,66 @@ +/** + * @author [aminos 🇮🇳](https://github.com/amino19) + * @file + * + * @brief [Program to count digits + * in an + * integer](https://www.geeksforgeeks.org/program-count-digits-integer-3-different-methods) + * @details It is a very basic math of finding number of digits in a given + * 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 + * method: by using "K = floor(log10(N) + 1)", but it's only applicable for + * numbers (not integers). + * For more details, refer to the + * [Algorithms-Explanation](https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Basic%20Math/Finding + * the number of digits in a number.md) repository. + */ + +#include /// for assert +#include /// for IO operations + +/** + * @brief The main function that checks + * the number of digits in a number. + * @param n the number to check its digits + * @returns the digits count + */ +uint64_t finding_number_of_digits_in_a_number(uint64_t n) { + uint64_t count = 0; ///< the variable used for the digits count + + // iterate until `n` becomes 0 + // remove last digit from `n` in each iteration + // increase `count` by 1 in each iteration + while (n != 0) { + // we can also use `n = n / 10` + n /= 10; + // each time the loop is running, `count` will be incremented by 1. + ++count; + } + + return count; +} + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + 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(10000) == 5); + assert(finding_number_of_digits_in_a_number(9) == 1); + 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(564) == 3); + + std::cout << "All tests have successfully passed!\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +} diff --git a/search/floyd_cycle_detection_algo.cpp b/search/floyd_cycle_detection_algo.cpp index ac3d62769..c7dd95aea 100644 --- a/search/floyd_cycle_detection_algo.cpp +++ b/search/floyd_cycle_detection_algo.cpp @@ -35,12 +35,14 @@ namespace cycle_detection { */ template int32_t duplicateNumber(const std::vector &in_arr, const uint32_t &n) { - if (n == 0 || n == 1) { // to find duplicate in an array its size should be atleast 2 + if (n == 0 || + n == 1) { // to find duplicate in an array its size should be atleast 2 return -1; } uint32_t tortoise = in_arr[0]; // variable tortoise is used for the longer // jumps in the array - uint32_t hare = in_arr[0]; // variable hare is used for shorter jumps in the array + uint32_t hare = + in_arr[0]; // variable hare is used for shorter jumps in the array do { tortoise = in_arr[tortoise]; hare = in_arr[in_arr[hare]]; diff --git a/sorting/dnf_sort.cpp b/sorting/dnf_sort.cpp index 0b13cffbe..8b7c2279e 100644 --- a/sorting/dnf_sort.cpp +++ b/sorting/dnf_sort.cpp @@ -1,7 +1,8 @@ /** * @file * @brief Implementation of the [DNF - * sort](https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/) implementation + * sort](https://www.geeksforgeeks.org/sort-an-array-of-0s-1s-and-2s/) + * implementation * @details * C++ program to sort an array with 0, 1 and 2 in a single pass(DNF sort). * Since one traversal of the array is there hence it works in O(n) time @@ -22,7 +23,8 @@ namespace sorting { /** * @namespace dnf_sort * @brief Functions for the [DNF - * sort](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) implementation + * sort](https://en.wikipedia.org/wiki/Dutch_national_flag_problem) + * implementation */ namespace dnf_sort { /**