How to resolve the algorithm Sequence of primes 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 Sequence of primes by trial division step by step in the C programming language

Table of Contents

Problem Statement

Generate a sequence of primes by means of trial division.

Trial division is an algorithm where a candidate number is tested for being a prime by trying to divide it by other numbers. You may use primes, or any numbers of your choosing, as long as the result is indeed a sequence of primes. The sequence may be bounded (i.e. up to some limit), unbounded, starting from the start (i.e. 2) or above some given value. Organize your function as you wish, in particular, it might resemble a filtering operation, or a sieving operation. If you want to use a ready-made is_prime function, use one from the Primality by trial division page (i.e., add yours there if it isn't there already).

Let's start with the solution:

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

This C program finds prime numbers within a specified range and calculates their sum. A step-by-step breakdown below:

  1. Header File:

    • #include <stdio.h>: Includes the standard input-output library for input and output operations.
  2. isPrime Function:

    • unsigned int isPrime(unsigned int n): This function checks if an input number n is prime.
    • It returns 1 if n is prime, and 0 otherwise.
    • If n is less than 2 or even (apart from 2), it returns 0.
    • It iterates through odd numbers up to the square root of n and checks if n is divisible by any of these numbers. If it finds a divisor, it returns 0; otherwise, it returns 1.
  3. main Function:

    • It starts by declaring necessary variables:
      • unsigned int l, u, i, sum = 0;: These variables will be used for the range and sum of prime numbers.
    • It prompts the user to input a range using scanf("%ld%ld", &l, &u). l represents the lower bound, and u represents the upper bound.
    • It enters a loop from i = l to u to check for prime numbers.
    • For each value of i, it calls the isPrime function to check if i is prime. If isPrime(i) returns 1, it means i is prime.
    • If i is prime, it prints the prime number and increments the sum variable.
    • Finally, it displays the count of prime numbers found within the given range and their sum.

Source code in the c programming language

#include<stdio.h>

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

	for (num = 3; num <= n/num; num += 2)
		if (!(n % num)) 
			return 0;
	return 1;
}

int main()
{
	unsigned int l,u,i,sum=0;
	
	printf("Enter lower and upper bounds: ");
	scanf("%ld%ld",&l,&u);
	
	for(i=l;i<=u;i++){
		if(isPrime(i)==1)
			{
				printf("\n%ld",i);
				sum++;
			}
	}
	
	printf("\n\nPrime numbers found in [%ld,%ld] : %ld",l,u,sum);
	
	return 0;
}


  

You may also check:How to resolve the algorithm ABC problem step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Introspection step by step in the Racket programming language
You may also check:How to resolve the algorithm Composite numbers k with no single digit factors whose factors are all substrings of k step by step in the Arturo programming language