Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A library for arbitrarily large integers📐, written in C

License

NotificationsYou must be signed in to change notification settings

adam-mcdaniel/bigint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Header Image

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.

Usage

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;}

Building

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

CMake

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})

License

This project is licensed under the MIT License - see theLICENSE file for details.

About

A library for arbitrarily large integers📐, written in C

Topics

Resources

License

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp