这个数字怎么样?


13

费马数是可表示为2 2 x +1且带整数x的正整数。

现在让我们定义一个称为“ Fermat-ness”的数字的属性:

  • 从基数开始,该数的费马性比二的幂的链的长度小一,其中二的幂被扩展以最大化费马性。
  • 非费马数的数字的费马率为零。

因此,17(= 2 2 2 2 0 +1)具有费马能级3。

挑战

给定一个非零正整数作为输入,输出数字的费马性。

规则

  • 您可以采用二进制,十进制,十六进制,大数字等输入形式,也可以采用让您最满意的格式
  • 您的解决方案必须能够处理长度超过64的数字,无论您使用哪种表示形式。
  • 仅非负整数幂。
  • 当然禁止出现标准漏洞
  • 这是,因此最短的答案会获胜。

测试用例

这些是格式input->output。输入为十六进制以节省空间。

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

十进制相同:

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

感谢geokavel在沙盒中的宝贵投入。


1
如果我输入1111,您怎么知道它是二进制,十进制还是十六进制???
J42161217

1
@Jenny_mathy我的意思是让回答者决定他们想要哪种输入格式。
HAEM

@ Mr.Xcoder出现在沙箱中的是,实际上并没有很多64位或更少的Fermat数字。我声称问题本质上是关于bignum的,所以我可以要求bignum处理。
HAEM

2
@HeikkiMäenpää请记住,无论别人提出什么建议,挑战都是您的挑战,您可以做到自己想要的。
isaacg

3
我认为为时过早,也不能接受。通常等待1或2周。有人说永不接受!
geokavel

Answers:




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个字节。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.