毕达哥拉斯的另一条腿


33

毕达哥拉斯的腿在战争中被炸断了。它必须被截肢,尽管他快要死了,他还是挺身而出,完全康复了。现在,用拐杖走路一年后,他有幸得到假肢!不过,有几种合适的方法,但是哪一种适合呢?

任务

给定一个正整数作为毕达哥拉斯三重奏的一条腿的长度的输入,输出另一条腿的所有可能性。例如,毕达哥拉斯最小的三元组是(3,4,5),它形成一个三角形,其两个腿的长度分别为3和4,长度为5的斜边。

例子

Leg:5
12

Leg:28
21
45
96
195

Leg:101
5100

Leg:1001
168
468
660
2880
3432
4080
5460
6468
10200
38532
45540
71568
501000

规则

  • 输入将是单个正整数n
  • 输出可以是任何顺序,可以使用任何定界符,可以使用任何基数(尽管该基数​​必须一致),并且可以使用可选的左括号和右括号,以及可选的尾随空格。也就是说,1 2 3[1,2,3],和1,11,111所有适合这种输出规格。
  • 您可能会假设该值n永远不会大于语言限制的第四根的四分之一(不使用库)。实际上,您可以假定输入将小于此值或10,000,以较小者为准。

毕达哥拉斯正等着你,所以最好快而简短地编写代码!


18
他是一个很奇怪的人。他愿意等待几千年的时间来发明计算机,但不会再花几纳秒的时间来读取几百个字节。至少可以说,一个非常精确的人。
corsiKa 2015年

Answers:



11

果冻,8字节

²R²+²Æ²O

这个答案是非竞争性的,因为它使用了发布挑战后已实现的功能。 在线尝试!

这种方法不使用浮点数学,因此只要中间列表可以放入内存,它就会给出正确的答案。

理念

如果(A,B,C)是毕达哥拉斯三重,有严格的正整数K,M,N,使得设定平等{A,B} = {千米2 -千牛2,2kmn}成立。

特别地,这意味着,A <B 2B < 2,所以对于输入一个我们可以简单地检查是否一个2 + B 2是每个完美的正方形b{1,...一个2 }

            Input: x

²           Compute x².
 R          Get get range 1 ... x².
  ²         Square each integer in that range.
   +²       Add x² to each resulting square.
     Ʋ     Check if the resulting sums are perfect squares.
       O    Get all indices of ones.

10

朱莉娅,35个字节

n->filter(i->hypot(i,n)%1==0,1:n^2)

这是一个接受整数并返回数组的匿名函数。

对于i从1到输入平方的每一个,我们使用Julia的内置hypot函数计算斜边,并确定小数部分是否为0。如果是,则保留它,否则将其排除。


6

CJam,17个字节

{:A2#{Amh1%!},1>}

这是一个匿名函数,该函数从堆栈中弹出一个整数,并返回一个数组。

在线尝试!

理念

如果(A,B,C)是毕达哥拉斯三重,有严格的正整数K,M,N,使得设定平等{A,B} = {千米2 -千牛2,2kmn}成立。

特别地,这意味着,A <B 2B < 2,所以对于输入一个我们可以简单地检查是否一个2 + B 2是每个完美的正方形b{1,...一个2 }

:A               Save the input in A.
  2#             Square it.
    {      },    Filter; for each B in {0, ..., A**2}:
     Amh           Calculate the hypotenuse of (A, B).
        1%!        Apply logical NOT to its fractional part.
                 Keep B if ! pushed 1.
             1>  Discard the first kept B (0).  

4

JavaScript ES6,60 62

与其他答案相同,从1到a * a-1

a=>[...Array(a*a).keys()].filter(b=>b&&!(Math.hypot(a,b)%1))

Thx @ Mwr247 是在ES6中建立范围的最短方法

@ETHproductions保存了2个字节


太棒了!我认为您可以使用内置代码节省一些字节:a=>[...Array(a*a).keys()].filter(b=>b&&!(Math.hypot(a,b)%1))
ETHproductions 2015年

@ETHproductions thx,我需要了解有关新的数学内置
函数的

在您已经链接的页面上也方便地讨论了它们。(我本人会提出建议,但当时我还没有登录。)
Neil

3

C,96字节

交替增加y(另一条腿)和z(斜边),直到它们的差降至1。输出c==0您在途中遇到的每个完全匹配()。

int x,y,z;main(int c,char**a){for(x=z=atoi(a[1]);++y<z;c=x*x+y*y-z*z,c?z+=c>0:printf("%d ",y));}

n为参数调用已编译的程序;它将输出用空格分隔的十进制数字列表。

显然不是最短的;拥有最快的产品可能会给我带来安慰。

$ time ./pyth 9999
200 2020 13332 13668 16968 44440 45360 54540 55660 137532 164832 168168 413080 494900 504900 617120 1514832 1851468 4544540 5554440 16663332 49990000 
real    0m0.846s
user    0m0.800s
sys     0m0.000s


3

Wolfram语言(Mathematica),40个字节

b/.Solve[#^2+b^2==c^2,PositiveIntegers]&

我使用的是无证形式Solve:当省略变量列表时,Solve默认为求解表达式中的所有符号。因此,我们比常规的节省6个字节Solve[#^2+b^2==c^2,{b,c},PositiveIntegers]

PositiveIntegers是Mathematica版本12中的新增功能,因此在TIO中不可用。在桌面Mathematica中,我们得到

F = b/.Solve[#^2+b^2==c^2,PositiveIntegers]& ;

F[5]
(*    {12}    *)

F[28]
(*    {21, 45, 96, 195}    *)

F[101]
(*    {5100}    *)

F[1001]
(*    {168, 468, 660, 2880, 3432, 4080, 5460, 6468, 10200, 38532, 45540, 71568, 501000}    *)

2

Python 2,53字节

lambda n:[i for i in range(1,n*n)if abs(i+n*1j)%1==0]

一个简单的解决方案,使用复数abs来计算斜边的长度。n*n用作另一条腿的上限是安全的,因为(n*n)^2 + n^2 < (n*n+1)^2。我尝试使用递归代替,但是没有得到任何更短的内容。


2

认真地,20个字节

,;╗ªDR;`╜@ÇA1@%Y`M@░

与xnor的Python回答相同的策略:检查i in range(1,n*n)where的值abs(i+nj) % 1 == 0,然后输出列表。在线尝试

说明:

,;╗    get input and save a copy in register 0
ªDR;   push two copies of range(1,n*n)
`╜@ÇA1@%Y`M    map the function across one of the ranges:
    ╜@ÇA         compute abs(i+nj)
    1@%Y         push 1 if result % 1 is 0, else 0
M@░    swap the two lists, take values in the original range where the corresponding values in the second range are truthy


2

APL(NARS),373个字符,746个字节

C←{h←{0=k←⍺-1:,¨⍵⋄(k<0)∨k≥i←≢w←⍵:⍬⋄↑,/{w[⍵],¨k h w[(⍳i)∼⍳⍵]}¨⍳i-k}⋄1≥≡⍵:⍺h⍵⋄⍺h⊂¨⍵}⋄P←{1≥k←≢w←,⍵:⊂w⋄↑,/{w[⍵],¨P w[a∼⍵]}¨a←⍳k}⋄d←{∪×/¨{k←≢b←1,π⍵⋄∪{b[⍵]}¨↑∪/101 1‼k k}⍵}⋄t←{(-/k),(×/2,⍵),+/k←⍵*2}⋄b←{⍬≡a←3 C d w←⍵:(⊂1,⍵,1)⋄(⊂1,⍵,1),a/⍨{⍵[2]>⍵[3]}¨a←↑∪/P¨,a/⍨{w=×/⍵}¨a}⋄u←{(↑⍵),2÷⍨(+/a),-/a←1↓⍵}⋄t1←{(↑¨⍵)×t¨1↓¨⍵}⋄f1←{0=2∣⍵:↑¨t1 b⍵÷2⋄{2⊃⍵}¨t1 u¨b⍵}⋄f←{m←⎕ct⋄⎕ct←0⋄r←f1⍵⋄⎕ct←m⋄r}

评论:

C: ⍺ combination in ⍵ list
P: permutations  in ⍵ list
d: divisors of ⍵ unsigned
t: Pythagorian triple from ⍵ list 2 unsigned
b: if argument ⍵ is one unsigned it would return the list of (k,i,j) where 
   k,i,j are all divisors of ⍵, and ⍵=k×i×j and i>j
u: from one triple (k,i,j) return (k,(i+j)/2,(i-j)/2)
t1: apply (k,i,j) to t in the way  k×t i,j 
f: the function of this exercise

这个想法将成为输入的因数,以便知道使用所有以输入为腿的毕达哥拉斯三元组生成的可能的m,n。测试:

  f 18298292829831839x
167413760243137645229428509060960 15219432749376149566311682641900 99808869980900940 
  1383584795397831778755607512840 
  f 5
12
  f 28
195 96 21 45 
  f 101
5100
  f 1001
501000 6468 38532 2880 468 660 168 5460 45540 4080 71568 3432 10200 
  ≢f 1001
13
  f 1663481166348349x
1383584795397831778755607512900 
  f 198820182831x
19764732550476133587280 346749693868002343608 5664631173992 6083327962596530720 613900915408 115583231289334114460 
  18249983887789596492 1883559626820 1040249081604007030900 54749951663368790920 6588244183492044529092 
  265093577108 2196081394497348176360 

2

APL(Dyalog扩展)15 14 字节 SBCS

匿名默认前缀功能。

(⍸⊢(+∊⊢)⍳×⍳)×⍨

在线尝试!

×⍨ 自变量的平方(照度乘法自拍照)

() 应用以下匿名默认功能:

ɩ ntegers 1通过参数

 乘以ɩ ntegers 1通过参数(即平方)

⊢()  应用以下匿名默认函数,并将参数作为左参数:

  + 是总和

   的成员

   它?

ɩ真理ndices


1

Perl 5,43个字节

$i=<>;{sqrt(++$_**2+$i**2)!~/\./&&say;redo}

如果你想脚本终止,我们可以检查其他的腿,只有N²,如解释XNOR,所以我们有48个字节:

map{sqrt(++$_**2+$i**2)!~/\./&&say}1..($i=<>)**2

1

Japt,16字节

1oU² f@!(MhXU %1

在线尝试!

怎么运行的

        // Implicit: U = input integer
1oU²    // Generate a range of integers from 1 to U squared.
f@!(    // Keep only items X that return falsily to:
MhXU %1 //  Math.hypot(X,U) % 1.
        // This keeps only the items where sqrt(X*X+U*U) = 0.
        // Implicit: output last expression


1

05AB1E,10 个字节

nDLn+Ųƶ0K

在线尝试验证所有测试用例

nDLʒnIn+Ų

在线尝试验证所有测试用例

说明:

n           # Take the square of the (implicit) input-integer
 D          # Duplicate it
  L         # Create a list in the range [1, input^2]
   n        # Square each value in this list
    +       # Add the input^2 we duplicated to each
     Ų     # Check for each of these if it's a square (1 if truthy; 0 if falsey)
       ƶ    # Multiply each value by its 1-based index
        0K  # Remove all 0s from the list
            # (after which the result is output implicitly)

nDL         # Same as above
   ʒ        # Filter this list by:
    n       #  Get the square of the current number
     In+    #  Add the squared input to it
        Ų  #  And check if it's a square
            # (after the filter, implicitly output the result)

1

MathGolf,9个字节

²╒gƲk²+°

在线尝试!

找不到删除任何²s(占用3/9字节)的好方法。否则很简单

说明

²           square input
 ╒          range(1,n+1)
  gÆ        filter list using next 5 operators
    ²       square list element
     k²     push input squared
       +    pop a, b : push(a+b)
        °   is perfect square

1

Java 8,72字节

n->{for(int i=0;++i<n*n;)if(Math.hypot(i,n)%1==0)System.out.println(i);}

在线尝试。

说明:

n->{                           // Method with integer as parameter and no return-type
  for(int i=0;++i<n*n;)        //  Loop `i` in the range (0, n²)):
    if(Math.hypot(i,n)         //   If sqrt(i² + n²)
       %1==0)                  //   has no decimal digits after the comma (so is an integer)
      System.out.println(i);}  //    Output `i` with trailing newline
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.