Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created May 29, 2009 21:46
Show Gist options
  • Save tebeka/120224 to your computer and use it in GitHub Desktop.
Save tebeka/120224 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <gc/gc.h>
mpz_t *
fact(unsigned n)
{
mpz_t *res;
res = (mpz_t *)GC_MALLOC(sizeof(mpz_t));
mpz_init_set_ui(*res, 1);
while (n > 1) {
mpz_mul_ui(*res, *res, n);
--n;
}
return res;
}
int
main(int argc, char *argv[])
{
mpz_t *f;
unsigned n;
GC_INIT();
if (argc != 2) {
fprintf(stderr, "usage: fact NUMBER\n");
return 1;
}
n = atol(argv[1]);
f = fact(n);
gmp_printf("%Zd\n", *f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment