Skip to content

Commit 1a13794

Browse files
Merge pull request #593 from shellhub/dev
[doc fix and add test]fix documentation in prime and strong number
2 parents 0b426c0 + 727169a commit 1a13794

File tree

2 files changed

+90
-35
lines changed

2 files changed

+90
-35
lines changed

misc/prime.c

+48-12
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,71 @@
1+
/**
2+
* @file
3+
* @brief Program to identify if a number is [prime
4+
* number](https://en.wikipedia.org/wiki/Prime_number) or not
5+
*/
6+
#include <assert.h>
17
#include <math.h>
8+
#include <stdbool.h>
29
#include <stdio.h>
310

4-
int isPrime(int x)
11+
/**
12+
* Check if a given number is prime number or not
13+
* @param x number to check
14+
* @return `true` if given number is prime number, otherwise `false`
15+
*/
16+
bool isPrime(int x)
517
{
618
if (x == 2)
719
{
8-
return 1;
20+
return true;
921
}
1022
if (x < 2 || x % 2 == 0)
1123
{
12-
return 0;
24+
return false;
1325
}
1426

1527
double squareRoot = sqrt(x);
1628

1729
for (int i = 3; i <= squareRoot; i += 2)
1830
{
1931
if (x % i == 0)
20-
return 0;
32+
{
33+
return false;
34+
}
2135
}
22-
return 1;
36+
return true;
2337
}
2438

39+
/**
40+
* Test function
41+
* @return void
42+
*/
43+
void test()
44+
{
45+
/* all the prime numbers less than 100 */
46+
int primers[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
47+
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
48+
for (size_t i = 0, size = sizeof(primers) / sizeof(primers[0]); i < size;
49+
++i)
50+
{
51+
assert(isPrime(primers[i]));
52+
}
53+
54+
/* Example Non-prime numbers */
55+
int NonPrimers[] = {-1, 0, 1, 4, 6, 8, 9, 10};
56+
for (size_t i = 0, size = sizeof(NonPrimers) / sizeof(NonPrimers[0]);
57+
i < size; ++i)
58+
{
59+
assert(!isPrime(NonPrimers[i]));
60+
}
61+
}
62+
63+
/**
64+
* Driver Code
65+
* @return None
66+
*/
2567
int main()
2668
{
27-
int a;
28-
printf("Input a number to see if it is a prime number:\n");
29-
scanf("%d", &a);
30-
if (isPrime(a))
31-
printf("%d is a prime number.\n", a);
32-
else
33-
printf("%d is not a prime number.\n", a);
69+
test();
3470
return 0;
3571
}

misc/strong_number.c

+42-23
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11
/**
2-
* Modified on 07/12/2017, Kyler Smith
3-
*
4-
* A number is called strong number if sum of the
5-
* factorial of its digit is equal to number itself.
2+
* @file
3+
* @brief Strong number is a number whose sum of all digits’ factorial is equal
4+
* to the number n For example: 145 = 1!(1) + 4!(24) + 5!(120)
65
*/
7-
6+
#include <assert.h>
7+
#include <stdbool.h>
88
#include <stdio.h>
99

10-
void strng(int a)
10+
/**
11+
* Check if given number is strong number or not
12+
* @param number
13+
* @return `true` if given number is strong number, otherwise `false`
14+
*/
15+
bool isStrong(int number)
1116
{
12-
int j = a;
17+
if (number < 0)
18+
{
19+
return false;
20+
}
1321
int sum = 0;
14-
int b, i, fact = 1;
15-
while (a > 0)
22+
int originalNumber = number;
23+
while (originalNumber != 0)
1624
{
17-
fact = 1;
18-
b = a % 10;
19-
for (i = 1; i <= b; i++)
25+
int remainder = originalNumber % 10;
26+
int factorial = remainder == 0 ? 0 : 1; /* 0! == 1 */
27+
28+
/* calculate factorial of n */
29+
for (int i = 1; i <= remainder; factorial *= i, i++)
2030
{
21-
fact = fact * i;
31+
;
2232
}
23-
a = a / 10;
24-
sum = sum + fact;
33+
sum += factorial;
34+
originalNumber /= 10;
2535
}
26-
if (sum == j)
27-
printf("%d is a strong number", j);
28-
else
29-
printf("%d is not a strong number", j);
36+
return number == sum;
37+
}
38+
39+
/**
40+
* Test function
41+
* @return void
42+
*/
43+
void test()
44+
{
45+
assert(isStrong(145)); /* 145 = 1! + 4! + 5! */
46+
assert(!isStrong(543)); /* 543 != 5!+ 4! + 3! */
3047
}
48+
49+
/**
50+
* Driver Code
51+
* @return None
52+
*/
3153
int main()
3254
{
33-
int a;
34-
printf("Enter the number to check");
35-
scanf("%d", &a);
36-
strng(a);
55+
test();
3756
return 0;
3857
}

0 commit comments

Comments
 (0)