Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-10-2006, 04:50 PM   #1 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Unhappy C prog-Fact of 1,00,000


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..
Pragadheesh is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 03-10-2006, 11:17 PM   #2 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: C prog-Fact of 1,00,000

Even the scientific calculator of FC5 gives "error" on that! I tried "long" in my factorial program, but it computes well to 12! .
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 03-10-2006, 11:19 PM   #3 (permalink)
In Pursuit of "Happyness"
 
kalpik's Avatar
 
Join Date: May 2005
Location: New Delhi
Posts: 3,432
Default Re: C prog-Fact of 1,00,000

Hehe.. Just use string pointers, and you will be limited only by the ammount of memory your system has!
kalpik is offline  
Old 04-10-2006, 12:48 AM   #4 (permalink)
Wise Old Owl
 
aadipa's Avatar
 
Join Date: Feb 2004
Location: Palghar, Mumbai
Posts: 1,000
Default Re: C prog-Fact of 1,00,000

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));
__________________
i generally prefer quality over quantity
1 aadi + 1 aadi = 1 full ;)
aadipa is offline  
Old 04-10-2006, 12:56 AM   #5 (permalink)
String Phreak
 
mediator's Avatar
 
Join Date: Mar 2005
Location: In ur Evil Mind!
Posts: 2,457
Default Re: C prog-Fact of 1,00,000

How?? I'm not so good at pointers in C !
__________________
Bad Bad server.....No candy for u!
mediator is offline  
Old 04-10-2006, 05:31 PM   #6 (permalink)
In The Zone
 
Pragadheesh's Avatar
 
Join Date: Jul 2006
Location: Coimbatore
Posts: 403
Post Re: C prog-Fact of 1,00,000

kalpik plz help... how to use string pointers.. even 'm not all dat good in pointers..
Pragadheesh is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by topgear
- by abhidev
- by clmlbx
- by Sarath

Advertisement




All times are GMT +5.5. The time now is 04:43 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2