December 8, 2023 # Belajar Bahasa C – Is a number prime?

Define a function that takes an integer argument and returns a logical value `true` or `false` depending on if the integer is a prime.

Per Wikipedia, a prime number ( or a prime ) is a natural number greater than `1` that has no positive divisors other than `1` and itself.

## Requirements

• You can assume you will be given an integer input.
• You can not assume that the integer will be only positive. You may be given negative numbers as well ( or `0` ).
• NOTE on performance: There are no fancy optimizations required, but still the most trivial solutions might time out. Numbers go up to 2^31 ( or similar, depending on language ). Looping all the way up to `n`, or `n/2`, will be too slow.

## Example

``````is_prime(1)  /* false */
is_prime(2)  /* true  */
is_prime(-1) /* false */``````

## Sample Tests

``````#include <criterion/criterion.h>
#include <stdbool.h>

bool is_prime(int num);

Test(Sample_Test, basic_test)
{
cr_assert_not(is_prime(0), "Your solution returned that 0 is prime, but it's not");
cr_assert_not(is_prime(1), "Your solution returned that 1 is prime, but it's not");
cr_assert(is_prime(2), "Your solution returned that 2 is not prime, but it is");
cr_assert(is_prime(73), "Your solution returned that 73 is not prime, but it is");
cr_assert_not(is_prime(75), "Your solution returned that 75 is prime, but it's not");
cr_assert_not(is_prime(-1), "Your solution returned that -1 is prime, but it's not");
}

Test(Sample_Test, test_prime)
{
cr_assert(is_prime(3), "Your solution returned that 3 is not prime, but it is");
cr_assert(is_prime(5), "Your solution returned that 5 is not prime, but it is");
cr_assert(is_prime(7), "Your solution returned that 7 is not prime, but it is");
cr_assert(is_prime(41), "Your solution returned that 41 is not prime, but it is");
cr_assert(is_prime(5099), "Your solution returned that 5099 is not prime, but it is");
}

Test(Sample_Test, test_not_prime)
{
cr_assert_not(is_prime(4), "Your solution returned that 4 is prime, but it's not");
cr_assert_not(is_prime(6), "Your solution returned that 6 is prime, but it's not");
cr_assert_not(is_prime(8), "Your solution returned that 8 is prime, but it's not");
cr_assert_not(is_prime(9), "Your solution returned that 9 is prime, but it's not");
cr_assert_not(is_prime(45), "Your solution returned that 45 is prime, but it's not");
cr_assert_not(is_prime(-5), "Your solution returned that -5 is prime, but it's not");
cr_assert_not(is_prime(-8), "Your solution returned that -8 is prime, but it's not");
cr_assert_not(is_prime(-41), "Your solution returned that -41 is prime, but it's not");
}

Test(Sample_Test, test_large)
{
cr_assert_not(is_prime(247464361), "Your solution returned that 247464361 is prime, but it's not");
cr_assert(is_prime(1634300119), "Your solution returned that 1634300119 is not prime, but it is");
}``````

## Solution

``````#include <stdbool.h>

bool is_prime(int num)
{
bool prime = true;
int i, k;

if (num <= 1 || (num % 2 == 0 && num != 2))
prime = false;
k = floor(sqrt(num));
for (i = 3; i <= k; i++)
if (num % i == 0)
prime = false;
return prime;
}`````` #### Rajo Intan

Blogger, pemiliki Onestring Lab, menulis artikel terkait teknologi informasi dan pendidikan. Web Developer, berpengalaman lebih dari 20 tahun mengembangkan berbagai aplikasi dan sistem informasi. Kerjasama kontak di onestringlab@gmail.com atau https://forms.gle/xAGKkpi6B3BzJyzk7

View all posts by Rajo Intan → 