Article View: comp.lang.pascal.misc
Article #14Re: Tough FACTORIAL math problem...
From: Evgeni Sorokin
Date: Mon, 19 Feb 1996 13:24
Date: Mon, 19 Feb 1996 13:24
43 lines
1613 bytes
1613 bytes
One may use the following observations: all zeros come from 5's and 2's, and
there are more 2's than 5's.
Now, since last digit of product depend solely upon the last digits
of the multipliers, we keep only the last digits. We also collect
all 2's separately, ahhihilating them with 5's, when possible.
var LastDigit, Count,
Multiplier, Twos : integer;
const Stop = 1000;
begin
LastDigit:=1;
Twos:=0;
for Count:=1 to Stop do begin
While (Multiplier mod 2) = 0 do begin
Twos:=Twos+1;
Multiplier:=Multiplier div 2;
end;
While (Multiplier mod 5) = 0 do begin
Twos:=Twos-1;
Multiplier:=Multiplier div 5;
end;
LastDigit:=(LastDigit*Multiplier) mod 10;
end;
for Count:=1 to Twos do
LastDigit:=(LastDigit*2) mod 10;
end.
--
Zhenya Sorokin
E-mail : sorokin@ps1.iaee.tuwien.ac.at, sor@rs6.iaee.tuwien.ac.at
Paper-mail : E. Sorokin, Gusshausstr. 27/359-4, 1040 Vienna, Austria
Voice-mail : +43(1)58801-3703, -3948
Fax-mail : +43(1)504 2477
Technical Details
Message-ID:
<4g9tlu$i9t@news.tuwien.ac.at>Path:
arc1-ca.pugleaf.net!rs6.iaee.tuwien.ac.at!sorReferences:
<4fr8be$ass@news.iconn.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!rs6.iaee.tuwien.ac.at!sor From: sor@rs6.iaee.tuwien.ac.at (Evgeni Sorokin) Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland Subject: Re: Tough FACTORIAL math problem... Followup-To: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland Date: 19 Feb 1996 13:24:14 GMT Organization: Vienna University of Technology, Austria Message-ID: <4g9tlu$i9t@news.tuwien.ac.at> References: <4fr8be$ass@news.iconn.net> NNTP-Posting-Host: rs6.iaee.tuwien.ac.at X-Newsreader: TIN [version 1.2 PL2]