广场中的广场


10

给定正整数的输入n,编写一个程序来完成以下过程。

  • 找到大于n该整数的最小正整数,它是一个完美的正方形,是n和其他数的串联。的数字顺序n不得更改。级联n以产生一个完美平方的数字可以称为r_1
  • 如果r_1不是理想的正方形,则重复上述过程,r_1并将其作为该过程的新输入。重复直到r_k是表示的完美正方形s
  • 打印的值sqrt(s)

输入可以采用任何格式。您可以假定这n是一个正整数。如果任何r_k一个具有前导零(且r_k≠0),则可以忽略零。


测试用例

这是一些测试用例。该过程演示了上述步骤。

Input:   23
Process: 23, 2304, 4
Output:  2

Input:   10
Process: 10, 100, 0
Output:  0

Input:   1
Process: 1, 16, 6, 64, 4
Output:  2

Input:   5
Process: 5, 529, 29, 2916, 16
Output:  4

Input:   145
Process: 145, 145161, 161, 16129, 29, 2916, 16
Output:  4

Input:   1337
Process: 1337, 13373649, 3649, 36493681, 3681, 368102596, 2596, 25969216, 9216
Output:  96

这是代码高尔夫。适用标准规则。最短的答案(以字节为单位)获胜。

Answers:


2

Pyth,26个字节

LsI@b2 fy=sh.fys+QZ1\0)@Q2

测试套件

输出为浮点型。如果需要以int形式输出,则它将为1个额外字节。

说明:

LsI@b2 fy=sh.fys+QZ1\0)s@Q2
                               Q = eval(input())
L                              def y(b): return
   @b2                         Square root of b
 sI                            Is an integer.
       f              )        Find the first positive integer T that satisfies
           h.f     1\0         Find the first digit string Z that satisfies
                +QZ            Concatenation of Q and Z
               s               Converted to an integer
              y                Is a pergect square.
          s                    Convert the string to an integer
         =                     Assign result to the next variable in the code, Q
        y                      Repeat until result is a perfect square
                               (The space) Discard return value
                        @Q2    Take square root of Q and print.

2

MATL,35 44.0字节

XK``x@2^tVKVXf1=a~]VKVnQ0h)UXKX^t1\

在线尝试!

XK        % implicit input: n. Copy to clipboard K
`         % do...while. Each iteration applies the algorithm
  `       %   do...while. Each iteration tests a candidate number
    x     %     delete top of stack
    @2^   %     iteration index squared
    t     %     duplicate
    V     %     convert to string                
    K     %     paste from clipboard K: n or r_k
    V     %     convert to string  
    Xf    %     find one string within another. Gives indices of starting matches, if any 
    1=a~  %     test if some of those indices is 1. If not: next iteration
  ]       %   end. We finish with a perfect square that begins with digits of n or r_k
  V       %   convert to string
  K       %   paste from clipboard K: n or r_k
  VnQ0h   %   index of rightmost characters, as determined by r_k
  )       %   keep those figures only
  U       %   convert to number. This is the new r_k
  XK      %   copy to clipboard K, to be used as input to algorithm again, if needed
  X^      %   square root
  1\      %   fractional part. If not zero: apply algorithm again
          % implitic do...while loop end
          % implicit display

2

python 2,98

i=input();d=o=9
while~-d:
 n=i;d=o+1;o=i=0
 while(n*d+i)**.5%1:i=-~i%d;d+=9*d*0**i
print'%d'%n**.5

在线尝试


既然我们还是处在花车滥用领域... while x**.5%1:也许?
Sp3000 '16

@ Sp3000谢谢!我现在打得更多了。
grc

@Ampora仅使用ideone链接打印了该过程,但我现在已对其进行了更改。
grc

1

Python中,200个 198 178字节

import math
def r(i):
 j=int(i**.5)+1
 while str(j*j)[:len(str(i))]!=str(i):j+=1
 return int(str(j*j)[len(str(i)):])
q=r(int(input()))
while math.sqrt(q)%1!=0:q=r(q)
print(q**.5)

缩短math.sqrt为可以节省大量字节m
Arcturus

@Ampora Aww是的,保存了2个字节
ThereGoesMySanity '16

1

Brachylog,26个字节

{~a₀X√ℕ∧YcX∧Yh?∧Ybcℕ≜!}ⁱ√ℕ

在线尝试!

在TIO链接中省略了最后一个测试用例,因为仅此一项就需要花费一分钟以上的时间来执行。我在笔记本电脑上运行它,并在不超过两个小时的时间内获得了正确的结果。

{                             The input
 ~a₀                          is a prefix of
    X√                        X, the square root of which
      ℕ                       is a whole number.
       ∧YcX                   Y concatenated is X.
           ∧Yh?               The input is the first element of Y.
               ∧Yb            The rest of Y,
                  c           concatenated,
                      }       is the output
                   ℕ          which is a whole number.
                    ≜         Make sure that it actually has a value,
                     !        and discard all choice points.
{                     }ⁱ      Keep feeding that predicate its own output until
                        √     its output's square root
                         ℕ    is a whole number
                              which is the output.

当初始输入已经是一个完美正方形时,倒数第二个是必需的,因此以它为前缀的第一个完美正方形本身就是它,并且!有必要确保迭代回溯而不是查找更大的串联正方形,但我真的不知道为什么要这样做,我只知道5没有它就会产生错误的答案。


(由于解析器中的错误,命名变量和s的可怕混乱实际上比使用三明治更短。)
不相关的字符串

0

Perl 6,101个字节

my&q={$^k;$_=({++($||=$k.sqrt.Int)**2}.../^$k/)[*-1];+S/$k//}
put (q(get),&q...!(*.sqrt%1))[*-1].sqrt
my &q = {
  $^k; # declare placeholder parameter
  # set default scalar to:
  $_ = ( # a list
    # code block that generates every perfect square
    # larger than the input
    { ++( $ ||= $k.sqrt.Int )**2 }
    ...   # produce a sequence
    /^$k/ # ending when it finds one starting with the argument
  )[*-1]; # last value in sequence

  # take the last value and remove the argument
  # and turn it into a number to remove leading zeros
  +S/$k//
}

put (     # print the result of:
  q(get),     # find the first candidate
  &q          # find the rest of them
  ...         # produce a sequence
  !(*.sqrt%1) # ending with a perfect square
)[*-1]        # last value in sequence
.sqrt         # find the sqrt

0

ES7,116个字节

n=>{do{for(i=n;!(r=(''+Math.ceil((i*=10)**0.5)**2)).startsWith(+n););n=r.replace(+n,'');r=n**0.5}while(r%1);return r}

是的,我可能可以使用保存一个字节eval

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.