简化平方根


29

给定一个正整数n,通过提取所有平方因子将平方根简化√n为形式a√b。输出的a,b应该是正整数,n = a^2 * b且整数应b尽可能小。

您可以输出ab以任何顺序在任何合理的格式。您可能不会忽略1as的输出。

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

相关:更复杂的版本


我们以前有过此功能,因此已关闭,作为此处链接的挑战的副本。
瑕疵的2016年

Answers:


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)

coren默认情况下返回的squarefree部分,但是将第二个参数标志设置为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)的数,,倍的平方根b应改为{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

JavaScript(ECMAScript 2016),40字节

n=>{for(k=n;n%k**2;k--);return[k,n/k/k]}

基本上是Dennis的Python 2 answer的JavaScript端口。

在JSBin上尝试

注意:它在严格模式下不起作用,因为k未在任何地方初始化。为了使其在严格模式下工作,k=n应将循环更改为let k=n


1

哈斯克尔,43> 42个字节

蛮力解决方案。

感谢Xnor,节省了1个字节

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

好的解决方案,我喜欢它不使用moddiv。我认为您可以这样做,y<-[1..]因为懒惰。
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

在线尝试


1

Python,74字节

def e(k):a=filter(lambda x:k/x**2*x*x==k,range(k,0,-1))[0];return a,k/a**2

直截了当。


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
请尝试尽可能多地回答您的问题,这是密码高尔夫球
caird coinheringaahing

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]

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.