Article View: comp.lang.pascal.misc
Article #62Re: Tough FACTORIAL math problem...
From: Steve Kettle
Date: Sun, 25 Feb 1996 21:02
Date: Sun, 25 Feb 1996 21:02
29 lines
1257 bytes
1257 bytes
The way to do this problem in a brute force way is:
- prime factorize n - not to hard you already have n factors
so you get something like 2^a * 3^b * 5^c ...
- now cast away the fives by pairing with twos
so you get something like 2^(a-c) * 3^b * 7^d
- now just multiply all the terms together only remembering
the last digit each time ( just take it mod 10 after is
multiply ).
eg. LASTNONZERODIGITOF(6!)
= LASTNONZERODIGITOF(2^4 * 3^2 * 5)
= LASTNONZERODIGITOF(2^3 * 3^2)
= No Problem can't get messed up by 10's!
- I bet though there is a MUCH faster way of doing this.
- Question for you - what is the running time of the above
algorithm?
--
Technical Details
Message-ID:
<DnCnsA.J42@undergrad.math.uwaterloo.ca>Path:
arc1-ca.pugleaf.net!undergrad.math.uwaterloo.ca!sckettleReferences:
<4fr8be$ass@news.iconn.net> <DMruKv.CCp@research.att.com>Show Full Headers
Complete Article Headers
Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland Path: arc1-ca.pugleaf.net!undergrad.math.uwaterloo.ca!sckettle From: sckettle@undergrad.math.uwaterloo.ca (Steve Kettle) Subject: Re: Tough FACTORIAL math problem... Sender: news@undergrad.math.uwaterloo.ca (news spool owner) Message-ID: <DnCnsA.J42@undergrad.math.uwaterloo.ca> Date: Sun, 25 Feb 1996 21:02:33 GMT References: <4fr8be$ass@news.iconn.net> <DMruKv.CCp@research.att.com> Nntp-Posting-Host: noether.math.uwaterloo.ca Organization: University of Waterloo