![]() |
![]() |
#1 |
32·733 Posts |
![]()
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. |
![]() |
![]() |
#2 |
May 2004
1208 Posts |
![]()
A possible fac.c to print the factorial of the first command-line 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 |
![]() |
![]() |
![]() |
#3 |
May 2004
24·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 |
![]() |
![]() |
![]() |
#4 |
11·283 Posts |
![]()
Thanks
|
![]() |
![]() |
#5 |
2·11·229 Posts |
![]()
How do I compile the GMP library, and I'm not sure what you mean by cygwin. I'm using Dev-c++ compiler.
Isn't there an easier way of producing factorial programmes. I'm quite new to C programming, hence I only know the basics. |
![]() |
![]() |
#6 |
May 2004
24·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 32-bit 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 |
![]() |
![]() |
![]() |
#7 | |
∂2ω=0
Sep 2002
República de California
3·53·31 Posts |
![]() Quote:
|
|
![]() |
![]() |
![]() |
#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 non-negative 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 | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Multi-Factorial Search | rogue | And now for something completely different | 1 | 2015-06-02 23:51 |
Factorial puzzle | henryzz | Puzzles | 5 | 2015-04-02 12:58 |
Factorial primes | Unregistered | Information & Answers | 2 | 2011-09-11 21:32 |
Factorial | mfgoode | Puzzles | 6 | 2007-07-24 14:24 |
Factorial problem | xilman | Puzzles | 12 | 2003-07-20 20:22 |