Factorial using C

H

Hamburgpear

Dear C/C++ fellow user,

How can I declare integer such that I can compute factorial of number larger
than 13, which can't return a correct result as it is larger than the
integer limit in C.

Thanks in advance

Hamburgpear
 
I

Ian

Hamburgpear said:
Dear C/C++ fellow user,

How can I declare integer such that I can compute factorial of number larger
than 13, which can't return a correct result as it is larger than the
integer limit in C.
use an unsigned long long (uint64_t) to go to a 64 bit unsigned value.
beyond that, you have to use double or a non-standard large integer class.

Ian
 
G

Gianni Mariani

Hamburgpear said:
Dear C/C++ fellow user,

How can I declare integer such that I can compute factorial of number larger
than 13, which can't return a correct result as it is larger than the
integer limit in C.

For really big numbers, you can use "big number" libraries.

http://www.swox.com/gmp/ is a popular one. I believe it has a C++
interface as well.
 
C

Cable_TXG

Use an unsigned long data type - ( unsigned long myFactorial; )
I do believe this is the only ANSI way of declaring it ( portability is
also a good thing).
 
J

jefong

Cable_TXG said:
Use an unsigned long data type - ( unsigned long myFactorial; )
I do believe this is the only ANSI way of declaring it ( portability is
also a good thing).

useing int64.
 
J

jacob navia

Hamburgpear said:
Dear C/C++ fellow user,

How can I declare integer such that I can compute factorial of number larger
than 13, which can't return a correct result as it is larger than the
integer limit in C.

Thanks in advance

Hamburgpear
Using the non-standard C compiler lcc-win32 you can
write:
#include <stdio.h>
#include "int128.h"
int main(void)
{
int128 n=1;
int i;
char buf[512];

for(i=2;i<34;i++) {
printf("%3d! = ",i);
n = n*i;
i128toa(&n,buf);
printf("%s\n",buf);
}
}

Output:

2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
21! = 51090942171709440000
22! = 1124000727777607680000
23! = 25852016738884976640000
24! = 620448401733239439360000
25! = 15511210043330985984000000
26! = 403291461126605635584000000
27! = 10888869450418352160768000000
28! = 304888344611713860501504000000
29! = 8841761993739701954543616000000
30! = 265252859812191058636308480000000
31! = 8222838654177922817725562880000000
32! = 263130836933693530167218012160000000
33! = 8683317618811886495518194401280000000
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top