Storing numbers greater than 20! (factorial)

I try to find numbers up to 100! (factorial), but after 20! it throws an error because the value is too large to handle. How to save such a number?

+8
c
source share
4 answers

20 factorial - 19 digits. 100 factorial is 158 digits, occupying 500 bits! This is actually:

 933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000 00000000 

None of the standard types will work for you here, you will need to implement your own procedure for processing such large numbers.

For example, you will need to accept that multiplying two 32-bit values ​​together can produce a 64-bit result.

It was a collaborative practice to calculate large numbers on days of 8-bit processors. It is called arbitrary precision arithmetic.

Here is a link to describe how this was done. Admittedly, this is for assembler, but it still persists. You will need to consider the default int sizes on your system.

+11
source share

At least on Linux, a possible multi-point library might be GMP (it also works on Solaris, Windows, MacOSX, etc.).

+10
source share

How do you do it on paper? Performing multiplications with numbers 160 digits long is not so difficult for a computer.

Try writing your own function or using an existing library.

0
source share

This code will help up to 170! . Use double for factorial function, because normal int or long int will not support this factorial result, double will support up to 1.7e308 .

 #include<stdio.h> double fact(int k){ int i; double f=1; for(i=1;i<=k;i++) f=f*i; return (f); } int main(){ int i; for(i=1;i<=65;i++) printf("%d! = %.3lf\n",i,fact(i)); return 0; } 
-3
source share

All Articles