2のべき乗の最後のk桁


16

整数r場合、最後のr桁が1または2 である2の累乗が存在します。

rx2xmod10r

以下のために、、以降 の場合、、以来、 注:場合、は(もう一度)r=2x=929=512
r=3x=89289=618970019642690137449562112
r=4x=89

入力:r100

出力:x

例えば。

入力:2
出力:9

入力:3
出力:89

プログラムは妥当な時間内に実行する必要があります。

編集:このチャレンジのoeisシーケンスはA147884です。


2
このタスクのOEISはA147884
Quixotic

@Debanjan、はい本当です。S.Mark @、2のべき乗ではなく、3
st0le

効率的なアルゴリズムを説明する論文があります。誰かがそれを前進させることができない場合、私はそれを投稿します。
st0le

ああ、わかった、ありがとう!
あなた

@ st0le:複雑さ?
whacko__Cracko

回答:


4

Python、166文字

k,f,g=1,4,16
i=j=2
n=input()
m=10**n
a=lambda c:c('')-1-i or c('1')+c('2')-c('')+1
while i<=n:
 while a(str(j)[-i:].count):j,k=j*g%m,k+f
 i,g,f=i+1,g**5%m,f*5
print k

すばらしい仕事です、マーク:)見つけたと思います:)
st0le

セミコロンを使用して数バイトを保存できます:161バイト
movatica

2

Wolfram言語(Mathematica)78 76 57 55バイト

(x=0;While[Max@Abs[2IntegerDigits[2^++x,10,#]-3]>1];x)&

オンラインでお試しください!

IntegerDigits[a,10,r]r最後の10進数のリストを生成しますa。3/2を減算し、それらがすべて-1/2または+1/2であることを確認します。

タイミングチェック:TIOで20秒r = 1 .. 10

Wolfram言語(Mathematica)102 95 91 89バイト

k/.FindInstance[Mod[n=0;Nest[#+10^n(2-Mod[#/2^n++,2])&,0,#]-2^k,5^#]==0,k,Integers][[1]]&

オンラインでお試しください!

このソリューションははるかに長いですが、はるかに高速です。で提案されているパスとることによってOEIS A147884を経由して行くためにOEIS A053312だけでなく、使用してFindInstance魔法を、TIOは、計算するために管理してr = 1 .. 12分未満で。


1

Ruby-118文字

k,f,g,m=1,4,16
i=j=2
m=10**(n=gets.to_i)
((k+=f;j=j*g%m)until j.to_s=~%r{[12]{#{i}}$};i+=1;f*=5;g=g**5%m)until n<i
p k



1

05AB1E18 15 バイト

∞.Δo©‹®I.£2X:`P

オンラインそれを試してみてくださいまたは最初の8テストケースを検証する(任意の複数回出)。

説明:

2x>rr2x

∞.Δ            # Find the first positive integer x which is truthy (==1) for:
   o           #  Take 2 to the power the integer: 2^x
    ©          #  Store it in variable `®` (without popping)
              #  Check that it's larger than the (implicit) input: r < 2^x
               #  (1 if truhy; 0 if falsey)
    ®          #  Push variable `®` again: 2^x
     I       #  Only leave the last input amount of digits
        2X:    #  Replace all 2s with 1s
           `   #  Push all digits separated to the stack
    P          #  Take the product of all digits on the stack (including the earlier check)
               #  (NOTE: Only 1 is truthy in 05AB1E)

0

CSharp-111文字

int a(int r){int x=1;a:x++;foreach(var c in Math.Pow(2,x)%Math.Pow(10,r)+"")if(c!='1'&&c!='2')goto a;return x;}


弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.