How to resolve the algorithm Quad-power prime seeds step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Quad-power prime seeds step by step in the C programming language

Table of Contents

Problem Statement

Generate the sequence of quad-power prime seeds: positive integers n such that:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Quad-power prime seeds step by step in the C programming language

The provided C code identifies and prints a list of the first 50 "quad-power prime seeds" and the first such seed greater than each power of 10 from 1 million to 10 million.

Core Concepts:

  • GMP Library: The code uses the GNU Multiple Precision Arithmetic Library (GMP) to handle large integer computations.

Function isQuadPowerPrimeSeed:

  • Checks if a given unsigned integer n is a "quad-power prime seed."
  • Quad-power prime seeds are positive integers such that:
    • (n+1)^{n^2} and (n^4+1)^{n^3} are both probable prime numbers.
  • The function repeatedly multiplies n by itself and checks the primality of the resulting expressions.

Function ord:

  • Converts a single-digit integer (0-9) to its ordinal suffix (e.g., "st" for 1, "th" for 2-9).

Main Function:

  • Initializes the GMP variables p and p2.
  • Sets the locale for numeric formatting to use appropriate thousands separators.

Printing the First 50 Quad-Power Prime Seeds:

  • The program iterates through positive integers starting from 1.
  • For each integer n, it calls isQuadPowerPrimeSeed to check if it's a quad-power prime seed.
  • If the condition is met, the program prints n with a consistent width and increments a counter c.
  • After every 10 printed seeds, it prints a new line.

Printing the First Quad-Power Prime Seed Greater Than Each Power of 10:

  • The program increments n and checks the primality condition again.
  • If the condition is met, it increments a counter c and compares n to each power of 10 from 1 million to 10 million.
  • Once n exceeds a power of 10, it prints the value of m (representing the power of 10), c (the ordinal seed number), and n in a formatted string.
  • The loop continues until m reaches 11 (10 million).

Output:

The output of the program resembles the following:

First fifty quad-power prime seeds:
       5          19          49          83          115         125         151
       183         209         233         253         269         281         299
       319         341         365         391         415         437         451
       473         485         509         527         547         569         581
       599         623         643         665         685         701         713

First quad-power prime seed greater than:
1 million is the 12th:  1,006,049
2 million is the 13th:  2,023,973
3 million is the 14th:  3,058,593
4 million is the 15th:  4,118,437
5 million is the 16th:  5,202,533
6 million is the 17th:  6,310,869
7 million is the 18th:  7,443,405
8 million is the 19th:  8,599,977
9 million is the 20th:  9,779,437
10 million is the 21st: 10,981,653

Source code in the c programming language

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

mpz_t p, p2;

bool isQuadPowerPrimeSeed(unsigned int n) {
    int i;
    mpz_set_ui(p, n);
    unsigned int k = n + 1;
    mpz_add_ui(p2, p, k);
    if (!mpz_probab_prime_p(p2, 15)) return false;
    for (i = 0; i < 3; ++i) {
        mpz_mul_ui(p, p, n);
        mpz_set(p2, p);
        mpz_add_ui(p2, p2, k);
        if (!mpz_probab_prime_p(p2, 15)) return false;
    }
    return true;
}

const char *ord(int c) {
    int m = c % 100;
    if (m >= 4 && m <= 20) return "th";
    m %= 10;
    return (m == 1) ? "st" :
           (m == 2) ? "nd" :
           (m == 3) ? "rd" : "th";
}

int main() {
    unsigned int n;
    int c = 0, m = 1;
    mpz_init(p);
    mpz_init(p2);
    setlocale(LC_NUMERIC, "");
    printf("First fifty quad-power prime seeds:\n");
    for (n = 1; c < 50; ++n) {
        if (isQuadPowerPrimeSeed(n)) {
            printf("%'7u  ", n);
            if (!((++c) % 10)) printf("\n");
        }
    }

    printf("\nFirst quad-power prime seed greater than:\n");
    while (1) {
        if (isQuadPowerPrimeSeed(n)) {
            ++c;
            if (n > 1000000 * m) {
                printf(" %2d million is the %d%s: %'10u\n", m, c, ord(c), n);
                if (++m == 11) break;
            }
        }
        ++n;
    }
    return 0;
}


  

You may also check:How to resolve the algorithm Rate counter step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Set right-adjacent bits step by step in the Ada programming language
You may also check:How to resolve the algorithm Function definition step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Two bullet roulette step by step in the 11l programming language
You may also check:How to resolve the algorithm Comments step by step in the EasyLang programming language