Skip to content

Instantly share code, notes, and snippets.

@jgrar
Created August 4, 2012 04:24
Show Gist options
  • Save jgrar/3254501 to your computer and use it in GitHub Desktop.
Save jgrar/3254501 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
int main (int argc, char *argv[])
{
mpz_t n, x, f;
if (argc < 2)
exit(EXIT_FAILURE);
mpz_init_set_ui(x, 1);
mpz_init_set_ui(f, 1);
mpz_init_set_str(n, argv[1], 10);
while (mpz_cmp(x, n))
{
mpz_mul(f, f, x);
mpz_add_ui(x, x, 1);
}
mpz_set_ui(n, 0);
while (mpz_cmp_ui(f, 0) > 0)
{
mpz_mod_ui(x, f, 10);
mpz_add(n, x, n);
mpz_div_ui(f, f, 10);
}
gmp_printf("%Zu\n", n);
mpz_clear(n);
mpz_clear(x);
mpz_clear(f);
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment