- Notifications
You must be signed in to change notification settings - Fork1
A library for arbitrarily large integers📐, written in C
License
adam-mcdaniel/bigint
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This repository implements a big integer library in C. The library supports many operations, such as basic arithmetic, comparison, fast modular exponentiation, prime number detection, square roots, and more.
To use the library, first include it in your C file.
#include"bigint.h"
To create a new big integer, use thebigint
struct. You can initialize a big integer from a string, an integer, or another big integer.
intmain() {// Initialize a big integer from a stringbiginta=bigint_from_string("123456789012345678901234567890");// Initialize a big integer from an integerbigintb=bigint_from_int(1234567890);// Initialize a big integer from another big integerbigintc=bigint_copy(a);bigint_delete(a);bigint_delete(b);bigint_delete(c);return0;}
To perform arithmetic operations on big integers, use the provided functions.
intmain() {// Initialize big integersbiginta=bigint_from_string("123456789012345678901234567890");bigintb=bigint_from_string("987654321098765432109876543210");// Perform arithmetic operationsbigintc=bigint_add(a,b);bigintd=bigint_sub(a,b);biginte=bigint_mul(a,b);bigintf=bigint_div(a,b);bigintg=bigint_mod(a,b);bigint_delete(a);bigint_delete(b);bigint_delete(c);bigint_delete(d);bigint_delete(e);bigint_delete(f);bigint_delete(g);return0;}
To compare big integers:
intmain() {// Initialize big integersbiginta=bigint_from_string("123456789012345678901234567890");bigintb=bigint_from_string("987654321098765432109876543210");// Compare big integersif (bigint_eq(a,b)) {printf("a == b\n"); }elseif (bigint_lt(a,b)) {printf("a < b\n"); }elseif (bigint_gt(a,b)) {printf("a > b\n"); }bigint_delete(a);bigint_delete(b);return0;}
To perform fast modular exponentiation:
intmain() {// Initialize big integersbiginta=bigint_from_string("2");bigintb=bigint_from_string("1234");bigintm=bigint_from_string("10007");// Perform fast modular exponentiationbigintc=bigint_fast_pow(a,b,m);bigint_delete(a);bigint_delete(b);bigint_delete(c);bigint_delete(m);return0;}
To check if a big integer is prime:
intmain() {// Initialize a big integerbiginta=bigint_from_string("123456789012345678901234567890");// Check if the big integer is primeif (bigint_is_prime(a)) {printf("a is prime\n"); }else {printf("a is not prime\n"); }bigint_delete(a);return0;}
To calculate the square root of a big integer:
intmain() {// Initialize a big integerbiginta=bigint_from_string("123412341");// Calculate the square rootbigintb=bigint_sqrt(a);bigint_delete(a);bigint_delete(b);return0;}
To build your program with the big integer library, simply add it to your include path and link against the C standard library.
gcc -I path/to/bigint main.c -o main
To build with CMake, you can use aCMakeLists.txt
file like the following:
cmake_minimum_required(VERSION 3.0)# Create a new projectproject(HelloWorld)# Add an executableadd_executable(HelloWorld main.c)include_directories(path/to/bigint)
Alternatively, you can useFetchContent
to download the big integer repository and include it in your project.
# Import the library from the git repoinclude(FetchContent)FetchContent_Declare( bigint GIT_REPOSITORY https://github.com/adam-mcdaniel/bigint GIT_TAG main)FetchContent_MakeAvailable(bigint)# Include the header only libraryinclude_directories(${bigint_SOURCE_DIR})
This project is licensed under the MIT License - see theLICENSE file for details.
About
A library for arbitrarily large integers📐, written in C