T
Tom St Denis
Richard Heathfield said:All that is required is a few hundred unsigned chars. We are guaranteed that
CHAR_BIT is at least 8, which is plenty, and we know we can get hold of a
good 32767 bytes' worth of RAM, which is also vastly more than required by
the problem at hand. I think it's very obvious that there are no resource
or size issues here.
It is quite possible todo in ISO C.... ;-)
#include <tommath.h>
int main(int argc, char **argv)
{
mp_int a;
char buf[1000];
int x;
if (argc != 2 || sscanf(argv[1], "%d", &x) != 1) {
printf("Syntax: f num\n");
return EXIT_FAILURE;
}
mp_init(&a);
mp_set(&a, 1);
do {
mp_mul_d(&a, x, &a);
} while (--x > 0);
mp_toradix(&a, buf, 10);
printf("%s ! == %s\n", argv[1], buf);
mp_clear(&a);
return EXIT_SUCCESS;
}
[I should have error checking... but you get the point right ?]
This produced
tom@tomlaptop ~
$ time ./f 73
73 ! ==
4470115461512684340891257138125051110076800700282905015819080092370422104067
183317016903680000000000000000
real 0m0.084s
user 0m0.040s
sys 0m0.040s
[most of that time was spent in cygwin startup and the mp_toradix conversion
because "f 4" and "f 350" produce basically the same numbers...]
There, that's how you do 73! in portable C.
Tom