C prog-Fact of 1,00,000

Status
Not open for further replies.

Pragadheesh

In the zone
Its a question asked in my C test..:(
Factorial of One Lakh(1,00,000)
The problem is ,wat the data type can hold the result..?? is there any other way?
plz help me frenz..
 

mediator

Technomancer
Even the scientific calculator of FC5 gives "error" on that! I tried "long" in my factorial program, but it computes well to 12! .
 

kalpik

In Pursuit of "Happyness"
Hehe.. Just use string pointers, and you will be limited only by the ammount of memory your system has! :)
 

aadipa

Padawan
Try to create something like BigInt in java.

Refer to this code
Code:
enum Sign { NON_NEGATIVE, NEGATIVE};

typedef enum Sign SIGN;

struct BigInt {
   char number[100];
   SIGN sign;
   } ONE  = {"1",NON_NEGATIVE},
     ZERO = {"0",NON_NEGATIVE};

typedef struct BigInt BigInteger;
Code:
char signChar(BigInteger b) {
   return  (b.sign==NEGATIVE) ? '-' : ' ';
   }

BigInteger getBigInt(void) {

   BigInteger b;

   scanf("%s",b.number);
   if(b.number[0]=='-') {
      xstrcpy(b.number, &b.number[1]);
      b.sign=NEGATIVE;
      }
   else {
      b.sign=NON_NEGATIVE;
      }
   return b;
   }


BigInteger addBigInt(BigInteger b1, BigInteger b2) {
   BigInteger result;

   int l1,l2,l;
   int i;
   int i1,i2,carry;

   if(b1.sign != b2.sign) {
      b2.sign = (b2.sign+1)%2;
      return subBigInt(b1,b2);
      }

   l1 = xstrlen(b1.number);
   l2 = xstrlen(b2.number);

   l = l1<l2?l2:l1;

   l1--;l2--;

   result.number[l]=NULL;
   carry=0;

   for(i=0; i<l; i++, l1--, l2--) {
      i1 = (l1>=0 ? ( b1.number[l1]) - '0' : 0);
      i2 = (l2>=0 ? ( b2.number[l2]) - '0' : 0);
      result.number[i] = ( i1 + i2 + carry)%10 + '0';
      carry = ( i1 + i2 + carry)/10;
      }

   if(carry != 0) {
      result.number[i++] = carry + '0';
      }
   result.number[i] = NULL;
   xstrcpy(xstrrev(result.number),result.number);

   result.sign = b1.sign;
   return result;
   }

BigInteger subBigInt(BigInteger b1, BigInteger b2) {
   BigInteger result;

   int l1,l2,l;
   int i;
   int i1,i2,carry;

   if(b1.sign != b2.sign) {
      b2.sign = (b2.sign+1)%2;
      return addBigInt(b1,b2);
      }

   l1 = xstrlen(b1.number);
   l2 = xstrlen(b2.number);

   l = l1<l2?l2:l1;

   l1--;l2--;

   result.number[l]=NULL;
   result.sign = b1.sign;
   carry=0;

   for(i=l-1; i>=0; i--, l1--, l2--) {
      i1 = (l1>=0 ? ( b1.number[l1]) - '0' : 0);
      i2 = (l2>=0 ? ( b2.number[l2]) - '0' : 0);
      result.number[i] = ( i1 - i2 + carry + 10)%10 + '0';
      carry = ( i1 - i2 + carry)<0?-1:0;
      }

   if(carry!=0) {
      for(i=l-1; i>=0; i--) {
	 result.number[i] = '9' - result.number[i] + '0';
	 }
      if(result.sign == NON_NEGATIVE)
	 result = addBigInt(result, ONE);
      else
	 result = subBigInt(result, ONE);

      result.sign = (result.sign+1)%2;
      }
   return result;
   }

BigInteger mulBigInt(BigInteger b1, BigInteger b2) {

   int l1,l2;
   int i;

   BigInteger temp,result;

   temp = ZERO;
   result = ZERO;

   l1 = xstrlen(b1.number);
   l2 = xstrlen(b2.number);

   for(i=0; i<l2; i++) {
      int c,j,num,carry=0;

      for(c=0,j=0;c<i;c++,j++) {
	 temp.number[j] = '0';
	 }

      for(c=0; c<l1 || carry!=0; c++,j++) {
	 num = (b2.number[l2-i-1] - '0')
	     * (c<l1 ? b1.number[l1-c-1] - '0' : 0)
	     + carry;
	 temp.number[j] = num%10 +'0';
	 carry = num/10;
	 }

      temp.number[j]=NULL;
      xstrcpy(temp.number, xstrrev(temp.number) );
      result = addBigInt(temp, result);
      }

   if(b1.sign != b2.sign) {
      result.sign = NEGATIVE;
      }

   return result;
   }
Code:
printf("\nEnter first number : ");
b1 = getBigInt();
.....
printf("\n   %075s%c", result.number, signChar(result));
 
Status
Not open for further replies.
Top Bottom