How to resolve the algorithm Summarize primes step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Summarize primes step by step in the C programming language

Table of Contents

Problem Statement

Considering in order of length, n, all sequences of consecutive primes, p, from 2 onwards, where p < 1000 and n>0, select those sequences whose sum is prime, and for these display the length of the sequence, the last item in the sequence, and the sum.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Summarize primes step by step in the C programming language

This C program finds and prints the sum of prime numbers in a given range that are also prime numbers. Here's a detailed explanation:

  1. Header Inclusions:

    • The program includes the <stdbool.h> and <stdio.h> header files for boolean operations and input/output.
  2. Function is_prime:

    • This function takes an integer n as input and checks if it's a prime number.
    • It starts by handling special cases where n is less than 2 or is divisible by 2 or 3.
    • Then, it uses a while loop to check for divisibility by numbers greater than 3 and up to the square root of n.
    • If any prime factor is found, it returns false. Otherwise, it returns true.
  3. Main Function:

    • const int start = 1; and const int stop = 1000;: These are constants defining the starting and ending values for the search range of prime numbers.
    • int sum = 0;, int count = 0;, int sc = 0;, and int p;: These variables are used to keep track of the sum of prime numbers (sum), the count of prime numbers (count), the count of prime sums (sc), and the current number p being checked.
    • It enters a loop that iterates through numbers from start to stop-1.
    • For each number p, it checks if it's prime using the is_prime function. If p is prime, it increments the count and adds p to the sum.
    • Then, it checks if the current sum is also prime. If it is, it prints the details (count, p, sum) and increments the sc count.
    • After the loop completes, the program prints the total count of prime sums found in the given range.
  4. Output:

    • The program prints the sum of prime numbers in the given range that are also prime numbers. For example:

      The sum of   4 primes in [2,  29] is   41 which is also prime
      The sum of   9 primes in [2,  59] is  149 which is also prime
      The sum of   10 primes in [2,  61] is  167 which is also prime
      The sum of   12 primes in [2,  71] is  229 which is also prime
      The sum of   15 primes in [2,  79] is  271 which is also prime
      The sum of   17 primes in [2,  97] is  337 which is also prime
      There are 6 summerized primes in [1, 1000)
      

Source code in the c programming language

#include <stdbool.h>
#include <stdio.h>

bool is_prime(int n) {
    int i = 5;

    if (n < 2) {
        return false;
    }

    if (n % 2 == 0) {
        return n == 2;
    }
    if (n % 3 == 0) {
        return n == 3;
    }

    while (i * i <= n) {
        if (n % i == 0) {
            return false;
        }
        i += 2;

        if (n % i == 0) {
            return false;
        }
        i += 4;
    }

    return true;
}

int main() {
    const int start = 1;
    const int stop = 1000;

    int sum = 0;
    int count = 0;
    int sc = 0;
    int p;

    for (p = start; p < stop; p++) {
        if (is_prime(p)) {
            count++;
            sum += p;
            if (is_prime(sum)) {
                printf("The sum of %3d primes in [2, %3d] is %5d which is also prime\n", count, p, sum);
                sc++;
            }
        }
    }
    printf("There are %d summerized primes in [%d, %d)\n", sc, start, stop);

    return 0;
}


  

You may also check:How to resolve the algorithm Sorting algorithms/Bubble sort step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Compiler/AST interpreter step by step in the Scheme programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Groovy programming language
You may also check:How to resolve the algorithm File input/output step by step in the J programming language