-
Notifications
You must be signed in to change notification settings - Fork 486
Expand file tree
/
Copy path6-is_prime_number.c
More file actions
48 lines (39 loc) · 833 Bytes
/
6-is_prime_number.c
File metadata and controls
48 lines (39 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* File: 6-is_prime_number.c
* Auth: Brennan D Baraban
*/
#include "holberton.h"
int is_divisible(int num, int div);
int is_prime_number(int n);
/**
* is_divisible - Checks if a number is divisible.
* @num: The number to be checked.
* @div: The divisor.
*
* Return: If the number is divisible - 0.
* If the number is not divisible - 1.
*/
int is_divisible(int num, int div)
{
if (num % div == 0)
return (0);
if (div == num / 2)
return (1);
return (is_divisible(num, div + 1));
}
/**
* is_prime_number - Checks if a number is prime.
* @n: The number to be checked.
*
* Return: If the integer is not prime - 0.
* If the number is prime - 1.
*/
int is_prime_number(int n)
{
int div = 2;
if (n <= 1)
return (0);
if (n >= 2 && n <= 3)
return (1);
return (is_divisible(n, div));
}