Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Calculating Factorial (Recursively & Iteratively) (http://www.go4expert.com/articles/calculating-factorial-recursively-t1757/)

pr1nc3k1d 11Jan2008 17:53

Re: Calculating Factorial (Recursively & Iteratively)
 
Did you try the code ? It's working fine ( no overflow ) but it's also limited. I didn't say that it's not. I wanted to write a code which calculates the factorial of 1000 but it's also working for greater values. The vector can hold a result with more than 10.000 digits , but it's limited.

asadullah.ansari 12Jan2008 13:10

Re: Calculating Factorial (Recursively & Iteratively)
 
Quote:

Originally Posted by pr1nc3k1d
Did you try the code ? It's working fine ( no overflow ) but it's also limited. I didn't say that it's not. I wanted to write a code which calculates the factorial of 1000 but it's also working for greater values. The vector can hold a result with more than 10.000 digits , but it's limited.



First i told you to store in vector too much overhead and second thing storing has some limit!!! But you know today In Statistical Software factorial of very big number is required. So on that case, By any way you have to go for sterling Approximation formula.

asadullah.ansari 15Jan2008 14:34

Re: Calculating Factorial (Recursively & Iteratively)
 
I got the solution for any Big number!!! This code is very efficient and optimized implemented by limked list..
Code:

class Link
{
//Data Member Parts
private:
 int dta;
 Link *nxt;
 Link *prv;
 Link *hd;
 Link *rear;

// Function Member Parts

public:
 Link(const int& tmp)  :dta(tmp),hd(NULL),rear(NULL),prv(NULL),nxt(NULL)
{
          ;
}
 Link* NxtLnk()  //getting next node
{
    return nxt;
}

 Link* PrvLnk()  //getting previous node
{
  return prv;
}
 void ExtraInsert(Link* p);          // After Inserting
 int getDta(void)
{
  return dta;
}
 void SetDta(int temp)
{
  dta = temp;
}
 int GetNoOfElem();
 Link* ToHead();
 Link* ToRear();
 void DeleteMe(void);
 void ClearAll(void);
};


int Link::GetNoOfElem()
{
 int count = 0;
 Link* p =ToHead();

 while(p->NxtLnk()!=NULL){
  count++;
  p = p->NxtLnk();
 }

 count++;
 return count;
}

void Link::ExtraInsert(Link* p)
{
 //    Link* p;
 if(prv == NULL) { hd = this;}
 p->nxt = nxt;
 p->prv = this;
 nxt = p;
 if(p->nxt == NULL){rear = p;}
};



Link* Link::ToHead()
{
 if(hd == this){ //this is the first node
  return this;
 }

 Link *p = this;
 while(p->prv != NULL){
  p = p->prv;
 }

 return p;
}

Link* Link::ToRear()
{
 if(rear == this){
  return this;
 }

 Link *p = this;
 while(p->nxt != NULL){
  p = p->nxt;
 }

 return p;
}



//***** Start ************//
void Link::DeleteMe(void)
{
 if(prv == NULL) // First Element
{
  nxt->prv = NULL;
  hd = nxt;  // Next will be first one
  delete this;
  return;
 }
 if(nxt == NULL)
{
  prv->nxt = NULL; // last node
  rear = prv; //Previous node will be first
  return;
 }
 prv->nxt = nxt;
 delete this;
}
//****  End Of Function*******//

void Link::ClearAll(void)
{
 Link* p1 = ToHead();
 Link* p2;
 while(p1->NxtLnk() != NULL){
  p2 = p1;
  p1 = p1->NxtLnk();
  delete p2;
 }
 delete p1;
};


int main()
{
 int n;// remainder
 int cy  // Carried over
 int rst;
 int N;
 Link* p = new Link(1);

 cout<<"Plz input the number:";
 cin>>N;
 for(int n=1;n<=N;n++)
 {
  rn = carry = 0;
  p = p->ToHead();

  while(p->NxtLnk() != NULL)
{
  rst = p->getDta()*n+cy
  if(rst>=10){
    rn = rst%10;
    carry = rst/10;
    p->SetDta(rn);
  }
  else
  {
      p->SetDta(rst);
  }
  p = p->NxtLnk();
  carry = rst/10;
  }
  rst = p->getDta()*n+cy


//Other allocated memory for carried over
  while(rst >= 10){
  Link * newLink = new Link(0);
  p->SetDta(rst%10);//rnder
  rst = rst/10;
  p->ExtraInsert(newLink);
  p = p->NxtLnk();
  }
  p->SetDta(rst);
 }

 p = p->ToRear();
 while(p->PrvLnk()!=NULL)
{
  cout<<p->GetDta();
  p=p->PrvLnk();
 }
 cout<<p->GetDta()<<endl;
 int num = p->GetNoOfElem();
 if(num >=5)
{
  p = p->ToRear();
  cout<<endl<<"Or"<<endl<<endl;

  cout<<p->GetDta()<<".";
  p = p->PrvLnk();

 for(int i=1;i<5;i++)
{
  cout<<p->GetDta();
  p = p->PrvLnk();
  }
  cout<<"E"<num-1<endl;
 }
 p->ClearAll();
 return 0;
}


shabbir 15Jan2008 17:14

Re: Calculating Factorial (Recursively & Iteratively)
 
asadullah.ansari, Learn to use Code block when you have code snippets in posts

pr1nc3k1d 16Jan2008 00:23

Re: Calculating Factorial (Recursively & Iteratively)
 
Quote:

Originally Posted by asadullah.ansari
I got the solution for any Big number!!! This code is very efficient and optimized implemented by limked list..
Code:

class Link
{
//Data Member Parts
private:
 int dta;
 Link *nxt;
 Link *prv;
 Link *hd;
 Link *rear;

// Function Member Parts

public:
 Link(const int& tmp)  :dta(tmp),hd(NULL),rear(NULL),prv(NULL),nxt(NULL)
{
          ;
}
 Link* NxtLnk()  //getting next node
{
    return nxt;
}

 Link* PrvLnk()  //getting previous node
{
  return prv;
}
 void ExtraInsert(Link* p);          // After Inserting
 int getDta(void)
{
  return dta;
}
 void SetDta(int temp)
{
  dta = temp;
}
 int GetNoOfElem();
 Link* ToHead();
 Link* ToRear();
 void DeleteMe(void);
 void ClearAll(void);
};


int Link::GetNoOfElem()
{
 int count = 0;
 Link* p =ToHead();

 while(p->NxtLnk()!=NULL){
  count++;
  p = p->NxtLnk();
 }

 count++;
 return count;
}

void Link::ExtraInsert(Link* p)
{
 //    Link* p;
 if(prv == NULL) { hd = this;}
 p->nxt = nxt;
 p->prv = this;
 nxt = p;
 if(p->nxt == NULL){rear = p;}
};



Link* Link::ToHead()
{
 if(hd == this){ //this is the first node
  return this;
 }

 Link *p = this;
 while(p->prv != NULL){
  p = p->prv;
 }

 return p;
}

Link* Link::ToRear()
{
 if(rear == this){
  return this;
 }

 Link *p = this;
 while(p->nxt != NULL){
  p = p->nxt;
 }

 return p;
}



//***** Start ************//
void Link::DeleteMe(void)
{
 if(prv == NULL) // First Element
{
  nxt->prv = NULL;
  hd = nxt;  // Next will be first one
  delete this;
  return;
 }
 if(nxt == NULL)
{
  prv->nxt = NULL; // last node
  rear = prv; //Previous node will be first
  return;
 }
 prv->nxt = nxt;
 delete this;
}
//****  End Of Function*******//

void Link::ClearAll(void)
{
 Link* p1 = ToHead();
 Link* p2;
 while(p1->NxtLnk() != NULL){
  p2 = p1;
  p1 = p1->NxtLnk();
  delete p2;
 }
 delete p1;
};


int main()
{
 int n;// remainder
 int cy  // Carried over
 int rst;
 int N;
 Link* p = new Link(1);

 cout<<"Plz input the number:";
 cin>>N;
 for(int n=1;n<=N;n++)
 {
  rn = carry = 0;
  p = p->ToHead();

  while(p->NxtLnk() != NULL)
{
  rst = p->getDta()*n+cy
  if(rst>=10){
    rn = rst%10;
    carry = rst/10;
    p->SetDta(rn);
  }
  else
  {
      p->SetDta(rst);
  }
  p = p->NxtLnk();
  carry = rst/10;
  }
  rst = p->getDta()*n+cy


//Other allocated memory for carried over
  while(rst >= 10){
  Link * newLink = new Link(0);
  p->SetDta(rst%10);//rnder
  rst = rst/10;
  p->ExtraInsert(newLink);
  p = p->NxtLnk();
  }
  p->SetDta(rst);
 }

 p = p->ToRear();
 while(p->PrvLnk()!=NULL)
{
  cout<<p->GetDta();
  p=p->PrvLnk();
 }
 cout<<p->GetDta()<<endl;
 int num = p->GetNoOfElem();
 if(num >=5)
{
  p = p->ToRear();
  cout<<endl<<"Or"<<endl<<endl;

  cout<<p->GetDta()<<".";
  p = p->PrvLnk();

 for(int i=1;i<5;i++)
{
  cout<<p->GetDta();
  p = p->PrvLnk();
  }
  cout<<"E"<num-1<endl;
 }
 p->ClearAll();
 return 0;
}



Is this code working cuz I tried it and it's not !?

asadullah.ansari 16Jan2008 09:07

Re: Calculating Factorial (Recursively & Iteratively)
 
Yes!!! It's working. which plateform you r trying...

vignesh_sv 22Jan2008 12:52

Re: Calculating Factorial (Recursively & Iteratively)
 
very good job buddy could u help me by saying which is the header file to b included for using
the command sleep( ); in c

debleena_doll2002 20Feb2008 11:30

Re: Calculating Factorial (Recursively & Iteratively)
 
Quote:

Originally Posted by vignesh_sv
very good job buddy could u help me by saying which is the header file to b included for using
the command sleep( ); in c

Why tou are asking this question in this thread. Just create new thread for it in Queries

lead.smart34 26Feb2008 18:18

Re: Calculating Factorial (Recursively & Iteratively)
 
good info

crazytolearn57 26Feb2008 18:38

Re: Calculating Factorial (Recursively & Iteratively)
 
good one


All times are GMT +5.5. The time now is 03:21.