首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >输出2的最小次方,并有两个相同的数字子串n长。

输出2的最小次方,并有两个相同的数字子串n长。
EN

Code Golf用户
提问于 2016-02-16 07:17:11
回答 3查看 502关注 0票数 10

对于每一个整数n,0或更高,输出的最小幂为2,它有两个相同的小数位子串,以及数字子字符串开始的两个索引(基于0)。

(不输出) 0 => 0 0 1 1 => 16 0 4 1 => 16 1 2 2 => 24 0 6 2 => 24 2 3

前三项投入的可能产出见上文。当一个给定的输入有多个输出时,两者都是可以接受的。只有数字位置是不同的。只要输出数字,括号只为清晰起见。你不需要输出n。

输出一分钟内所能找到的尽可能多的列表,否则您的代码将耗尽资源,以第一位为准。请包括你的答案的输出。如果列表很大,只需将其限制在第一个和最后一个5。您的代码必须能够在一分钟内找到n=9。我编写了一个Perl程序,在大约20秒后找到n=9,所以这应该很容易做到。

您的代码不需要自动停止。如果你在一分钟后手动爆发,这是可以接受的。

这是密码-高尔夫,所以最少的字节数获胜!

EN

回答 3

Code Golf用户

发布于 2021-01-14 04:32:13

Dyalog扩展,68字节

代码语言:javascript
复制
'01'(0{x≢`∪x←⍺⍺,/⍺:⍺((⍺⍺+1)∇∇)⊃⎕←⍵,⊃a/⍨</¨⊢a←⍸∘.≡⍨x⋄(+⌂big⍨⍺)∇1+⍵})0

在网上试试!

APL不以大整数运算、无限输出或解释器速度而闻名(使用扩展会带来更多开销),但它的成功输出仅不到一分钟就能达到n=9

是如何工作的

代码基本上是一个无限运行的递归运算符。左参数是2次方的字符串表示,右参数是对应的指数(满足速度要求所需),左操作数是n的当前值。

代码语言:javascript
复制
{
  x←⍺⍺,/⍺          ⍝ Extract length-n segments of given number
  x≢`∪x:           ⍝ If it has duplicates...
  a←⍸∘.≡⍨x         ⍝   Construct the pairs of indices of equal slices
  ⊃a/⍨</¨⊢a        ⍝   Keep the ones (L,R) where L<R and take the first one
  ⊃⎕←⍵,...         ⍝   Concat with the exponent, print it, extract ⍵ back
  ⍺((⍺⍺+1)∇∇)...   ⍝   Continue running with n+1
  ⋄                ⍝ Otherwise...
  (+⌂big⍨⍺)∇1+⍵    ⍝   Recurse with next power of two, using same n implicitly
}
'01'(0{...})0  ⍝ Invoke the dop with starting values
票数 5
EN

Code Golf用户

发布于 2016-02-16 07:56:49

Mathematica,109个字节

代码语言:javascript
复制
f@n_:={NestWhile[#+1&,0,Length[p=Position[#,#&@@Commonest@#,1,2]-1]&@Partition[IntegerDigits[2^#],n,1]<2&],p}
代码语言:javascript
复制
n    output
0    {0, {{0},{1}}}
1    {16, {{0},{4}}}
2    {24, {{0},{6}}}
3    {41, {{8},{9}}}
4    {73, {{10},{18}}}
5    {130, {{14},{17}}}
6    {371, {{8},{52}}}
7    {875, {{68},{101}}}
8    {2137, {{12},{240}}}
9    {2900, {{270},{355}}}
10   {7090, {{803},{1123}}}
11   {12840, {{1672},{3185}}}
票数 4
EN

Code Golf用户

发布于 2021-03-24 23:19:14

JavaScript (ES6),111个字节

代码语言:javascript
复制
n=>(C=i=>(I=S.indexOf(S.slice(i,i+n)))==i?S[i+n]?C(i+1):-1:i,P=p=>(z=C(0,S=(2n**p+'')))<0?P(++p):p,[P(0n),I,z])

它计算前9个相当快,但随后达到最大的调用堆栈大小的n=10

代码语言:javascript
复制
[0n, 0, 1]
[16n, 0, 4]
[24n, 0, 6]
[41n, 8, 9]
[73n, 10, 18]
[130n, 14, 17]
[371n, 8, 52]
[875n, 68, 101]
[2137n, 12, 240]
[2900n, 270, 355]

下面是它的工作原理:

代码语言:javascript
复制
n=>(                                // take n as input
  C=i=>                             // C finds recurring match in string S
    (I=S.indexOf(S.slice(i,i+n)))   // calculate the first index of a substring
      ==i                           // the index of the substring being tested
    ?                               // if those are equal, no match
      S[i+n]                        // if we're not at end of string,
      ?C(i+1)                       // continue recursing
      :-1                           // otherwise, return -1 (no match)
    :i,                             // we have a match, return i
  P=p=>                             // loops over powers of 2 and check matches
    (z=C(0,S=(2n**p+'')))<0         // check 2^p for match
    ?P(++p),                        // if no match, continue recursing
    :p                              // if we find a match, return the power
  [P(0n),I,z]                       // return power, first and last index
)
票数 0
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/73258

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档