How to resolve the algorithm De Polignac numbers step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm De Polignac numbers step by step in the C programming language

Table of Contents

Problem Statement

Alphonse de Polignac, a French mathematician in the 1800s, conjectured that every positive odd integer could be formed from the sum of a power of 2 and a prime number. He was subsequently proved incorrect. The numbers that fail this condition are now known as de Polignac numbers. Technically 1 is a de Polignac number, as there is no prime and power of 2 that sum to 1. De Polignac was aware but thought that 1 was a special case. However. 127 is also fails that condition, as there is no prime and power of 2 that sum to 127. As it turns out, de Polignac numbers are not uncommon, in fact, there are an infinite number of them.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm De Polignac numbers step by step in the C programming language

The provided C code is designed to find and display De Polignac numbers, which are prime numbers that can be expressed as the difference between a prime number and a power of 2. The code accomplishes this using the following steps:

  1. isPrime Function: It determines if a given number is prime or not.

  2. Main Function:

    • Initialize variables and data structures for computations and storage.
    • Generate an array of powers of 2 (pows2).
    • Initialize the first element of the De Polignac numbers array (dp) to 1.
  3. Loop for De Polignac Number Search:

    • Iterate through odd numbers starting from 3.
    • For each odd number, check if it can be represented as (prime - power of 2).
    • If such a representation is found, it's not a De Polignac number. Otherwise, it is a De Polignac number.
  4. Counting and Storing De Polignac Numbers:

    • Keep track of the count of De Polignac numbers encountered.
    • Store the first 50 De Polignac numbers in the array dp.
    • Record the 1000th and 10000th De Polignac numbers in dp1000 and dp10000, respectively.
  5. Printing Results:

    • Display the first 50 De Polignac numbers in groups of 10 per line.
    • Print the 1000th and 10000th De Polignac numbers.

In summary, this code finds and displays De Polignac numbers up to the 10000th one, providing insights into the distribution and properties of these remarkable numbers.

Source code in the c programming language

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

bool isPrime(int n) {
    if (n < 2) return false;
    if (n%2 == 0) return n == 2;
    if (n%3 == 0) return n == 3;
    int d = 5;
    while (d*d <= n) {
        if (n%d == 0) return false;
        d += 2;
        if (n%d == 0) return false;
        d += 4;
    }
    return true;
}

int main() {
    int i, j, n, pows2[20], dp[50], dpc = 1, dp1000, dp10000, count, pow;
    bool found;
    for (i = 0; i < 20; ++i) pows2[i] = 1 << i;
    dp[0] = 1;
    for (n = 3, count = 1; count < 10000; n += 2) {
        found = false;
        for (j = 0; j < 20; ++j) {
            pow = pows2[j];
            if (pow > n) break;
            if (isPrime(n-pow)) {
                found = true;
                break;
            }
        }
        if (!found) {
            ++count;
            if (count <= 50) {
                dp[dpc++] = n;
            } else if (count == 1000) {
                dp1000 = n;
            } else if (count == 10000) {
                dp10000 = n;
            }
        }
    }
    setlocale(LC_NUMERIC, "");
    printf("First 50 De Polignac numbers:\n");
    for (i = 0; i < dpc; ++i) {
        printf("%'5d ", dp[i]);
        if (!((i+1)%10)) printf("\n");
    }
    printf("\nOne thousandth: %'d\n", dp1000);
    printf("\nTen thousandth: %'d\n", dp10000);
    return 0;
}


  

You may also check:How to resolve the algorithm Sorting algorithms/Stooge sort step by step in the Raku programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Objeck programming language
You may also check:How to resolve the algorithm Compiler/lexical analyzer step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Repeat step by step in the VBA programming language
You may also check:How to resolve the algorithm Binary search step by step in the UNIX Shell programming language