Tripel Trithagoras

16

Sebuah Pythagoras Tiga adalah solusi bilangan bulat positif persamaan:

Triple Pythagoras

Triple Trithagoras adalah solusi bilangan bulat positif untuk persamaan:

Persamaan trithagoras

Di mana Δn menemukan nomor segitiga ke-n . Semua tripel Trithagoras juga solusi untuk persamaan:

masukkan deskripsi gambar di sini

Tugas

Diberikan bilangan bulat positif c, output semua pasangan bilangan bulat positif a,bsedemikian rupa sehingga jumlah bilangan segitiga th adan bth adalahc segitiga th. Anda dapat menampilkan pasangan dengan cara apa pun yang paling nyaman. Anda hanya harus mengeluarkan setiap pasangan satu kali.

Ini adalah

Uji Kasus

2: []
3: [(2, 2)]
21: [(17, 12), (20, 6)]
23: [(18, 14), (20, 11), (21, 9)]
78: [(56, 54), (62, 47), (69, 36), (75, 21), (77, 12)]
153: [(111, 105), (122, 92), (132, 77), (141, 59), (143, 54), (147, 42), (152, 17)]
496: [(377, 322), (397, 297), (405, 286), (427, 252), (458, 190), (469, 161), (472, 152), (476, 139), (484, 108), (493, 54), (495, 31)]
1081: [(783, 745), (814, 711), (836, 685), (865, 648), (931, 549), (954, 508), (979, 458), (989, 436), (998, 415), (1025, 343), (1026, 340), (1053, 244), (1066, 179), (1078, 80), (1080, 46)]
1978: [(1404, 1393), (1462, 1332), (1540, 1241), (1582, 1187), (1651, 1089), (1738, 944), (1745, 931), (1792, 837), (1826, 760), (1862, 667), (1890, 583), (1899, 553), (1917, 487), (1936, 405), (1943, 370), (1957, 287), (1969, 188)]
2628: [(1880, 1836), (1991, 1715), (2033, 1665), (2046, 1649), (2058, 1634), (2102, 1577), (2145, 1518), (2204, 1431), (2300, 1271), (2319, 1236), (2349, 1178), (2352, 1172), (2397, 1077), (2418, 1029), (2426, 1010), (2523, 735), (2547, 647), (2552, 627), (2564, 576), (2585, 473), (2597, 402), (2622, 177), (2627, 72)]
9271: [(6631, 6479), (6713, 6394), (6939, 6148), (7003, 6075), (7137, 5917), (7380, 5611), (7417, 5562), (7612, 5292), (7667, 5212), (7912, 4832), (7987, 4707), (8018, 4654), (8180, 4363), (8207, 4312), (8374, 3978), (8383, 3959), (8424, 3871), (8558, 3565), (8613, 3430), (8656, 3320), (8770, 3006), (8801, 2914), (8900, 2596), (8917, 2537), (9016, 2159), (9062, 1957), (9082, 1862), (9153, 1474), (9162, 1417), (9207, 1087), (9214, 1026), (9229, 881), (9260, 451), (9261, 430), (9265, 333)]
Posting Rock Garf Hunter
sumber
Bisakah kita menampilkan pasangan berulang? Contoh, untuk 21keluaran[(17, 12), (20, 6), (12, 17), (6, 20)]
Luis Mendo
7
Saya pikir Anda meminta kami untuk menemukan a^3+ b^3 = c^3. : D
Beta Decay
@LuisMendo No. Saya akan memasukkan ini dalam pertanyaan.
Posting Rock Garf Hunter
3
@BetaDecay MATL, 0 byte
Luis Mendo
3
@EriktheOutgolfer a^3+ b^3 = c^3diketahui tidak memiliki solusi integer; lihat teorema terakhir Fermat
Luis Mendo

Jawaban:

7

Mathematica, 53 49 48 byte

Solve[{x.(x+1)==#^2+#,a>=b>0},x={a,b},Integers]&

Contoh:

In[1]:= Solve[{x.(x+1)==#^2+#,a>=b>0},x={a,b},Integers]&[21]

Out[1]= {{a -> 17, b -> 12}, {a -> 20, b -> 6}}
alephalpha
sumber
ooohh, vektorisasi yang bagus, jauh lebih baik daripada yang akan saya lakukan
Greg Martin
6

MATL , 17 13 byte

:Ys&+G:s=R&fh

Setiap pasangan adalah output dengan angka yang lebih kecil terlebih dahulu.

Cobalah online!

Penjelasan

Pertimbangkan input 3.

:      % Implicitly input n. Push [1 2 ... n]
       % STACK: [1 2 3]
Ys     % Comulative sum
       % STACK: [1 3 6]
&+     % All pairwise sums
       % STACK: [2 4 7; 4 6 9; 7 9 12]
G:s    % Push 1+2+...+n
       % STACK: [2 4 7; 4 6 9; 7 9 12], 6
=      % Is equal?
       % STACK: [0 0 0; 0 1 0; 0 0 0]
R      % Upper triangular part of matrix. This removes duplicate pairs
       % STACK: [0 0 0; 0 1 0; 0 0 0]
&f     % Push row and column indices (1-based) of non-zero entries
       % STACK: 2, 2
h      % Concatenate horizontally. Implicitly display
       % STACK: [2, 2]
Luis Mendo
sumber
Penjelasannya tolong?
Erik the Outgolfer
@EriktheOutgolfer Tentu, saya akan menambahkannya nanti
Luis Mendo
Pastikan Anda menampilkan pasangan unik, itu sebabnya saya bertanya.
Erik the Outgolfer
@EriktheOutgolfer Ya, mereka unik (urus Ritu)
Luis Mendo
1
@EriktheOutgolfer Penjelasan ditambahkan
Luis Mendo
6

Jelly , 12 byte

j‘c2ḅ-
ŒċçÐḟ

Cobalah online!

Bagaimana itu bekerja

ŒċçÐḟ   Main link. Argument: c

Œċ      Yield all 2-combinations w/repetition of elements of [1, ..., c].
  çÐḟ   Filterfalse; keep only 2-combinations for which the helper link returns 0.


j‘c2ḅ-  Helper link. Left argument: [a, b]. Right argument: c

j       Join [a, b] with separator c, yielding [a, c, b].
 ‘      Increment; yield [a+1, c+1, b+1].
  c2    Combination count; compute [C(a+1,c), C(c+1,c), C(b+1,c)], yielding
        [½a(a+1), ½c(c+1), ½b(b+1)].
    ḅ-  Convert from base -1 to integer, yielding
        ½(-1)²a(a+1) + ½(-1)¹c(c+1) + ½(-1)⁰b(b+1) = ½(a(a+1) - c(c+1) + b(b+1)),
        which is 0 if and only if a(a+1) + b(b+1) = c(c+1).
Dennis
sumber
4

Python 2, 69 byte

Cobalah online

lambda c:[(a,b)for a in range(c)for b in range(a+1)if~a*a==c*~c-~b*b]

-9 byte, terima kasih kepada @WheatWizard

Possum Mati
sumber
Dan ~a*a==c*~c-~b*bbyte lebih pendek dari itu. Cobalah online!
Post Rock Garf Hunter
@WheatWizard Barang bagus: D
Dead Possum
4

Jelly , 16 14 byte

RS
ŒċÇ€S$⁼¥ÐfÇ

Cobalah online!

Ini terlalu lama pasti ...

Penjelasan:

ŒċÇ€S$⁼¥ÐfÇ (main) Arguments: z
Œċ             Return [[1, 1], [1, 2], ..., [1, z], [2, 2], ..., [z, z]]
          Ç    Return T(z)
  Ç€S$⁼¥Ðf     Only keep the pairs such as ΣT(a, b)=T(z)

RS (helper 1) Arguments: z
R  [1, 2, ..., z]
 S Take the sum
Erik the Outgolfer
sumber
4

AWK , 72 byte

{for(n=$1;++i<=n;)for(j=i;j<=n;++j)if(i^2+j^2+i+j==n^2+n)$0=$0" "i":"j}1

Cobalah online!

Output adalah c a1:b1 a2:b2 .... TIO Link memiliki 4 byte tambahani=0; untuk memungkinkan input multiline.

Ini sama sekali tidak efisien, tetapi berhasil. :)

Robert Benson
sumber
3

PHP, 94 Bytes

for($a=$c=$argn;$a--;)for($b=$a;$b;$b--)$a**2+$a+$b**2+$b!=$c**2+$c?:$e[]=[$a,$b];print_r($e);

Cobalah online!

Jörg Hülsermann
sumber
3

Haskell, 50 byte

f c=[(a,b)|a<-[1..c],b<-[1..a],a^2+a+b^2+b==c^2+c]

Contoh penggunaan: f 21-> [(17,12),(20,6)]. Cobalah online!

Menggunakan persamaan ke-2.

nimi
sumber
2

Axiom, 281 204 196 191 byte

q(b,m)==(r:=1+4*m;v:=4.*b*(b+1);r<v=>0;(sqrt(r-v)-1)/2);g(c:NNI):Any==(r:List List INT:=[];i:=0;repeat(i:=i+1;i>=c=>break;w:=q(i,c^2+c);w>=i and fractionPart(w)=0=>(r:=cons([w::INT,i],r)));r)

tes dan ungolf

-- if m=c^2+c than a^2+a+b^2+b-m=0 has the solutions [a,b] with a>0,b>0
-- if it is used a=(-1+sqrt(1+4*m-4*(b)*(b-1)))/2 because the other return a<0
-- o(b,m) return that solution if 1+4*m-4*(b)*(b-1)>0 [so exist in R sqrt] else return 0
o(b,m)==(r:=1+4*m;v:=4.*b*(b+1);r<v=>0;(sqrt(r-v)-1)/2)

--it Gets one not negative integer c; return one list of list(ordered) of 2 integers
--[a,b] with  a^2+a+b^2+b=c^2+c
gg(c:NNI):List List INT==
   r:List List INT:=[]  -- initialize the type make program more fast at last it seems 10x
   i:=0
   repeat
      i:=i+1
      i>=c=>break
      w:=o(i,c^2+c)
      w>=i and fractionPart(w)=0=>(r:=cons([w::INT,i],r))
   r

(6) -> [[i,g(i)]  for i in [2,3,21,23,78,153,496,1081,1978,2628,9271]]
   (6)
   [[2,[]], [3,[[2,2]]], [21,[[17,12],[20,6]]], [23,[[18,14],[20,11],[21,9]]],
    [78,[[56,54],[62,47],[69,36],[75,21],[77,12]]],
    [153,[[111,105],[122,92],[132,77],[141,59],[143,54],[147,42],[152,17]]],

     [496,
       [[377,322], [397,297], [405,286], [427,252], [458,190], [469,161],
        [472,152], [476,139], [484,108], [493,54], [495,31]]
       ]
     ,

     [1081,
       [[783,745], [814,711], [836,685], [865,648], [931,549], [954,508],
        [979,458], [989,436], [998,415], [1025,343], [1026,340], [1053,244],
        [1066,179], [1078,80], [1080,46]]
       ]
     ,

     [1978,
       [[1404,1393], [1462,1332], [1540,1241], [1582,1187], [1651,1089],
        [1738,944], [1745,931], [1792,837], [1826,760], [1862,667], [1890,583],
        [1899,553], [1917,487], [1936,405], [1943,370], [1957,287], [1969,188]]
       ]
     ,

     [2628,
       [[1880,1836], [1991,1715], [2033,1665], [2046,1649], [2058,1634],
        [2102,1577], [2145,1518], [2204,1431], [2300,1271], [2319,1236],
        [2349,1178], [2352,1172], [2397,1077], [2418,1029], [2426,1010],
        [2523,735], [2547,647], [2552,627], [2564,576], [2585,473], [2597,402],
        [2622,177], [2627,72]]
       ]
     ,

     [9271,
       [[6631,6479], [6713,6394], [6939,6148], [7003,6075], [7137,5917],
        [7380,5611], [7417,5562], [7612,5292], [7667,5212], [7912,4832],
        [7987,4707], [8018,4654], [8180,4363], [8207,4312], [8374,3978],
        [8383,3959], [8424,3871], [8558,3565], [8613,3430], [8656,3320],
        [8770,3006], [8801,2914], [8900,2596], [8917,2537], [9016,2159],
        [9062,1957], [9082,1862], [9153,1474], [9162,1417], [9207,1087],
        [9214,1026], [9229,881], [9260,451], [9261,430], [9265,333]]
       ]
     ]
                                                      Type: List List Any
RosLuP
sumber
1

CJam , 30 28 byte

{_,2m*:$_&f+{{_)*}%~+=},1f>}

Blok anonim mengharapkan argumennya di stack dan membiarkan hasilnya di stack.

Cobalah online!

Penjelasan

Saya akan merujuk input sebagai n

_,     e# Copy n, and get the range from 0 to n-1.
2m*    e# Get the 2nd Cartesian power of this range.
:$_&   e# Sort the pairs and deduplicate, to get all unique pairs.
f+     e# Prepend n to each pair.
{      e# Filter these triplets; keep only those that give a truthy result:
 {     e#  Map this block over the triplet:
  _)*  e#   Multiply x by x+1. (i.e. x^2 + x)
 }%    e#  (end map)
 ~+=   e#  Check if the sum of the second and third is equal to the first.
},     e# (end filter)
1f>    e# Remove the first element from all remaining triplets.
Kucing Bisnis
sumber
1

Pyth - 23 21 byte

L*bhbfqyQ+yhTyeT.CUQ2

Cobalah

L*bhbfqyQ+yhTyeT.CUQ2
L*bhb                     Define y(b)=b*(b+1)
                .CUQ2     All pairs of numbers less than the input
     fqyQ+yhTyeT          Filter based on whether y(input) == y(1st elem. of pair) + y(2nd elem. of pair)
Maria
sumber
1

JavaScript (ES6), 83 byte

c=>[...Array(c*c)].map((_,x)=>[x%c,x/c|0]).filter(([a,b])=>a>=b&a++*a+b++*b==c*c+c)

Uji kasus

Mengabaikan di sini input terbesar yang membutuhkan terlalu banyak waktu untuk cuplikan.

Arnauld
sumber