How to resolve the algorithm Prime conspiracy step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Prime conspiracy step by step in the C programming language

Table of Contents

Problem Statement

A recent discovery, quoted from   Quantamagazine   (March 13, 2016): and

The task is to check this assertion, modulo 10. Lets call    i -> j   a transition if    i   is the last decimal digit of a prime, and    j   the last decimal digit of the following prime.

Considering the first one million primes.   Count, for any pair of successive primes, the number of transitions    i -> j   and print them along with their relative frequency, sorted by    i . You can see that, for a given    i ,   frequencies are not evenly distributed.

(Modulo 10),   primes whose last digit is   9   "prefer"   the digit   1   to the digit   9,   as its following prime.

Do the same for one hundred million primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Prime conspiracy step by step in the C programming language

The C code provided is a program that generates and analyzes transitions between the last digits of consecutive prime numbers. Here's a detailed explanation of what the code does:

  1. Header Inclusion: The code includes the following standard C libraries:

    • <assert.h>: For assertions.
    • <stdbool.h>: For the bool data type.
    • <stdio.h>: For input/output operations.
  2. Custom Data Structure: The code defines a custom data structure called Transition to represent transitions between last digits of prime numbers:

    typedef unsigned char byte;
    
    struct Transition {
       byte a, b;
       unsigned int c;
    } transitions[100];
    • byte is an unsigned 8-bit integer type.
    • a and b are bytes representing the last digits of two consecutive prime numbers.
    • c is an unsigned integer representing the count of occurrences for the given transition.
  3. Initialization: The init function initializes the transitions array with all possible transitions between last digits of prime numbers up to 9:

    void init() {
       int i, j;
       for (i = 0; i < 10; i++) {
           for (j = 0; j < 10; j++) {
               int idx = i * 10 + j;
               transitions[idx].a = i;
               transitions[idx].b = j;
               transitions[idx].c = 0;
           }
       }
    }
  4. Transition Recording: The record function records a transition between the last digits of two consecutive prime numbers:

    void record(int prev, int curr) {
       byte pd = prev % 10; // Last digit of the previous prime number
       byte cd = curr % 10; // Last digit of the current prime number
       int i;
    
       for (i = 0; i < 100; i++) {
           if (transitions[i].a == pd && transitions[i].b == cd) {
               transitions[i].c++; // Increment the count for the transition
               break;
           }
       }
    }
  5. Transition Analysis: The printTransitions function prints out the transitions with their counts and frequencies:

    void printTransitions(int limit, int last_prime) {
       int i;
    
       printf("%d primes, last prime considered: %d\n", limit, last_prime);
    
       for (i = 0; i < 100; i++) {
           if (transitions[i].c > 0) {
               printf("%d->%d  count: %5d  frequency: %.2f\n",
                      transitions[i].a, transitions[i].b, transitions[i].c,
                      100.0 * transitions[i].c / limit);
           }
       }
    }
  6. Prime Number Checking: The isPrime function checks if a given number is prime:

    bool isPrime(int n) {
       // Handle special cases
       if (n % 2 == 0) return n == 2;
       if (n % 3 == 0) return n == 3;
       if (n % 5 == 0) return n == 5;
       if (n % 7 == 0) return n == 7;
       if (n % 11 == 0) return n == 11;
       if (n % 13 == 0) return n == 13;
       if (n % 17 == 0) return n == 17;
       if (n % 19 == 0) return n == 19;
    
       // General prime checking algorithm
       int s, t, a1, a2;
       t = 23;
       a1 = 96;
       a2 = 216;
       s = t * t;
       while (s <= n) {
           if (n % t == 0) return false;
    
           // First increment
           s += a1;
           t += 2;
           a1 += 24;
    
           if (s <= n) {
               if (n % t == 0) return false;
    
               // Second increment
               s += a2;
               t += 4;
               a2 += 48;
           }
       }
    
       return true;
    }

    The prime checking algorithm uses a combination of pre-processing for small prime numbers and a general prime checking algorithm for larger numbers.

  7. Main Logic: In the main function, the program performs the following steps:

    • Initializes the transitions array.
    • Records the transition from 2 to 3 (the first consecutive prime numbers).
    • Iterates through odd numbers starting from 5 and checks if they are prime:
      • If a prime number is found, it records the transition from the previous prime to the current prime.
      • It also increments the count of prime numbers found.
    • Once the desired limit (LIMIT, which is set to 1000000) of prime numbers is reached, it prints out the transitions and their frequencies.
  8. Output: The program prints out the transitions between last digits of the first LIMIT consecutive prime numbers, along with their counts and frequencies. This output can be used to analyze patterns in the last digits of prime numbers.

Source code in the c programming language

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

typedef unsigned char byte;

struct Transition {
    byte a, b;
    unsigned int c;
} transitions[100];

void init() {
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++) {
            int idx = i * 10 + j;
            transitions[idx].a = i;
            transitions[idx].b = j;
            transitions[idx].c = 0;
        }
    }
}

void record(int prev, int curr) {
    byte pd = prev % 10;
    byte cd = curr % 10;
    int i;

    for (i = 0; i < 100; i++) {
        int z = 0;
        if (transitions[i].a == pd) {
            int t = 0;
            if (transitions[i].b == cd) {
                transitions[i].c++;
                break;
            }
        }
    }
}

void printTransitions(int limit, int last_prime) {
    int i;

    printf("%d primes, last prime considered: %d\n", limit, last_prime);

    for (i = 0; i < 100; i++) {
        if (transitions[i].c > 0) {
            printf("%d->%d  count: %5d  frequency: %.2f\n", transitions[i].a, transitions[i].b, transitions[i].c, 100.0 * transitions[i].c / limit);
        }
    }
}

bool isPrime(int n) {
    int s, t, a1, a2;

    if (n % 2 == 0) return n == 2;
    if (n % 3 == 0) return n == 3;
    if (n % 5 == 0) return n == 5;
    if (n % 7 == 0) return n == 7;
    if (n % 11 == 0) return n == 11;
    if (n % 13 == 0) return n == 13;
    if (n % 17 == 0) return n == 17;
    if (n % 19 == 0) return n == 19;

    // assuming that addition is faster then multiplication
    t = 23;
    a1 = 96;
    a2 = 216;
    s = t * t;
    while (s <= n) {
        if (n % t == 0) return false;

        // first increment
        s += a1;
        t += 2;
        a1 += 24;
        assert(t * t == s);

        if (s <= n) {
            if (n % t == 0) return false;

            // second increment
            s += a2;
            t += 4;
            a2 += 48;
            assert(t * t == s);
        }
    }

    return true;
}

#define LIMIT 1000000
int main() {
    int last_prime = 3, n = 5, count = 2;

    init();
    record(2, 3);

    while (count < LIMIT) {
        if (isPrime(n)) {
            record(last_prime, n);
            last_prime = n;
            count++;
        }
        n += 2;

        if (count < LIMIT) {
            if (isPrime(n)) {
                record(last_prime, n);
                last_prime = n;
                count++;
            }
            n += 4;
        }
    }

    printTransitions(LIMIT, last_prime);

    return 0;
}


  

You may also check:How to resolve the algorithm Mutual recursion step by step in the REXX programming language
You may also check:How to resolve the algorithm Last letter-first letter step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Scilab programming language
You may also check:How to resolve the algorithm Latin Squares in reduced form step by step in the C# programming language
You may also check:How to resolve the algorithm Jewels and stones step by step in the Lambdatalk programming language