🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.logo
Article #257

Re: um, maybe this is a silly question

#257
From: Max Hailperin
Date: Tue, 13 Oct 1992 19:17
51 lines
2774 bytes
In article <1992Oct13.035329.19258@pasteur.Berkeley.EDU>
 matt@volga.Berkeley.EDU (Matt Wright) writes:

   ...
   int Factorial(int x) {
       int result;
       result = 1;
       while (x > 0) {
           result *= x;
           x--;
       }
       return result;
   }

   Should you initialize result to 1 or 0?  Is it while x >0 or x >= 0, or 
   x > 1 ?  Don't forget to decrement x each time you loop...  etc, etc...

   ...

Ok, I'm going to stop beating the dead horse after this, but it seems
like a good place to plug more concretely what I said in the abstract.
Let's rewrite your procedure a little and show that the various
boundaries, initial values, whatever, are trivial by explicating them
with overkill comments:

int factorial_product(int x, int n){ /* computes x * n! */
   if (n == 0)
     return x;  /* because x * 0! = x * 1 = x */
   else
     return factorial_product(x*n, n-1);
       /* because x * n! = x * (n * (n-1)!) = (x*n) * (n-1)! */
   }

int Factorial(int n) {
   return factorial_product(1, n); /* because n! = 1 * n! */
   }

Surely no one would "initialize the result to 0" because that means
saying to compute 0*n!, which clearly isn't going to equal n!.  You
just need to look at the spec.  The rest follows similarly.

Note that my version generates every bit as iterative a process as
yours, presuming a decent compiler.  The fundamental distinction isn't
between these two version, but rather between the two of them and the
version based directly on n! = n * (n-1)!.  It may be easier to solve
the problem that way (breaking off a subproblem) than this way
(transforming to a simpler problem with the same answer), but the
difference *isn't* the one you suggest.  Any problem you had figuring
out whether to start your "result" variable at 0 or 1 was purely a
product of not packaging the loop in such a way as to make clear what
the functional relationship was between its inputs and output.
Technical Details
Message-ID: <MAX.92Oct13121742@Kolmogorov.gac.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!pmafire!news.dell.com!natinst.com!cs.utexas.edu!sun-barr!ames!olivea!spool.mu.edu!umn.edu!noc.msc.net!gacvx2.gac.edu!nntp-server!max
References: <1992Oct11.192128.5448@netcom.com> <1ba7vcINNpam@agate.berkeley.edu><1992Oct12.163421.24051@sctc.com><1992Oct13.035329.19258@pasteur.Berkeley.EDU>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!pmafire!news.dell.com!natinst.com!cs.utexas.edu!sun-barr!ames!olivea!spool.mu.edu!umn.edu!noc.msc.net!gacvx2.gac.edu!nntp-server!max
From: max@Kolmogorov.gac.edu (Max Hailperin)
Newsgroups: comp.lang.logo
Subject: Re: um, maybe this is a silly question
Message-ID: <MAX.92Oct13121742@Kolmogorov.gac.edu>
Date: 13 Oct 92 19:17:42 GMT
References: <1992Oct11.192128.5448@netcom.com> 
 <1ba7vcINNpam@agate.berkeley.edu><1992Oct12.163421.24051@sctc.com><1992Oct13.035329.19258@pasteur.Berkeley.EDU>
Reply-To: Max Hailperin <max@nic.gac.edu>
Distribution: usa
Organization: Gustavus Adolphus College, St. Peter, MN
Lines: 51
Nntp-Posting-Host: kolmogorov.gac.edu
In-reply-to: matt@volga.Berkeley.EDU's message of Tue, 13 Oct 1992 03:53:29 GMTLines: 51