Article View: comp.lang.pascal.misc
Article #35Re: Tough FACTORIAL math problem...
From: Mike McCarty
Date: Thu, 15 Feb 1996 19:18
Date: Thu, 15 Feb 1996 19:18
36 lines
1940 bytes
1940 bytes
In article <31224679.6193@born.com>, John Cleland <clelaj@born.com> wrote:
)The Crow wrote:
)>
)> Here is what I am trying to do, I want to find the last NON-ZERO digit of a
)> given factorial. For instance,
)>
)> 5! = 120
)>
)> so the last non-zero digit is 2. I want to be able to do this up to 1000.
)> Problem is, no matter how huge of a data-type you use, there isn't any way for
)> the computer to handle 1000!, it is however possible to find the last non-zero
)> digit somehow. One thing I have tried is as I went through mulitplying the
)> series of numbers in the factorial (5 * 4 * 3 * 2) I would remove all the
)> trailing ZEROS, I got this to work up to 789, but it wont work with 1000 and i
)> am not really sure why. If anyone has a clue how I can do this let me know.
)
)Don't just strip the trailing zeros, remove all digits except the last non-zero
)digit (which is your output) and then multiply by the next number in your sequence.
)This keeps the numbers down to a managable level and gives the correct answer as
)no more significant digit can affect the value of the LSD.
Let's try your technique:
1! = 1 ~ 1
2! = 2x1! ~ 2
3! = 3x2! ~ 3x2 = 6
4! = 4x3! ~ 4x6 = 24 ~ 4
5! = 5x4! ~ 5x4 = 20 ~ 0
You should at least think before engaging the fingers.
Mike
----
char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
I don't speak for DSC. <- They make me say that.
Technical Details
Message-ID:
<4g00u6$gkk@sun001.spd.dsccc.com>Path:
arc1-ca.pugleaf.net!sun001.spd.dsccc.com!spd!jmccartyReferences:
<4fr8be$ass@news.iconn.net> <31224679.6193@born.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sun001.spd.dsccc.com!spd!jmccarty From: jmccarty@spd.dsccc.com (Mike McCarty) Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland Subject: Re: Tough FACTORIAL math problem... Date: 15 Feb 1996 19:18:30 GMT Organization: DSC Communications Corporation, Plano, Texas USA Message-ID: <4g00u6$gkk@sun001.spd.dsccc.com> References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com> NNTP-Posting-Host: aplo139.spd.dsccc.com