20050407, 13:04  #1 
3^{2}·733 Posts 
Factorial in C programming
Hello everybody
I need a C programme which will compute factorials. I searched for it on the internet, and found some programmes, but they don't seem to work when I try factorial 13, instead when I tried factorial 100 I get the answer 0. Can anybody help, create a programme which works for all numbers. 
20050407, 13:10  #2 
May 2004
120_{8} Posts 
A possible fac.c to print the factorial of the first commandline argument:
Code:
#include <stdio.h> #include <gmp.h> int main (int argc, char **argv) { mpz_t x; mpz_init (x); mpz_fac_ui (x, atoi (argv[1])); gmp_printf ("%Zd\n", x); mpz_clear (x); return 0; } Code:
gcc fac.c o fac lgmp Dave 
20050407, 13:12  #3 
May 2004
2^{4}·5 Posts 
Btw if you only need a few values then use the applet at www.swox.com/gmp. If you're only interested in the source then look at mpz/fac_ui.c in the gmp distribution.
Dave 
20050407, 13:14  #4 
11·283 Posts 
Thanks
Thanks

20050407, 13:39  #5 
2·11·229 Posts 
Yes but...
How do I compile the GMP library, and I'm not sure what you mean by cygwin. I'm using Devc++ compiler.
Isn't there an easier way of producing factorial programmes. I'm quite new to C programming, hence I only know the basics. 
20050407, 14:14  #6 
May 2004
2^{4}·5 Posts 
Sorry, I don't have a windows machine so I can't walk you through this. Search the forums for "gmp" and "cygwin", it's been done. Btw it's quicker to type "cygwin" into google than post "I'm not sure what you mean by cygwin" on the forums and wait for a reply.
What I told you is IMO by far the easiest way to compute factorials in C. If you want to use Python then it's also easy (but involves you installing Python). The underlying problem is that 32bit unsigned longs can only hold numbers less than 2^32, i.e. 13! and above will overflow. So "raw C" isn't good enough  you need a bignum library. Dave 
20050407, 14:42  #7  
∂^{2}ω=0
Sep 2002
República de California
3·5^{3}·31 Posts 
Quote:


20050409, 20:13  #8 
Mar 2003
New Zealand
13×89 Posts 
Here is one using base 10 multiplication, the number is stored as a text string and multiplied digit by digit. It is very slow, to make it fast you need to use base 2^16 or 2^32, and write a routine to convert into base 10 for printing.
Code:
#include <stdio.h> #include <stdlib.h> int multiply(char *str, int len, int n) { int c, i; for (c = i = 0; i < len; i++) { c += n * (str[i]  '0'); str[i] = c % 10 + '0'; c /= 10; } while (c > 0) { str[i++] = c % 10 + '0'; c /= 10; } return i; } void usage_error(void) { printf("Usage: 'factorial n', n a nonnegative integer.\n"); exit(1); } void memory_error(void) { printf("Failed to allocate enough memory.\n"); exit(1); } int main(int argc, char **argv) { int n, len, buf = 10000; char *str; if (argc < 2) usage_error(); n = atoi(argv[1]); if (n < 0) usage_error(); str = (char *)malloc(buf); if (str == NULL) memory_error(); str[0] = '1'; len = 1; while (n > 1) { if (len > buf  10) { buf *= 2; str = (char *)realloc(str, buf); if (str == NULL) memory_error(); } len = multiply(str, len, n); } while (len >= 1) putchar(str[len]); putchar('\n'); return 0; } 
Thread Tools  
Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
MultiFactorial Search  rogue  And now for something completely different  1  20150602 23:51 
Factorial puzzle  henryzz  Puzzles  5  20150402 12:58 
Factorial primes  Unregistered  Information & Answers  2  20110911 21:32 
Factorial  mfgoode  Puzzles  6  20070724 14:24 
Factorial problem  xilman  Puzzles  12  20030720 20:22 