How to resolve the algorithm Primality by trial division step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Primality by trial division step by step in the C programming language

Table of Contents

Problem Statement

Write a boolean function that tells whether a given integer is prime.

Remember that   1   and all non-positive numbers are not prime. Use trial division. Even numbers greater than   2   may be eliminated right away. A loop from   3   to   √ n    will suffice,   but other loops are allowed.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Primality by trial division step by step in the C programming language

The C code you provided defines a function called is_prime that checks whether a given unsigned integer is a prime number or not. Here's how the function works:

  1. unsigned int p;: This line declares an unsigned integer variable p that will be used to iterate through potential divisors of the input number n.

  2. if (!(n & 1) || n < 2 ) return n == 2;: This is a conditional statement that checks for the following cases:

    • !(n & 1): This checks if the number n is even. If it is, the expression becomes true because the bitwise AND (&) operation of n with 1 (n & 1) will result in 0 for even numbers.
    • n < 2: This checks if n is less than 2. If either of these conditions is true, the function returns whether n is equal to 2. This is because 2 is the only even prime number, and any number less than 2 is not prime.
  3. for (p = 3; p <= n/p; p += 2): This loop iterates through potential divisors of n starting from 3 and up to the square root of n (n/p). The loop increment is 2 because we are only interested in odd divisors (prime numbers are always odd except for 2).

  4. if (!(n % p)) return 0;: Inside the loop, this conditional statement checks if n is divisible by the current divisor p. If it is, the expression n % p will be 0, and the function returns 0, indicating that n is not a prime number.

  5. If the loop completes without finding any divisors, the function returns 1, indicating that n is a prime number.

In summary, the is_prime function efficiently checks for prime numbers by iterating through potential divisors up to the square root of the input number. It handles special cases like even numbers and numbers less than 2, and it returns 1 if the number is prime and 0 if it's not.

Source code in the c programming language

int is_prime(unsigned int n)
{
	unsigned int p;
	if (!(n & 1) || n < 2 ) return n == 2;

	/* comparing p*p <= n can overflow */
	for (p = 3; p <= n/p; p += 2)
		if (!(n % p)) return 0;
	return 1;
}


  

You may also check:How to resolve the algorithm RSA code step by step in the C programming language
You may also check:How to resolve the algorithm Haversine formula step by step in the C programming language
You may also check:How to resolve the algorithm Ternary logic step by step in the C programming language
You may also check:How to resolve the algorithm Pythagorean triples step by step in the C programming language
You may also check:How to resolve the algorithm Active object step by step in the C programming language