/* fact.c basic factorial using gmp */ /* compile: gcc -o fact -I. fact.c -L. -lgmp */ /* assumes libgml.a and gmp.h in this directory */ #include #include "gmp.h" void fact(mpz_t factn, int n) { int i; mpz_t mpzi; mpz_init(mpzi); mpz_set_ui(factn, 1); if(n<=1) return; for(i=2; i<=n; i++) { mpz_set_ui(mpzi, i); mpz_mul(factn, factn, mpzi); } } /* end fact */ int main(int argc, char * argv[]) { int i; mpz_t nf; char my_str[1000]; printf("fact.c using gmp running \n"); mpz_init(nf); /* must init every variable */ for(i=0; i<=52; i++) { fact(nf, i); mpz_get_str(my_str, 10, nf); printf("%d ! = %s \n", i, my_str); } printf("end fact.c using gmp \n"); return 0; }