Merge pull request #593 from shellhub/dev

[doc fix and add test]fix documentation in prime and strong number
This commit is contained in:
Du Yuanchao 2020-08-15 08:26:52 +08:00 committed by GitHub
commit 1a13794cfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 93 additions and 38 deletions

View File

@ -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 <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
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;
}

View File

@ -1,38 +1,57 @@
/**
* 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 <assert.h>
#include <stdbool.h>
#include <stdio.h>
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;
}