平方根を単純化する


29

正の整数nを指定すると、すべての平方因子を抽出√nすることa√bにより平方根を形式に単純化します。出力されたa,bとの正の整数でなければなりませんn = a^2 * bbできる限り小さく。

あなたは、出力してもよいaし、b任意の合理的な形式のいずれかの順序で。の出力を1暗黙的に省略できません。

n=1..36as の出力(a,b)

1 (1, 1)
2 (1, 2)
3 (1, 3)
4 (2, 1)
5 (1, 5)
6 (1, 6)
7 (1, 7)
8 (2, 2)
9 (3, 1)
10 (1, 10)
11 (1, 11)
12 (2, 3)
13 (1, 13)
14 (1, 14)
15 (1, 15)
16 (4, 1)
17 (1, 17)
18 (3, 2)
19 (1, 19)
20 (2, 5)
21 (1, 21)
22 (1, 22)
23 (1, 23)
24 (2, 6)
25 (5, 1)
26 (1, 26)
27 (3, 3)
28 (2, 7)
29 (1, 29)
30 (1, 30)
31 (1, 31)
32 (4, 2)
33 (1, 33)
34 (1, 34)
35 (1, 35)
36 (6, 1)

これらはOEIS A000188A007913です。

関連:より複雑なバージョン


私たちは持っていたこの前に、それはここにリンクされ挑戦の重複として閉鎖されました。
フレイ

回答:


13

ゼリー、9 バイト

ÆE;0d2ZÆẸ

オンラインでお試しください!または、すべてのテストケースを確認します

使い方

ÆE;0d2ZÆẸ  Main link. Argument: n

ÆE         Exponents; generate the exponents of n's prime factorization.
  ;0       Append 0 since 1ÆE returns [].
    d2     Divmod by 2.
      Z    Zip/transpose to group quotients and remainders.
       ÆẸ  Unexponent; turn the exponents of prime factorizations into integers.

3
UTF-8ではそうですが、Jellyはカスタムコードページを使用します。ヘッダーのバイトリンクはそれを指します。
デニス

:あなたはので、多分あなたは、たとえば(明確のようにバイトをしなければならない、そのコメントをたくさん掲載[bytes](link-to-byes) (not UTF-8)
NoOneIsHere

12

PARI / GP、12バイト

n->core(n,1)

coreは、nデフォルトで平方自由部分を返しますが、2番目の引数フラグを1に設定すると、両方の部分が返されます。出力順序は(b, a)、たとえば(n->core(n,1))(12) -> [3, 2]です。



6

MATL、12バイト

t:U\~f0)GyU/

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

説明

t     % Take input n implicitly. Duplicate
:U    % Push [1 4 9 ... n^2]
\~    % True for entries that divide the input
f0)   % Get (1-based) index of the last (i.e. largest) dividing number
G     % Push input again
y     % Duplicate index of largest dividing number
U     % Square to recover largest dividing number
/     % Divide input by that. Implicitly display stack


2

Mathematica 34バイト

#/.{a_ b_^_:>{a, b},_[b_,_]:>{1,b}}&

これは#、次の規則に従ってすべての入力()を置き換えることを意味します。(1)数値、abの平方根を{a, b} 、関数bを{1、b } 関数は、入力が次の形式であると想定していることに注意してください。Sqrt[n]。他の種類の入力では機能しません。

この名前のない関数はMathematicaにとって非常に不可解です。完全な形式を表示し、その後に元の短い形式を置き換えることにより、ある程度明確にすることができます。

Function[
   ReplaceAll[
      Slot[1],
      List[
         RuleDelayed[Times[Pattern[a,Blank[]],Power[Pattern[b,Blank[]],Blank[]]],List[a,b]],
         RuleDelayed[Blank[][Pattern[b,Blank[]],Blank[]],List[1,b]]]]]

と同じです

   ReplaceAll[
      #,
      List[
         RuleDelayed[Times[Pattern[a,Blank[]],Power[Pattern[b,Blank[]],Blank[]]],List[a,b]],
         RuleDelayed[Blank[][Pattern[b,Blank[]],Blank[]],List[1,b]]]]&

そして

ReplaceAll[#, 
  List[RuleDelayed[
    Times[Pattern[a, Blank[]], 
     Power[Pattern[b, Blank[]], Blank[]]], {a, b}], 
   RuleDelayed[Blank[][Pattern[b, Blank[]], Blank[]], {1, b}]]] &

そして

ReplaceAll[#, 
  List[RuleDelayed[Times[a_, Power[b_, _]], {a, b}], 
   RuleDelayed[Blank[][b_, _], {1, b}]]] &

そして

ReplaceAll[#, {RuleDelayed[a_*b^_, {a, b}], RuleDelayed[_[b_, _], {1, b}]}]&

そして

ReplaceAll[#, {a_*b^_ :> {a, b}, _[b_, _] :> {1, b}}] &


1

Matlab、51バイト

x=input('');y=1:x;z=y(~rem(x,y.^2));a=z(end)
x/a^2

説明

x=input('')       -- takes input
y=1:x             -- numbers from 1 to x
z=y(~rem(x,y.^2)) -- numbers such that their squares divide x
a=z(end)          -- biggest such number (first part of output)
x/a^2             -- remaining part


1

Haskell、43> 42バイト

ブルートフォースソリューション。

Xnorのおかげで1バイト節約

f n=[(x,y)|y<-[1..],x<-[1..n],x*x*y==n]!!0

素敵な解決策、私はそれが使用しない方法が好きですmodまたはdiv。あなたはy<-[1..]怠dueのためにできると思います。
-xnor

はい、あなたは正しいです。私の最初の解決策では不可能でしたlast[(x,y)|x<-[1..n],y<-[1..n],x*x*y==n]が、今では機能します。ありがとう。Haskellに独自のソリューションがありますか?
ダミアン

1

05AB1E、14バイト

Lv¹ynÖi¹yn/y‚ï

説明した

Lv              # for each x in range(1,N) inclusive
  ¹ynÖi         # if N % x^2 == 0
       ¹yn/y‚ï  # create [N/x^2,x] pairs, N=12 -> [12,1] [3,2]
                # implicitly output last found pair

オンラインで試す



0

Python 2.7(未使用)-181バイト

def e(n):   
 for x in range(1,n+1):
  r=(1,x)
  for i in range(1,x+1):
   l=i**.5
   for j in range(1,x+1): 
    if i*j==x and l%1==0 and j<r[1]:r=(int(l),j)                
  print x,r

実行:e(number)例。e(24)

サンプル出力:

>> e(24)
1 (1, 1)
2 (1, 2)
3 (1, 3)
4 (2, 1)
5 (1, 5)
6 (1, 6)
7 (1, 7)
8 (2, 2)
9 (3, 1)
10 (1, 10)
11 (1, 11)
12 (2, 3)
13 (1, 13)
14 (1, 14)
15 (1, 15)
16 (4, 1)
17 (1, 17)
18 (3, 2)
19 (1, 19)
20 (2, 5)
21 (1, 21)
22 (1, 22)
23 (1, 23)
24 (2, 6)

1
可能な限りあなたの答えをゴルフしてみてください。これはコードゴルフです
ケアード・コイナーリンガー

0

APL、25文字

 {(⊢,⍵÷×⍨)1+⍵-0⍳⍨⌽⍵|⍨×⍨⍳⍵}

英語で:

  • 0⍳⍨⌽⍵|⍨×⍨⍳⍵:完全にnを分割するnまでの最大の正方形のインデックス。
  • 1+⍵-:インデックスは逆配列にあるため、インデックスを調整します
  • (⊢,⍵÷×⍨):結果を生成します:インデックス自体(a)と商b(つまり、n÷a * a)

テスト:

     ↑{(⊢,⍵÷×⍨)⊃z/⍨0=⍵|⍨×⍨z←⌽⍳⍵}¨⍳36
1  1
1  2
1  3
2  1
1  5
1  6
1  7
2  2
3  1
1 10
1 11
2  3
1 13
1 14
1 15
4  1
1 17
3  2
1 19
2  5
1 21
1 22
1 23
2  6
5  1
1 26
3  3
2  7
1 29
1 30
1 31
4  2
1 33
1 34
1 35
6  1

0

JavaScript(ECMAScript 6)、35バイト

f=(n,k=n)=>n/k%k?f(n,--k):[k,n/k/k]

JavaScript 1 +、37 B

for(k=n=prompt();n/k%k;--k);[k,n/k/k]

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