From c259ac4c3255c7e48d371f376429ed8be84470f0 Mon Sep 17 00:00:00 2001 From: shellhub Date: Tue, 11 Aug 2020 23:01:10 +0800 Subject: [PATCH 1/2] * fix doc * add test --- misc/prime.c | 60 ++++++++++++++++++++++++++++++-------- misc/strong_number.c | 69 +++++++++++++++++++++++++++----------------- 2 files changed, 91 insertions(+), 38 deletions(-) diff --git a/misc/prime.c b/misc/prime.c index 6f88b088..165da574 100644 --- a/misc/prime.c +++ b/misc/prime.c @@ -1,15 +1,27 @@ +/** + * @file + * @brief Program to identify if a number is [prime + * number](https://en.wikipedia.org/wiki/Prime_number) or not + */ +#include #include +#include #include -int isPrime(int x) +/** + * Check if a given number is prime number or not + * @param x number to check + * @return `true` if given number is prime number, otherwise `false` + */ +bool isPrime(int x) { if (x == 2) { - return 1; + return true; } if (x < 2 || x % 2 == 0) { - return 0; + return false; } double squareRoot = sqrt(x); @@ -17,19 +29,43 @@ int isPrime(int x) for (int i = 3; i <= squareRoot; i += 2) { if (x % i == 0) - return 0; + { + return false; + } } - return 1; + return true; } +/** + * Test function + * @return void + */ +void test() +{ + /* all the prime numbers less than 100 */ + int primers[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, + 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; + for (size_t i = 0, size = sizeof(primers) / sizeof(primers[0]); i < size; + ++i) + { + assert(isPrime(primers[i])); + } + + /* Example Non-prime numbers */ + int NonPrimers[] = {-1, 0, 1, 4, 6, 8, 9, 10}; + for (size_t i = 0, size = sizeof(NonPrimers) / sizeof(NonPrimers[0]); + i < size; ++i) + { + assert(!isPrime(NonPrimers[i])); + } +} + +/** + * Driver Code + * @return None + */ int main() { - int a; - printf("Input a number to see if it is a prime number:\n"); - scanf("%d", &a); - if (isPrime(a)) - printf("%d is a prime number.\n", a); - else - printf("%d is not a prime number.\n", a); + test(); return 0; } diff --git a/misc/strong_number.c b/misc/strong_number.c index ddb0d97c..388ea0e9 100644 --- a/misc/strong_number.c +++ b/misc/strong_number.c @@ -1,38 +1,55 @@ /** - * Modified on 07/12/2017, Kyler Smith - * - * A number is called strong number if sum of the - * factorial of its digit is equal to number itself. + * @file + * @brief Strong number is a number whose sum of all digits’ factorial is equal + * to the number n For example: 145 = 1!(1) + 4!(24) + 5!(120) */ - +#include +#include #include -void strng(int a) +/** + * Check if given number is strong number or not + * @param number + * @return `true` if given number is strong number, otherwise `false` + */ +bool isStrong(int number) { - int j = a; - int sum = 0; - int b, i, fact = 1; - while (a > 0) + if (number < 0) { - fact = 1; - b = a % 10; - for (i = 1; i <= b; i++) - { - fact = fact * i; - } - a = a / 10; - sum = sum + fact; + return false; } - if (sum == j) - printf("%d is a strong number", j); - else - printf("%d is not a strong number", j); + int sum = 0; + int originalNumber = number; + while (originalNumber != 0) + { + int remainder = originalNumber % 10; + int factorial = remainder == 0 ? 0 : 1; /* 0! == 1 */ + + /* calculate factorial of n */ + for (int i = 1; i <= remainder; factorial *= i, i++) + ; + sum += factorial; + originalNumber /= 10; + } + return number == sum; } + +/** + * Test function + * @return void + */ +void test() +{ + assert(isStrong(145)); /* 145 = 1! + 4! + 5! */ + assert(!isStrong(543)); /* 543 != 5!+ 4! + 3! */ +} + +/** + * Driver Code + * @return None + */ int main() { - int a; - printf("Enter the number to check"); - scanf("%d", &a); - strng(a); + test(); return 0; } From 727169a13cdb8f2e2debce2b855d14cb1ce67172 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Fri, 14 Aug 2020 00:14:30 +0000 Subject: [PATCH 2/2] clang-tidy fixes for c259ac4c3255c7e48d371f376429ed8be84470f0 --- misc/strong_number.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/misc/strong_number.c b/misc/strong_number.c index 388ea0e9..ee6dae58 100644 --- a/misc/strong_number.c +++ b/misc/strong_number.c @@ -27,7 +27,9 @@ bool isStrong(int number) /* calculate factorial of n */ for (int i = 1; i <= remainder; factorial *= i, i++) + { ; + } sum += factorial; originalNumber /= 10; }