Article View: comp.lang.pascal.misc
Article #31Re: Tough FACTORIAL math problem...
From: Jos A. Horsmeier
Date: Thu, 15 Feb 1996 17:00
Date: Thu, 15 Feb 1996 17:00
45 lines
1903 bytes
1903 bytes
In article <wzenrwg142.fsf@stego.cs.ruu.nl>, piet@stego.cs.ruu.nl wrote:
|>>>>> thecrow@iconn.net (The Crow) (TC) writes:
|
|TC> Here is what I am trying to do, I want to find the last NON-ZERO digit
|TC> of a given factorial. For instance,
|
|TC> 5! = 120
|
|TC> so the last non-zero digit is 2. I want to be able to do this up to
|TC> 1000. Problem is, no matter how huge of a data-type you use, there
|TC> isn't any way for the computer to handle 1000!, it is however possible
|A little thinking before coding solves this problem very easily:
|
|Let N be then number whose factorial you are considering.
|
|What you want is the number of trailing zeroes + 1.
[ clever solution of not-the-posters-problem deleted ... ]
No, that's not what he wanted. A little reading before thinking before
coding may come in handy sometimes ;-) What the original poster wants
is the value of the rightmost non-zero digit of n!.
Let D(n) be the value of the rightmost non-zero digit of number n,
it is easy to show that D(n!)= D(D(n)*D((n-1)!). Here's function D,
written in C (it still is c.l.c isn't it?)
int D(n) {
while (n && !(n%10))
n/= 10;
return n%10;
}
I leave the rest of the program as an exercise for the reader (boy,
I still love writing that ;-)
kind regards,
Jos aka jos@and.nl
--
Atnwgqkrl gy zit vgksr, ug qshiqwtzoeqs!
Technical Details
Message-ID:
<4fvorm$dfm@beach.and.nl>Path:
arc1-ca.pugleaf.net!beach.and.nl!usenetReferences:
<4fr8be$ass@news.iconn.net> <wzenrwg142.fsf@stego.cs.ruu.nl>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!beach.and.nl!usenet From: jos@and.nl (Jos A. Horsmeier) 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 17:00:38 GMT Organization: AND Operations Research B.V. Message-ID: <4fvorm$dfm@beach.and.nl> References: <4fr8be$ass@news.iconn.net> <wzenrwg142.fsf@stego.cs.ruu.nl> NNTP-Posting-Host: klepzeiker.and.nl Mime-Version: 1.0 Content-Type: Text/Plain; charset=ISO-8859-1 X-Newsreader: WinVN 0.99.5