-1

I'm trying to run a program that does certain operations on factorials of large numbers (say 50!; viz 3.041e+64 - huge!) and therefore doesn't fit in the normal int data types that I'm aware of(unsigned long long int etc)

Which data type do I use to store these values?

P.S I was trying to find the trailing zeroes in a factorial. The following was my approach:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int FactorialFinder(int a)
{
    if (a>1)
    a= a* (FactorialFinder(a-1)) ;

    return a;
}


int main()
 {
  printf("Enter number \n");
  int num ;
  scanf("%d",&num) ;
  printf("number is %d\n",num);

  printf("Factorial is %d",(num = FactorialFinder(num))) ;

int x=0, count = 0 ;

while(num>0)
{
    x = (num%10) ;
    if (x == 0)
        count++ ;
    else
        break;

    num= num/10 ;
}

printf("\nNumber of trailing zeroes is %d",count) ;
getchar() ;
return 0;
}

Works fine upto 12! beyond which the results are erroneous (from 17! it starts returning negative factorial values(?), from 34! it gives 0) I'm guessing due to the datatype problem. Can someone help me out?

Adrian
  • 14,931
  • 9
  • 45
  • 70
Ambareesh S J
  • 97
  • 1
  • 9
  • 3
    The number of trailing zeros in the decimal representation of `n!` is determined by the number of factors of `5` in the factorial. You don't need to compute the full factorial. – EOF Jun 07 '16 at 22:22
  • You could brute-force this with a bignum library, but if you're going to get more problems from wherever you got this one, implementing the naive algorithm with bignums is unlikely to cut it for all your problems. – user2357112 Jun 07 '16 at 22:24
  • @EOF I know that approach, that's why I didn't ask for the solution. Is there a way using my method? – Ambareesh S J Jun 07 '16 at 22:27
  • @BLUEPIXY neither of those questions answer this one – Adrian Jun 07 '16 at 22:29
  • 1
    and that one isn't even related – Adrian Jun 07 '16 at 22:34
  • Try http://stackoverflow.com/questions/117429/handling-large-numbers-in-c It is not a single data type but an array used to store all the digits. – cup Jun 07 '16 at 22:39
  • Use a bignum library. – fuz Jun 07 '16 at 23:31

1 Answers1

1

Well these kind of numbers cannot be stored as single number rightly as you've reasoned there are no data types to hold them. The best way to work with large numbers is to store them as arrays of int type or char type.

for example you can store 1234567898765 as an array int big[14] where,

big[0]=1
big[1]=2
.
.
.
big[13]=5  //last element
big[14]=-1 //to mark the end of number...

or in the reverse order with -1 as last element (select which is is convenient for your implementation)


and now comes the challenging part where you have to create functions for addition, subtraction, multiplication and other operations which you require.. There are many ways of implementing these function..give a try.. or you can just look up how to do them here's a source : click

this provides a implementation of arithmetic numbers for up to 100 digit numbers however you can try to build one which can deal with even larger numbers :)

Cherubim
  • 5,287
  • 3
  • 20
  • 37