正方形の正方形


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

これはコードゴルフです。標準ルールが適用されます。最短の回答(バイト単位)が優先されます。

回答:


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 2016

@ Sp3000ありがとう!もう少しゴルフをしました。
grc 2016

@Amporaはideoneリンクのみがプロセスを印刷しましたが、今は変更しました。
grc 2016

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.sqrtm
Arcturus 2016

@Ampora Awwええ、2バイト節約
ThereGoesMySanity

1

Brachylog、26バイト

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

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

最後のテストケースだけでは実行に1分以上かかるため、TIOリンクでは省略されました。ラップトップで実行したところ、2時間以内に正しい結果が得られました。

{                             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.

最後から2番目は、最初の入力がすでに完全な正方形である場合に必要です。したがって、それを接頭辞として持つ最初の完全な正方形はそれ自体であり!、より大きな連結された正方形を見つけるのではなく、バックトラックが反復することを確認するために必要です。しかし、なぜ必要なのか本当にわかりません。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ます。

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