Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci.
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

The GMP library

Links:

D has a built-in BigInt type, but it's slow if you want to work with really huge numbers. "Performance is optimized for numbers below ~1000 decimal digits." And if you want to test such huge numbers for primality, you need an efficient solution.

First, let's see how to use the GMP library in C.

Under Manjaro, I installed the package "gmp" with the pacman package manager. This way, gmp.h and libgmp.so become available.

(1) C example
#include <gmp.h>
#include <stdio.h>
#include <string.h>

/**
 * Check if a GMP integer is prime
 * @param n: the number to test
 * @return: true if prime, false if composite
 */

bool isPrime(const mpz_t n)
{
    int result = mpz_probab_prime_p(n, 25);
    return result > 0; // Returns true for both "probably prime" and "definitely prime"
}

/**
 * Check if a string representation of a number is prime
 * @param str: string representation of the number
 * @return: true if prime, false if composite or invalid
 */

bool isPrimeStr(const char *str)
{
    mpz_t n;
    mpz_init(n);

    // Try to set the number from string
    if (mpz_set_str(n, str, 10) != 0)
    {
        mpz_clear(n);
        return false; // Invalid number format
    }

    bool result = isPrime(n);
    mpz_clear(n);
    return result;
}

/**
 * Check if a regular integer is prime (for convenience)
 * @param num: the number to test
 * @return: true if prime, false if composite
 */

bool isPrimeInt(unsigned long num)
{
    mpz_t n;
    mpz_init_set_ui(n, num);
    bool result = isPrime(n);
    mpz_clear(n);
    return result;
}

int main()
{
    mpz_t big_number;
    char input[1000];

    mpz_init(big_number);

    // Test with small numbers
    printf("Testing small numbers:\n");
    for (int i = 2; i <= 20; i++)
    {
        printf("%d is %s\n", i, isPrimeInt(i) ? "prime" : "composite");
    }

    // Test with large numbers
    printf("\nTesting large numbers:\n");

    // Test Mersenne prime 2^127 - 1
    mpz_ui_pow_ui(big_number, 2, 127);
    mpz_sub_ui(big_number, big_number, 1);
    printf("2^127 - 1 is %s\n", isPrime(big_number) ? "prime" : "composite");

    // Test from string input
    printf("\nEnter a large number to test: ");
    if (fgets(input, sizeof(input), stdin) != NULL)
    {
        input[strcspn(input, "\n")] = 0; // Remove newline
        printf("%s is %s\n", input, isPrimeStr(input) ? "prime" : "composite");
    }

    mpz_clear(big_number);
    return 0;
}

Compilation:

gcc main.c -lgmp
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 August 31, 17:42