How to resolve the algorithm Blum integer step by step in the C programming language
How to resolve the algorithm Blum integer step by step in the C programming language
Table of Contents
Problem Statement
A positive integer n is a Blum integer if n = p x q is a semi-prime for which p and q are distinct primes congruent to 3 mod 4. In other words, p and q must be of the form 4t + 3 where t is some non-negative integer.
21 is a Blum integer because it has two prime factors: 3 (= 4 x 0 + 3) and 7 (= 4 x 1 + 3). Find and show on this page the first 50 Blum integers. Also show the 26,828th.
Find and show the 100,000th, 200,000th, 300,000th and 400,000th Blum integers. For the first 400,000 Blum integers, show the percentage distribution by final decimal digit (to 3 decimal places). Clearly, such integers can only end in 1, 3, 7 or 9.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Blum integer step by step in the C programming language
This C program generates and analyzes Blum integers, which are a class of integers with special properties in number theory. Here's a detailed explanation of the code:
Header Inclusions:
<stdio.h>
: For input and output operations.<stdbool.h>
: For thebool
data type (boolean values).<locale.h>
: For setting the locale to use localized numeric formatting (e.g., commas as thousand separators).
Global Variables:
inc[8]
: An array to help in the efficient generation of prime numbers (used infirstPrimeFactor
).
Function Declarations:
isPrime(int n)
: Checks if the given integern
is prime.firstPrimeFactor(int n)
: Finds and returns the smallest prime factor of the given odd integern
.
Main Function:
i
: Main loop counter, starts from 1.j
: Loop counter for various purposes.bc
: Counter for Blum integers found.p
,q
: Variables to hold prime factors.blum[50]
: Array to store the first 50 Blum integers.counts[4]
: Array to count the frequency of Blum integers ending in 1, 3, 5, and 7.digits[4]
: Array of digits 1, 3, 5, and 7 to correspond withcounts
.
Main Loop:
- The program enters an infinite loop (
while (true)
) to continuously search for Blum integers. - It calls
firstPrimeFactor
on the currenti
to find its smallest prime factorp
. - If
p
is congruent to 3 modulo 4 (p % 4 == 3
), it calculatesq
asi / p
. - If both
q
andi
are congruent to 3 modulo 4 andq
is prime, it has found a Blum integer. - It stores the Blum integer in the
blum
array and increments the corresponding count incounts
. - The program prints various status messages and updates based on certain milestones:
- When 50 Blum integers have been found, it prints the first 50.
- When the 26,828th or any subsequent 100,000th Blum integer is found, it prints the number of Blum integers found so far.
- When the 400,000th Blum integer is found, it prints the percentage distribution of the first 400,000 Blum integers ending in 1, 3, 5, or 7.
- The loop continues indefinitely, searching for and analyzing Blum integers.
- The program runs indefinitely until you terminate it.
Source code in the c programming language
#include <stdio.h>
#include <stdbool.h>
#include <locale.h>
int inc[8] = {4, 2, 4, 2, 4, 6, 2, 6};
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;
}
// Assumes n is odd.
int firstPrimeFactor(int n) {
if (n == 1) return 1;
if (!(n%3)) return 3;
if (!(n%5)) return 5;
for (int k = 7, i = 0; k*k <= n; ) {
if (!(n%k)) {
return k;
} else {
k += inc[i];
i = (i + 1) % 8;
}
}
return n;
}
int main() {
int i = 1, j, bc = 0, p, q;
int blum[50], counts[4] = {0}, digits[4] = {1, 3, 5, 7};
setlocale(LC_NUMERIC, "");
while (true) {
p = firstPrimeFactor(i);
if (p % 4 == 3) {
q = i / p;
if (q != p && q % 4 == 3 && isPrime(q)) {
if (bc < 50) blum[bc] = i;
++counts[i % 10 / 3];
++bc;
if (bc == 50) {
printf("First 50 Blum integers:\n");
for (j = 0; j < 50; ++j) {
printf("%3d ", blum[j]);
if (!((j+1) % 10)) printf("\n");
}
printf("\n");
} else if (bc == 26828 || !(bc % 100000)) {
printf("The %'7dth Blum integer is: %'9d\n", bc, i);
if (bc == 400000) {
printf("\n%% distribution of the first 400,000 Blum integers:\n");
for (j = 0; j < 4; ++j) {
printf(" %6.3f%% end in %d\n", counts[j]/4000.0, digits[j]);
}
break;
}
}
}
}
i += (i % 5 == 3) ? 4 : 2;
}
return 0;
}
You may also check:How to resolve the algorithm Sort disjoint sublist step by step in the 11l programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Brownian tree step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Empty string step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Split a character string based on change of character step by step in the Common Lisp programming language