2012! - 2012팩토리얼(Factorial)

곱하기 공장을 돌리자

!    

전체 자리수 :    뒤쪽 0의 개수 :

도입

2012!을 계산하면 뒤쪽에 연속으로 0이 몇 개나 있을까?
이 문제는 중학생(빠르면 초등학생) 정도의 지식이 있으면 풀 수 있을 것이다.
중간 과정은 생략하고, 402 + 80 + 16 + 3 = 501. 즉, 501개의 0이 뒤쪽에 붙을 것이다.

하지만, 0이 아닌 가장 오른쪽 수는 얼마인지 묻는다면 이는 쉽지 않은 문제이다.
난 그냥 프로그래밍을 통해 계산해 보았다.

문제 출처 - http://blog.naver.com/lovetaehong/130129125714

결과

컴퓨터에서 어떤 프로그래밍 언어를 사용하더라도 수천 자리를 단번에 계산하여 표시해줄 수는 없다.
javascript를 사용하여 2012!을 계산할 경우, "Infinity"라는 에러를 볼 수 있다.
배열을 이용하여 이를 계산한 결과는 전체 5776자리의 수이며, 뒤쪽 0은 501개, 0이 아닌 마지막 수는 8임을 알 수 있다.

2012! =

1412253972707227379195669601681251353849193212501443985873332281217138294974456257830568933803558040
4764354450095438311291718003723921619433751338870013355333341221130497736338745960840296983595991662
5964577700917792298728305810761076888711917090207310380322522087672321212991856988800398009655941634
2432082487889088964637423226471611880546063714004672347038849395817138102627273477555359683680012259
3815166092297730901860548867540556974394901274358124036718515681337702547826672691446650461786301389
5234546002069541508182520440860536498683626879822897115085905023194151548589081145124783870694763409
5045687818858586646689522600270351425421265098403239725975868381549808585030885969918222684541947875
5174394280472552346154153953303341814883918737817757217924860154144886587202455162940886014190146223
7120570004093390762235183214248265906393290539911061031466171509393570070638681702343668221955826096
3897051992891172655198971709215745714067606665459308288878795906764494040126649322232786985309586373

7763314493440183065207729616425183244692275778332091090486535798444220063268742017941829787254378619
7053409188818146750280106948539529322964070936045314937311536529173030315519293245267203278595503775
0892287985419773612051941170401690358392883442760352191482727176255307955453084271752563657156224490
0321793997819701221817443769332981260211016456668862211533466618622361269323174714617783446872850512
1371379164154163263769666603562028061392177774497967343993130781049211893387682425064283148755022573
2131867131505605544410986507100033772449176174136645751962863912858737111891511996258329940194109500
6872324039025001940154076811801158456623611287661407262241418798296968693930013933645031812309417127
6039392913675944867203078970737007962775076446209810606456999578693335711211062288443831812042196669
8115865094565824494508664276500003639329181388541036967538646992858499558830705893792442517477049638
4330647723944039005256280627124805978830020425129819401767589203499975802378345970327339475670018315

3922989706430966867966370716275829656160705963367778367331167397028773111304891147359301353147529642
7699195267778927067650554448425095988651680519560781746108971279393199919891927741678726978867423141
1252268864891663488238209780143777789627683632180703867577417266853980205448585757940617690974064629
3770486987882274988900303501979091701662910424018940108406380737042097304032656944741147343859003936
0023362658778774938813429844023542079635267831966409931359469689825172017331022308622085701118302981
1295034631616336450668878896659809653037686168397008680724029169336491716372578659633509491846408822
0119047617440046422899210241682806291822493079148759607294166077126164821194191985881527764639440892
0119747013180993689578464973831510369351534433822840943759341665959194095552047278503560356447228135
9292010520320339622220005748514581496238925909350596017560966574904588515249967032930079840619830828
0808793732049225093199164305115795248589671822555001970938494216103064613541856159331390380291726121

6166166194986869600235004016735813868845266990624451493440213441072600793478345730980603735164208160
2232838267890380114352150360253238082883633093838934230314615672633831193810507445645331920809211149
5824368911193860354911023142918306218275492114703541648667188413916286100808670792529712645922426962
2123547103008037073231082135982375433269674697520853313147653734218878206175533203958278567752417676
2172646456431142626144873456178085626019095645391074631392107831216721187275245813452996465902164826
1029587362687851803340173330894682704279326699048681353617478687236382048885652631844468781408986650
4563108268135757577428210681005076636639140097660089697324383379322152645616754405216294840255120481
7899639892096989669622276491211529615719545968682185585092558263203489587031026617991393454596137966
3624308767991768547612150633688626935605195611563381447364115517018433948370610716732877187866951450
0355775221869712865230351937490406305226879888499990316013548267499706947263750662697978814814058948

9227779901999679459769950321115390186830178533445927630546302469872845388494735846892739853499716853
7849304246474963411522534677468408729078736734213374241703944036926974473149731479647426259617486526
6923207080489756021701114763838261189901902081306302863066098572599161530114108569509948737344444520
5707822558315028145436939839690264311684838670848255854260872245033028391860739288260673783494717574
6850726866281010573821755740718850024612917609158170212968817042816338277515132738721865393485554658
1556870693656028528073518356350632867032411819193155644868409542395489578024612258718137875340297170
9386186947827023613080075282169435532231781873971301958688434207746594969289686025534491540331101757
4238092781120619317326315710524490235051803832614431155221916462669945168289493365084421779216106703
6382561043101325479090754764797263063903727465632771282964116944061011179280331021521933490539345838
5262549438318869721587320234575902071353091616352826244221765663920595411234701674038092428042727102

3882183198452849666077471991016381256164346711467983761256257977447192906153686280495036455095099408
6129422173620945456247714869850431012898853249974871424570759842107328157982295530556089941649280957
2403796539293614140782990483399766675656214441255752144840198111230196449280000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000 (5776자리)

(p.s)

여기서는 자바스크립트를 사용했으며, 최적화하지는 않았다.
그저, 여러자리수를 계산하는 방법을 보여주는데 목적을 두었다.
여기서 사용된 방법을 나름대로 분석(이 페이지 소스를 분석하면 됨)하여 더 많은 자리에 대해 보다 빨리 계산할 수 있도록 만드는 것은 여러분의 몫이다.


Home


최초 작성일 : 2012.01.20