Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Part of the SPBU's programming course. C library which provides a set of basic arithmetic operations on arbitrary-precision integers

License

Notifications You must be signed in to change notification settings

artem-burashnikov/big-int-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

big-int-lib

Build status MIT License

Overview

The repository provides a C library which implements a set of basic arithmetic operations on arbitrary-precision integers.

Project structure

Below is the default project structure.

big-int-lib/
├── big-int-lib
│   └── (source files)
│   
├── tests
│   └── (test files)
│
├── CMakeLists.txt
├── LICENSE
└── README.md

Source files have self explanatory names.

Features

Biginteger structure can be initialized from and converted to a string and integers.

Digits are stored in little-endian order.

Make sure the integer type can hold a given bigint before converting to integer.

  • Basic arithmetic operations:
    • Addition +
    • Subtraction -
    • Multiplication *
    • Integer division /
    • Modulo division %

All operations return a new bigint structure.

Table of contents

Getting Started

Prerequisites

  • CMake (version 3.22.1 or higher)
  • GCC

Building

Open the terminal and follow these steps:

  1. Clone the repository:

    git clone git@github.com:artem-burashnikov/big-int-lib.git
  2. Navigate to the project root:

    cd big-int-lib
  3. Generate Makefile using CMake:

    mkdir build && cd build;
    cmake ..
  4. Build a static library:

    cmake --build . --clean-first
  5. Now you can include a bigint.h header file in your source files and link a static library libbigint.a during a compilation.

Usage

Below are ways you can utilize functions in this library.

Initialize and free a bigint

#include "bigint.h"
/* Initialization. */
bigint_t *a = bigint_from_str("2236913125512124108572352");
bigint_t *b = bigint_from_int(3236);

/* Clean up. */
bifree(a);
bifree(b);

Perform arithmetic operations

#include "bigint.h"

bigint_t *a_sum_b = bigint_sum(a, b);
bigint_t *a_sub_b = bigint_sub(a, b);
bigint_t *a_mul_b = bigint_mul(a, b);
bigint_t *a_div_b = bigint_div(a, b);
bigint_t *a_mod_b = bigint_mod(a, b);

/* Clean up. */
bifree(a);
bifree(b);
bifree(a_sum_b);
bifree(a_sub_b);
bifree(a_mul_b);
bifree(a_div_b);
bifree(a_mod_b);

Examples

For example, you can calculate 1000! using functions defined in this library.

bigint_t *factorial(const unsigned int n) {
  bigint_t *resp, *tmp, *factor;
  unsigned int p;

  resp = tmp = factor = NULL;

  resp = bigint_from_int(1);

  if ((n == 0) || (n == 1)) {
    return resp;
  }

  for (p = 2; p <= n; ++p) {
    factor = bigint_from_int(p);
    tmp = bigint_mul(resp, factor);
    bifree(factor);
    bifree(resp);
    resp = tmp;
  }

  return resp;
}

bigint_t factorial_1000 = factorial(1000);
char *str = bigint_to_str(factorial_1000);

/* str now holds all digits of 1000! in little-endian order. */

bifree(factorial_1000);
free(str);

Licenses

The project is licensed under an MIT License.

About

Part of the SPBU's programming course. C library which provides a set of basic arithmetic operations on arbitrary-precision integers

Topics

Resources

License

Stars

Watchers

Forks