www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/05/01:12:43

From: "Bonifati" <abonifati AT telsa DOT it>
Newsgroups: comp.os.msdos.djgpp
Subject: optimizing a C program
Date: Tue, 4 May 1999 22:13:50 +0200
Organization: Centro Servizi Interbusiness
Lines: 51
Message-ID: <7gnkj2$4hu$2@fe1.cs.interbusiness.it>
NNTP-Posting-Host: 212.210.156.27
X-Newsreader: Microsoft Outlook Express 4.72.3110.5
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

how can i optimize this little program which calculates N!  ?
i don't now what optimization flags to set into RHIDE


/* this program works; i have tested it */

#define MAXPIECES 36068  /* 36068 are those necessary for 42950! */
#define N 1000           /* from 0 a 42950 */

/* a "long int" must be  32 bit (at least) */
/* "short" must be at least 16 bit, or use int instead */

unsigned long Fact[MAXPIECES], Product;
unsigned short i, i2, Carry, Index, EndIndex;

main()
{
   /* calculates N FACTORIAL */
   Fact[0]=1;
   Index=EndIndex=0;

   for (i=N; i>1; i--)
   {
      Carry=0;
      for (i2=Index; i2<=EndIndex; i2++)
      {
         Product=Fact[i2]*i + Carry;
         Fact[i2]=Product%100000;
         Carry=Product/100000;
      }
      /* manage last carry */
      if (Carry)
         Fact[++EndIndex]=Carry;
      /* adjust Index to optimize */
      while (Fact[Index]==0)
         Index++;
   }

   /* print */
   printf("%ld", Fact[EndIndex]);
   for (i2=EndIndex; i2>0;)
      printf("%05ld", Fact[--i2]);
   putchar('\n');
}







- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019