9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GMPを利用した多倍長計算

Last updated at Posted at 2015-03-02

GNU Multi-Precision Library(GMP)を利用した多倍長計算のサンプルを数回に分けて掲載していきたいと思います。

GNU Multi-Precision Library(GMP)は、多倍長整数など任意の精度の算術ライブラリで、符号付き整数、有理数、浮動小数点数を扱います。
事実上、動作中のハードウェアが持つメモリ容量以外には精度は制限されません。

例えば、GMPを利用した123^45を計算するコードは以下のようになります。

sample.c
# include <gmp.h>

int main(int argc, const char * argv[])
{
    mpz_t n;
    mpz_init(n);

    mpz_ui_pow_ui(n, 123, 45);
    gmp_printf("%Zd\n", n);

    return 0;
}

OS Xを使っている人はHomebrewでインストールすることができます。

$ brew install gmp

コンパイル方法と実行方法は以下のとおりです。

$ gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
$ gcc sample.c -lgmp
$ ./a.out 
11110408185131956285910790587176451918559153212268021823629073199866111001242743283966127048043
9
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?