How to resolve the algorithm Chernick's Carmichael numbers step by step in the C programming language
How to resolve the algorithm Chernick's Carmichael numbers step by step in the C programming language
Table of Contents
Problem Statement
In 1939, Jack Chernick proved that, for n ≥ 3 and m ≥ 1: is a Carmichael number if all the factors are primes and, for n > 4, m is a multiple of 2^(n-4).
For n = 5, the smallest number m that satisfy Chernick's conditions, is m = 380, therefore U(5, 380) is the smallest Chernick's Carmichael number with 5 prime factors. U(5, 380) is a Chernick's Carmichael number because m = 380 is a multiple of 2^(n-4), where n = 5, and the factors { (6380 + 1), (12380 + 1), (18380 + 1), (36380 + 1), (72*380 + 1) } are all prime numbers.
For n ≥ 3, let a(n) be the smallest Chernick's Carmichael number with n prime factors.
Note: it's perfectly acceptable to show the terms in factorized form:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Chernick's Carmichael numbers step by step in the C programming language
This C program is designed to find the values of m
that satisfy the Chernick conditions for a given value of n
. Chernick's conditions are a set of divisibility and primality tests used to determine the existence of certain types of prime numbers.
Here's a detailed breakdown of the code:
-
Header Files:
- The program includes the necessary header files:
<stdio.h>
: For input and output functions.<stdlib.h>
: For theexit
function.<gmp.h>
: For the GMP (GNU Multiple Precision) library, which provides functions for handling large integer arithmetic.
- The program includes the necessary header files:
-
Type Definitions:
typedef unsigned long long int u64;
: Defines a new data type aliasu64
for representing unsigned 64-bit integers.
-
Constants:
TRUE
andFALSE
are defined as 1 and 0, respectively, for logical comparisons.
-
Functions:
primality_pretest
: Performs a quick primality test on a given integerk
. It checks divisibility by small primes (3, 5, 7, 11, 13, 17, 19, and 23) and returnsTRUE
ifk
is not divisible by any of them. Otherwise, it returnsFALSE
.probprime
: Uses thempz_probab_prime_p
function from the GMP library to perform a probabilistic primality test onk
. It setsn
to the value ofk
usingmpz_set_ui
and then invokes the probabilistic primality test. The function returnsTRUE
ifk
is probably prime andFALSE
otherwise.is_chernick
: Checks if a givenm
satisfies the Chernick conditions for a givenn
. It performs the following steps:- Calculates
t = 9*m
and performs the primality pretest on6*m+1
and12*m+1
. If either of these is not a prime, it returnsFALSE
. - Iterates over values of
i
from 1 ton-2
and performs primality pretests on(t << i) + 1
. If any of these is not a prime, it returnsFALSE
. - Uses
mpz_probab_prime_p
to perform probabilistic primality tests on6*m+1
,12*m+1
, and(t << i) + 1
fori
from 1 ton-2
. If any of these is not a probable prime, it returnsFALSE
. - If all the primality tests pass, it returns
TRUE
.
- Calculates
main
:- Initializes an MPZ (multiple precision integer) variable
z
usingmpz_inits
. - Iterates over values of
n
from 3 to 10. - Calculates the multiplier value based on
n
. - Iterates over values of
k
until it finds anm
that satisfies the Chernick conditions for the givenn
. - Once
m
is found, it prints the result asa(n) has m = <value of m>
. - Finally, it returns 0 to indicate successful program termination.
- Initializes an MPZ (multiple precision integer) variable
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
typedef unsigned long long int u64;
#define TRUE 1
#define FALSE 0
int primality_pretest(u64 k) {
if (!(k % 3) || !(k % 5) || !(k % 7) || !(k % 11) || !(k % 13) || !(k % 17) || !(k % 19) || !(k % 23)) return (k <= 23);
return TRUE;
}
int probprime(u64 k, mpz_t n) {
mpz_set_ui(n, k);
return mpz_probab_prime_p(n, 0);
}
int is_chernick(int n, u64 m, mpz_t z) {
u64 t = 9 * m;
if (primality_pretest(6 * m + 1) == FALSE) return FALSE;
if (primality_pretest(12 * m + 1) == FALSE) return FALSE;
for (int i = 1; i <= n - 2; i++) if (primality_pretest((t << i) + 1) == FALSE) return FALSE;
if (probprime(6 * m + 1, z) == FALSE) return FALSE;
if (probprime(12 * m + 1, z) == FALSE) return FALSE;
for (int i = 1; i <= n - 2; i++) if (probprime((t << i) + 1, z) == FALSE) return FALSE;
return TRUE;
}
int main(int argc, char const *argv[]) {
mpz_t z;
mpz_inits(z, NULL);
for (int n = 3; n <= 10; n ++) {
u64 multiplier = (n > 4) ? (1 << (n - 4)) : 1;
if (n > 5) multiplier *= 5;
for (u64 k = 1; ; k++) {
u64 m = k * multiplier;
if (is_chernick(n, m, z) == TRUE) {
printf("a(%d) has m = %llu\n", n, m);
break;
}
}
}
return 0;
}
You may also check:How to resolve the algorithm Matrix-exponentiation operator step by step in the Julia programming language
You may also check:How to resolve the algorithm Non-decimal radices/Output step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Cheryl's birthday step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Algebraic data types step by step in the C++ programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the Wren programming language