この数はフェルマーですか?


13

フェルマー数は正の整数で、整数xで2 2 x +1 として表現できます。

「Fermat-ness」と呼ばれる番号の属性を定義しましょう。

  • 数のフェルマーネスは、基数から始まり、フェルマーネスを最大化するために2のべき乗が拡張された2のべき乗の長さよりも1つ小さくなります。
  • フェルマー数ではない数のフェルマーネスはゼロです。

したがって、17(= 2 2 2 2 0 +1)にはフェルマーネス3があります。

チャレンジ

入力として正の非ゼロ整数を与え、数値のフェルマーネスを出力します。

ルール

  • 入力を2進数、10進数、16進数、bignum、またはゴルフに最適な形式で入力できます。
  • ソリューションは、使用する表現が64を超えるビット長の数値を処理できる必要があります。
  • 非負の整数のべき乗のみ。
  • もちろん、標準的な抜け穴は禁止されています。
  • これはなので、最短の回答が勝ちます。

テストケース

これらはフォーマットですinput->output。入力はスペースを節約するために16進数です。

10000000000000000000000000000000000000000000000000000000000000001 -> 2
1000000000000BC00000000000000000000000000000000001000000000000001 ->0
1234567890ABCDEF -> 0
100000000000000000000000000000001 -> 1
5 -> 2
11 -> 3
10001 -> 4
101 -> 1

10進数で同じ:

115792089237316195423570985008687907853269984665640564039457584007913129639937 -> 2
115792089237316497527923305698859709742143344804209838213621568094470773145601 -> 0
1311768467294899695 -> 0
340282366920938463463374607431768211457 -> 1
5 ->2
17 -> 3
65537 -> 4
257 -> 1

サンドボックスへの貴重な入力をしてくれたgeokavelに感謝します。


1
1111を入力した場合、それが2進数、10進数、または16進数であることをどのように確認しますか?
J42161217

1
@Jenny_mathy回答者が入力の形式を決定することを意図していました。
HAEM

@ Mr.Xcoderサンドボックスでは、64ビット以下のフェルマー数が実際には多くないことがわかりました。私は質問が本質的にビッグナムに関するものであると主張しているので、ビッグナム処理を要求することができます。
HAEM

2
@HeikkiMäenpää覚えておいて、他の人が何を推薦しても、挑戦はあなたのものであり、あなたが望むものを作ることができます。
isaacg

3
あまりにも早く受け入れられると思います。通常、1〜2週間待ちます。決して受け入れないと言う人もいます!
-geokavel

回答:




0

RProgN 2、75バイト

«\`n=1\]{1-\n*\]}:[»`^=«1-`n=001{]2\^2\^ne{2\^`n=1+0}{1+}?]2\^2\^n>¬}:[»`¤=

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

あなたが追加しない場合、それはわずか70バイトだ«»'¤=にFermatness計算を割り当てるを¤キャラクターに。その場合は、現在のようにフッターではなくTIOのヘッダーセクションに番号を入力する必要があります。

これは、私のPythonの答えと同じロジックを効果的に使用するため、RProgN 2の動作を気にしない場合は、何が起こっているのかを説明するためにそれを見てください。さもないと

コードの内訳:

«\`n=1\]{1-\n*\]}:[»`^=
«                  »`^=`                            # Create a local function and assign it to the ^ character (x y ^ is x to the power of y)
 \`n=                                               # Swap the top two values of the stack and assign the new top to the variable n
     1\]                                            # Push a 1 (our starting point for x to the y), swap with the y value, then duplicate y
        {       }:                                  # Start a while loop that pops the top stack value and loops if it is truthy
         1-                                         # Subtract 1 from y to keep a tally of how many multiplications we've done
           \n*                                      # Swap the counter with our current value and multiply it by n
              \]                                    # Swap this new value with the current value of y, and duplicate it to be used as the truthy value for the loop

«1-`n=001{]2\^2\^ne{2\^`n=1+0}{1+}?]2\^2\^n>¬}:[»`¤=# The main Fermatness function (x ¤ to get the Fermatness of x)
«                                               »`¤=# Create another local function for this calculation
 1-`n=                                              # Decrement the input by 1 and assign it to n
      001                                           # Push a counter for Fermatness, a counter for calculating 2^2^i, and an initial truthy value
         {                                   }:     # Start a while loop for calculating the Fermatness
          ]2\^2\^ne                                 # Duplicate i, calculate 2^2^i, and compare it to n
                   {         }{  }?                 # Start an if statement based on the equality of 2^2^i and n
                    2\^`n=                          # n==2^2^i, so set n to 2^i (same as saying n=log_2(n))
                          1+0                       # Increment the Fermatness counter and reset i
                               1+                   # n!=2^2^i, so just increment i
                                   ]2\^2\^n>¬       # Duplicate the counter and check if 2^2^i<=n, if true the loop continues, else it exits
                                               [    # Pop i from the stack, leaving us with just the Fermatness counter

残念ながら、対数関数Šと通常のべき乗関数^には、これをネイティブに行う精度が不足しているため、乗算の精度がはるかに高いため、べき乗のしくみを再定義する必要がありました。その再定義がなければ、この答えは23バイト短くなります。


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