How to resolve the algorithm Cuban primes step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Cuban primes step by step in the C programming language

Table of Contents

Problem Statement

The name   cuban   has nothing to do with   Cuba  (the country),   but has to do with the fact that cubes   (3rd powers)   play a role in its definition.

Cuban primes were named in 1923 by Allan Joseph Champneys Cunningham.

Note that   cuban prime   isn't capitalized   (as it doesn't refer to the nation of Cuba).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Cuban primes step by step in the C programming language

Source Code Explanation

1. Header Files and Definitions

The code includes various header files and defines a PrimeArray structure to store an array of prime numbers. This structure has a pointer to the array (ptr), its size (size), and its capacity (capacity).

2. allocate() Function

This function allocates memory for a new PrimeArray structure, initializes its members, and returns the created structure.

3. deallocate() Function

This function frees the memory allocated for the PrimeArray structure's array pointer and sets the pointer to NULL.

4. push_back() Function

This function adds a prime number to the PrimeArray by reallocating memory if necessary and then storing the prime in the array.

5. main() Function

C Program:

  • Defines constants: cutOff (200), bigUn (100,000), chunks (50), and little (2,000).
  • Initializes a PrimeArray named primes, a counter c, and a flag showEach.
  • Begins a loop to generate Cuban prime numbers (i.e., numbers of the form v = (u + 6)^3).
  • Checks if v is prime using an array of previously found primes, expanding the array as needed.
  • If v is prime, it adds it to the primes array and prints it if showEach is true.
  • Updates counters and flags as v increases.
  • When c (the count of Cuban primes) reaches bigUn, it prints the final result and deallocates the primes array.

GMP Program:

  • Provides an alternative method for finding Cuban prime numbers using the GMP library.
  • Initializes GMP variables a and b and sets a found counter to 0.
  • Enters a loop to generate Cuban prime numbers using GMP functions for exponentiation and primality testing.
  • Prints the first 200, 100,000th, and 1,000,000th Cuban prime numbers.

Source code in the c programming language

#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

typedef long long llong_t;
struct PrimeArray {
    llong_t *ptr;
    size_t size;
    size_t capacity;
};

struct PrimeArray allocate() {
    struct PrimeArray primes;

    primes.size = 0;
    primes.capacity = 10;
    primes.ptr = malloc(primes.capacity * sizeof(llong_t));

    return primes;
}

void deallocate(struct PrimeArray *primes) {
    free(primes->ptr);
    primes->ptr = NULL;
}

void push_back(struct PrimeArray *primes, llong_t p) {
    if (primes->size >= primes->capacity) {
        size_t new_capacity = (3 * primes->capacity) / 2 + 1;
        llong_t *temp = realloc(primes->ptr, new_capacity * sizeof(llong_t));
        if (NULL == temp) {
            fprintf(stderr, "Failed to reallocate the prime array.");
            exit(1);
        } else {
            primes->ptr = temp;
            primes->capacity = new_capacity;
        }
    }

    primes->ptr[primes->size++] = p;
}

int main() {
    const int cutOff = 200, bigUn = 100000, chunks = 50, little = bigUn / chunks;
    struct PrimeArray primes = allocate();
    int c = 0;
    bool showEach = true;
    llong_t u = 0, v = 1, i;

    push_back(&primes, 3);
    push_back(&primes, 5);

    printf("The first %d cuban primes:\n", cutOff);
    for (i = 1; i < LLONG_MAX; ++i) {
        bool found = false;
        llong_t mx = ceil(sqrt(v += (u += 6)));
        llong_t j;

        for (j = 0; j < primes.size; ++j) {
            if (primes.ptr[j] > mx) {
                break;
            }
            if (v % primes.ptr[j] == 0) {
                found = true;
                break;
            }
        }
        if (!found) {
            c += 1;
            if (showEach) {
                llong_t z;
                for (z = primes.ptr[primes.size - 1] + 2; z <= v - 2; z += 2) {
                    bool fnd = false;

                    for (j = 0; j < primes.size; ++j) {
                        if (primes.ptr[j] > mx) {
                            break;
                        }
                        if (z % primes.ptr[j] == 0) {
                            fnd = true;
                            break;
                        }
                    }
                    if (!fnd) {
                        push_back(&primes, z);
                    }
                }
                push_back(&primes, v);
                printf("%11lld", v);
                if (c % 10 == 0) {
                    printf("\n");
                }
                if (c == cutOff) {
                    showEach = false;
                    printf("\nProgress to the %dth cuban prime: ", bigUn);
                }
            }
            if (c % little == 0) {
                printf(".");
                if (c == bigUn) {
                    break;
                }
            }
        }
    }
    printf("\nThe %dth cuban prime is %lld\n", c, v);

    deallocate(&primes);
    return 0;
}


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

typedef unsigned long int uint;

int main(void)
{
	mpz_t a, b;
	mpz_init(a);
	mpz_init(b);

	int found = 0;
	int col = 0;
	for (uint n = 1; ; n++) {
		mpz_ui_pow_ui(a, n, 3);
		mpz_ui_pow_ui(b, n + 1, 3);
		mpz_sub(a, b, a);

		if (!mpz_probab_prime_p(a, 5)) continue;

		if (++found <= 200) {
			gmp_printf("%10Zu", a);
			if (++col == 8) {
				putchar('\n');
				col = 0;
			}
		} else if (found == 100000) {
			gmp_printf("100000th: %Zu\n", a);
		} else if (found == 1000000) {
			gmp_printf("1000000th: %Zu\n", a);
			break;
		}
	}
	return 0;
}


  

You may also check:How to resolve the algorithm Euler's identity step by step in the C# programming language
You may also check:How to resolve the algorithm Sequence of non-squares step by step in the Ol programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Ring programming language
You may also check:How to resolve the algorithm Largest proper divisor of n step by step in the Swift programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the K programming language