9
\$\begingroup\$

A math teacher wants to give his students an interesting geometry problem.

He has the following idea (Fig. 1):

Let \$AB\$ and \$CD\$ be two chords of a circle with center \$M\$, intersecting orthogonally at point \$S\$. Find the lengths of \$CS\$ and \$DS\$ if \$AS\$ is \$n\$ cm, \$BS\$ is \$m\$ cm, and the circle radius is \$r\$ cm.

Figure 1.

Now he needs to choose the appropriate values ​​for \$n,\:m\$ and \$r\$ for the problem. According to established convention, where possible, both the results and the given values ​​are integers. This would allow the students to intuitively understand whether their solution is correct. Unfortunately, the math teacher cannot find a combination of three integer input values ​​that would result in two integers.

The Challenge

Write a program or a function that will find all possible combinations of the three segments [m, n, r] that have integer lengths and integer solutions.

Input and output

A program or a function that inputs a limiting number \$X\$ and outputs all combinations of \$m, n, r ≤ X\$.

The output is arranged such that \$m\$ and \$n\$ precede \$r\$.

Rules

In this challenge, a chord cannot be equal to the diameter: \$AB \ne 2 \cdot r\$ and \$CD \ne 2 \cdot r\$

The lengths of the chords are different \$AB \ne CD\$

This is code-golf. Standard i/o. Standard loopholes.

Test case:

X = 100:

         m   n    r
 [1,]    4   76   85
 [2,]    9   41   65
 [3,]    4   44   25
 [4,]    8   22   25
 [5,]    6   72   65
 [6,]    8   58   65
 [7,]   17   49   65
 [8,]   11   91   85
 [9,]   23   49   85
[10,]    8   88   50
[11,]   16   44   50
[12,]   14   64   65
[13,]    9   39   25
[14,]   13   27   25
[15,]   15   87   85
[16,]   27   53   85
[17,]   23   55   65
[18,]   24   66   75
[19,]   38   64   85
[20,]   32   88  100
[21,]   17   95   65
[22,]   19   85   65
[23,]   18   78   50
[24,]   26   54   50
[25,]   21   99   65
[26,]   27   77   65
[27,]   36   68   65
[28,]   27   93   65
[29,]   31   81   65
[30,]   39   81   75
[31,]   30   96   65
[32,]   40   72   65
[33,]   55   81   85
[34,]   38   88   65
[35,]   44   76   65
[36,]   62   88   85
[37,]   64   90   85
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Can you clarify how 2, 14, 10 is not a solution? \$\endgroup\$ Commented yesterday
  • 1
    \$\begingroup\$ @Ajax1234 All the additional solutions you find: 2, 14, 10 , 1,7,5 etc. have one thing in common: the x and y coordinates of the intersection are equal, and both chords are split equally. These solutions are in some sense trivial. I understood "a diagonal cannot be a solution m+n<2r" to mean "a diameter cannot be a solution" but it seems the intent may indeed have been to exclude solutions where the intersection falls on a diagonal as well as ones where a chord is a diameter. I will wait for OP response but that's the only way I can explain the test cases. \$\endgroup\$ Commented yesterday
  • \$\begingroup\$ @LevelRiverSt Thank you very much, that makes sense \$\endgroup\$ Commented yesterday
  • \$\begingroup\$ According to your last edit, we should only exclude chords that are diameters. But something like (m,n,r)=(1,7,5) (with CS=BS) now looks valid. For X=100, I believe this would lead to 74 solutions instead of 37. \$\endgroup\$ Commented 20 hours ago
  • 1
    \$\begingroup\$ Yes, I think so! \$\endgroup\$ Commented 20 hours ago

4 Answers 4

4
\$\begingroup\$

Python3, 256 bytes

lambda X:[(m,n,r)for m in range(1,X+1)for n in range(m+1,X+1)for r in range(1,X+1)if F(n,m,r)and m+n<2*r]
def F(n,m,r):
 if(T:=r**2-((n+m)/2)**2)<0:return 0
 L=2*T**.5
 return int(U:=abs((-L+(L**2+4*m*n)**.5)/2))==U and U and sorted([U,L+U])!=sorted([n,m])

Try it online!

\$\endgroup\$
7
  • 1
    \$\begingroup\$ I'm not sure why this has been downvoted, but the TIO link produces no output. I added a print(k) after the first line of the footer and it gave several tuples which are not in the test case and with r not the largest value, in addition to some correct ones. I would encourage people to point out issues rather than downvoting without explanation. It may be a mistake in uploading. \$\endgroup\$ Commented yesterday
  • \$\begingroup\$ Further note: all the additional tuples produced by this code look valid to me so I don't know why they are excluded from the test case in the question. \$\endgroup\$ Commented yesterday
  • \$\begingroup\$ @LevelRiverSt Thank you for your comments. I believe my solution, as it stands, covers all correct cases, including some that are not included in the OP's output. I asked them for clarification, and have updated my post and TIO link to reflect this. There may be an aspect to the rules that has eluded me, so I will delete this if the OP can clarify. \$\endgroup\$ Commented yesterday
  • 1
    \$\begingroup\$ I believe the intent is for X to be a parameter to your lambda. \$\endgroup\$ Commented 23 hours ago
  • 1
    \$\begingroup\$ @Neil Thank you, updated \$\endgroup\$ Commented 13 hours ago
4
\$\begingroup\$

JavaScript (V8), 121 bytes

A naive solution that prints the triplets m n r.

X=>{for(m=++X;m--;)for(n=X;--n>m;)for(x=X;x--;d=(m*m+n*n+x*x+y*y)**.5,d%2||y<x|d>X*2|m+n>=d|m==x||print(m,n,d/2))y=m*n/x}

Try it online!

A note about Math.hypot()

The diameter \$d\$ really should be computed with d=Math.hypot(m,n,x,y) (saving 2 bytes). Unfortunately, this leads to rounding errors and invalidates some valid solutions.

\$\endgroup\$
3
\$\begingroup\$

Ruby, 126 bytes

->s{(t=*1..s).product(t,t).map{|i|r,x,y=i
x==y||!(x*x+y*y<q=r*r)||(q-x*x)**0.5%1+(n=x+j=(q-y*y)**0.5)%1>0||n>s||p([j-x,n,r])}}

Try it online!

Scans all potential integer intersection points x,y inside circles of diameters r and checks if the half-lengths of the chords are integers. Didn't work out as well as I had hoped as I had to add an extra condition to eliminate cases where n is greater than the input value.

Proof that for valid solutions the intersection coordinates of x,y must fall on an integer grid

n and m are integers so n+m is an integer. x = (n+m)/2-m must be an integer if n+m is even, or integer + 0.5 if n+m is odd. A similar argument follows for y.

If x is an integer + 0.5, then x*x will be an integer + 0.25. Similarly for y, and for the endpoints of the chords. By pythagoras, r*r = (x+m)*(x+m) + y*y is then not an integer, so r is not an integer. But the question requires it to be an integer. To avoid this contradiction x and y must both be integers.

Going back to the first point, this means the total length of chords (n+m) is always even.

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 75 bytes

≔⊕NθFθFιF⌕AX…¹θ²ΣX⊕⟦κι⟧²⊞υ⊕⟦κιλ⟧FυFυ¿∧⁼⌈ι⌈κ‹ικF…ι²Eօ겋⁺λμθ⭆¹⟦↔⁻λμ⁺λμ⌈ι

Try it online! Link is to verbose version of code. Explanation: Possibly not the shortest approach, but it only uses integer arithmetic. Points B and C on the circle must have integer coordinates, along with both distances MB and MC being the integer r, therefore taking two different Pythagorean triples with the same hypotenuse will generate four solutions (although some of them may result in m > X which must then be excluded).

≔⊕Nθ

Increment X as this saves bytes on the ranges and conditions.

FθFιF⌕AX…¹θ²ΣX⊕⟦κι⟧²⊞υ⊕⟦κιλ⟧

Loop over all p and q values to find Pythagorean triples p, q, r with p < q < r <= X.

FυFυ¿∧⁼⌈ι⌈κ‹ικ

Loop over (distinct) pairs of Pythagorean triples p, q, r and p', q', r where p < p' < q' < q < r.

F…ι²Eօ겋⁺λμθ⭆¹⟦↔⁻λμ⁺λμ⌈ι

Loop s over p and q and s' over p' and q' where s + s' <= X and output the triple m = |s - s|', n = s + s', r.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.