User blog:LittlePeng9/Computing digits of mega

Yesterday while talking to Sbiis on IRC he has mentioned the topic of computing more than 14 last digits of mega. Later I have decided to see what I can do. After about 5 minutes of thinking, I have figured out an iterative algorithm to find the digits of that number, and the day later (i.e. today) I have found simplification. I told vell about it, he said it should be possible to write this in Julia and then execute. He has found the following, short, code:

b = BigInt(10)^5000 n = BigInt(256) for i in 1:256 n = powermod(n, n, b) end n

It computes, in merely 4 minutes, last 5000 (!) digits of mega, which are the following:

95349023028618553749832119920226094055997377690296163291300803575045996441166806301502490450260656489250941844326130861876392462502884613118510014308714341419920580220488755977791245771686978621780403387279049316009315221282761396923062835402571774079871954531151266685756361190405786179863997344068152164745142524897730204092118143855405317944082521493422518317613074196462805648070013494883116882004797830412565696958279065952177095868847496615856875852827813700840223622561610205724003893286970820394311776709595607453373509304364840305069952253071508094565321578887063138658966575711049819432725968506965930299264055858177423532584243554764391610278179076245812181716331733159767873005927139753605775517120744004250432567177363584503824271363294656157510703027464952289280918078420433574244249786552393917572978712500322409413567808864770480362989064766191315200888400447492540383424696602021239736353891084877006003571681139812144518601466782766729076339952661466381190438945575188075171180135825803964011080368184144595766219179628343924571964117866881238351646791810054633514790090480044244685246024533808314928362155553765205438396723116142935075450090534837013734856677220282083148689064105138170868631895122129272060982997139875659986041345044350440927598486168740859881248566969923372060589951648114028836936704388949027171407618738222648780263600718103937678102460630405955327423976298361967379921490236772141189786313638175210409645244821740911631287628942078356239984976117168957007693658904438759589140649745855249646025304676914433806466179007002634722296651015289857094282501322900251659741108047304808503604808605953443391389201816835003736669323947661981562334664299378003823283506012908613626917253640755992580534561235403330513563433184311921752909314501366444647358186796261387016366273975550847865126346078704950794203263912243070102964503080187139456044715599312191926888336033853800136039906307961759929981034765131565665285357062755812508724646246576938803863584152146119129142215433310350605350043061109446660174055356688566632269360070882426362037823204020044131477637548964265797420406440203455139619314560448120115647321668061982480649019048385777227628441937345048054710943419343980811158314685075006484053366940174705907304079611684792646530235472006333076110574120487496348502866017575099422424941312865775622882350971868378969773121060297876975338019148889157716153492096851318552709076028059272548029438773875047381687939634495341685512643664231696478629048224472316041444252932601815776797792801595529986871529099021548381403098115162964634595550036775303387680861805861137545491529425401245054386393545831529415008237525623656592473044974441044445433588247192262911815940508229424374824427972571789678016455860632780682638159149431900407221261908828157935556386629092271527099527548495870693287431596032432813170791418070111288240997174197687045039388519282596515663904905899483642109930612002436419162932346974468120744933882539509389941962019047762216608957011722784055021500518051182386455695865138139265731364383557811531546972412222073847589808307671259289947493055201747562185914472471563537904639373432516604956957510953882072435930977225907249715370894200775843537944693315633809827093707371119887243789293950211589902345569778452807213111715048263369611842524703330048385057028466909305253336373281753304083288839157974079560719057087445555019809033629084808498482700958480836589924034313908129508178954403079678655369215555193487391832303113527535338604061475248064276862936758605367812980003425680369838450282475531576982183932638795992157158722571233321027011290298989159537809293240246808467427355220756060413176878244759327955244774312122618195057638789807285186306199600717614998210505569476805810678818980548760679753320874780578116884099700423716302102864814370267881993840332320606411659160486754107821615798064217137799684818996751279640380942073475112280910627589866684606623694380777240017114646807096674299884998468194716532630918779544419471882730607724108264936414916927895525116970379589815699860789428179752223540768603591111785116844998864190112486263849933610613465386927919192469831496598716697950848752582277438126636875521941759989636744054945416125552511322379108415541598503747293982471986260657665703465038601001235952116037944917901444627919809770467399267919231340786425321602602844904940428194927026211510168127986032737054183640162959410191530910128122872448951298789194912244209798914913155189119008412102279931826609234262961460122045725315785186425736461268034794021680658790786586247524764872803902837976103376819650464339061415489976640723044576837710810075626398219535580177406392400725774201348843029490585776329091934693098566888682178174163383443798637165141207014929599697879439430046777231331325642730394610854006632923911301838502897561606601450696228009791206968151401164151569201969226596586425189027828673870027190816633074193637388524012354109181117468059671715057145877044749894910112922449731993539660742656

The idea of the algorithm is to simply iterate map \(x\mapsto x^x\mod 10^k\) 256 times (in order to find k digits). However, it's not immediately clear why this algorithm works: after all it's not true in general that \(a^b\equiv a^(b\mod m)\mod m\). Now I'll explain why it works anyways.

Start with \(x_0=256\) and define \(x_{n+1}=x_n^{x_n}\). Working mod \(2^k\), for moderately small \(k\), we see that either \(x_1=256^{256}\) will be \(0\) mod \(2^k\), or \(x_2=x_1^{x_1}\) will be. It follows easily that mega, which is \(x_{256}\), will be \(0\) mod \(2^k\). It's also easily seen that our algorithm will produce a number which is \(0\mod 2^k\). So we only need to check that the result is correct mod \(5^k\), for then we can use Chinese remainder theorem.

Define \(y_{n+1}=y_n^{y_n}\mod 10^k\) with \(y_0=256\). We will use induction on \(n\). Suppose \(x_n\equiv y_n\mod 5^k\). Then we can write \(x_{n+1}=x_n^{x_n}\equiv y_n^{x_n}\mod 5^k\). It's clear enough that \(4\mid x_n,4\mid y_n\) so that \(x_n\equiv y_n\mod 4\cdot 5^k\), and thus \(x_n\equiv y_n\mod 4\cdot 5^{k-1}=\varphi(5^k)\), so \(\varphi(5^k)\mid x_n-y^n\). Because \(5\nmid y_n\), we have \(y_n^{x_n}=y_n^{y_n}\cdot y_n^{x_n-y_n}\equiv y_n^{y_n}=y_{n+1}\mod 5^k\). So \(x_{n+1}\equiv y_{n+1}\), so inductive step works.