// Magma program for OEIS A280660: // "Least k such that at least half of the last n digits of 2^k are 9." nMax:=27; nDgtsPerBlock:=7; power10:=10^nDgtsPerBlock; // precompute and store the counts of 9s // for each integer from 0 through power10-1 block9countsOffset1:=[0]; // offset by 1 because Magma apparently (?) doesn't // offer a way to dimension an array that has an // element with index 0 // compute and store the counts of 9s for j in [1..power10-1] do if j mod 10 eq 9 then block9countsOffset1[j+1]:=block9countsOffset1[(j div 10)+1]+1; else block9countsOffset1[j+1]:=block9countsOffset1[(j div 10)+1]; end if; end for; for n in [2..nMax] do numFullBlocks:=n div nDgtsPerBlock; numDgtsPartialBlock:=n mod nDgtsPerBlock; power10partialBlock:=10^numDgtsPartialBlock; fullBlockVal:=[]; for j in [1..numFullBlocks] do fullBlockVal[j]:=0; end for; if numFullBlocks gt 0 then fullBlockVal[1]:=1; partialBlockVal:=0; else partialBlockVal:=1; end if; k:=0; while true do k+:=1; l9count:=0; carry:=0; for iDgtBlock in [1..numFullBlocks] do temp:=2*fullBlockVal[iDgtBlock]+carry; if temp ge power10 then fullBlockVal[iDgtBlock]:=temp-power10; carry:=1; else fullBlockVal[iDgtBlock]:=temp; carry:=0; end if; l9count+:=block9countsOffset1[fullBlockVal[iDgtBlock]+1]; end for; partialBlockVal:=(2*partialBlockVal+carry) mod power10partialBlock; l9count+:=block9countsOffset1[partialBlockVal+1]; if l9count*2 ge n then n, k; break; end if; end while; end for;