All Euler problems
Project Euler

Circle and Tangent Line

Given a circle C centered at the origin with radius r, and an external point P = (a, 0) where a > r: 1. Find the two tangent lines from P to C. 2. Compute the tangent length (distance from P to eac...

Source sync Apr 19, 2026
Problem #0410
Level Level 28
Solved By 342
Languages C++, Python
Answer 799999783589946560
Length 362 words
geometrysequencelinear_algebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let \(C\) be the circle with radius \(r\), \(x^2 + y^2 = r^2\). We choose two points \(P(a, b)\) and \(Q(-a, c)\) so that the line passing through \(P\) and \(Q\) is tangent to \(C\).

For example, the quadruplet \((r, a, b, c) = (2, 6, 2, -7)\) satisfies this property.

Let \(F(R, X)\) be the number of the integer quadruplets \((r, a, b, c)\) with this property, and with \(0 < r \leq R\) and \(0 < a \leq X\).

We can verify that \(F(1, 5) = 10\), \(F(2, 10) = 52\) and \(F(10, 100) = 3384\).

Find \(F(10^8, 10^9) + F(10^9, 10^8)\).

Problem 410: Circle and Tangent Line

Mathematical Derivation

Tangent Length

By the Pythagorean theorem applied to the triangle formed by the center OO, the tangent point TT, and the external point PP:

OT2+PT2=OP2|OT|^2 + |PT|^2 = |OP|^2 r2+L2=a2r^2 + L^2 = a^2 L=a2r2L = \sqrt{a^2 - r^2}

Angle of Tangency

The half-angle α\alpha subtended at PP satisfies:

sin(α)=ra\sin(\alpha) = \frac{r}{a} α=arcsin ⁣(ra)\alpha = \arcsin\!\left(\frac{r}{a}\right)

Tangent Point Coordinates

The tangent points are:

T1=(r2a,  ra2r2a)T_1 = \left(\frac{r^2}{a},\; \frac{r\sqrt{a^2 - r^2}}{a}\right) T2=(r2a,  ra2r2a)T_2 = \left(\frac{r^2}{a},\; -\frac{r\sqrt{a^2 - r^2}}{a}\right)

Proof: The tangent point lies on the circle (x2+y2=r2x^2 + y^2 = r^2) and OTPTOT \perp PT. The foot of the perpendicular from OO onto line OPOP at distance r2/ar^2/a from OO gives the xx-coordinate. The yy-coordinate follows from the circle equation.

Tangent Line Equations

The tangent line at point (x0,y0)(x_0, y_0) on the circle x2+y2=r2x^2 + y^2 = r^2 is:

x0x+y0y=r2x_0\, x + y_0\, y = r^2

Substituting the tangent points:

r2ax±ra2r2ay=r2\frac{r^2}{a}\, x \pm \frac{r\sqrt{a^2 - r^2}}{a}\, y = r^2

Simplifying:

rx±a2r2y=rar\, x \pm \sqrt{a^2 - r^2}\, y = r\, a

Area Between Tangent Lines and Minor Arc

The area consists of the triangle OT1T2O T_1 T_2 minus the circular sector, plus the triangle PT1T2P T_1 T_2 minus the same sector. More directly:

A=Lrr2αA = L \cdot r - r^2 \cdot \alpha

where L=a2r2L = \sqrt{a^2 - r^2} and α=arcsin(r/a)\alpha = \arcsin(r/a).

This is the area of the kite OT1PT2O T_1 P T_2 (which equals LrL \cdot r) minus the circular sector of angle 2α2\alpha (which has area r2αr^2 \alpha).

Numerical Example

For r=1r = 1, P=(3,0)P = (3, 0):

QuantityValue
Tangent length LL82.8284\sqrt{8} \approx 2.8284
Half-angle α\alphaarcsin(1/3)19.47°\arcsin(1/3) \approx 19.47°
Tangent point T1T_1(1/3,  22/3)(0.333,0.943)(1/3,\; 2\sqrt{2}/3) \approx (0.333, 0.943)
Tangent point T2T_2(1/3,  22/3)(0.333,0.943)(1/3,\; -2\sqrt{2}/3) \approx (0.333, -0.943)
Enclosed area8arcsin(1/3)2.488\sqrt{8} - \arcsin(1/3) \approx 2.488

Generalizations

  • Multiple circles: Given nn circles, the number of common external tangent lines and their intersections form rich combinatorial structures.
  • Lattice tangent lines: Counting tangent lines from lattice points to the unit circle connects to number theory (sums of two squares).
  • Power of a point: The quantity a2r2a^2 - r^2 is the power of point PP with respect to circle CC. It equals the product of signed distances along any line through PP intersecting CC.

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Answer

799999783589946560\boxed{799999783589946560}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_410/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/**
 * Problem 410: Circle and Tangent Line
 *
 * Given a circle centered at origin with radius r and external point P=(a,0),
 * compute tangent length, tangent points, tangent line equations, and enclosed area.
 */

struct Point {
    double x, y;
    Point(double x = 0, double y = 0) : x(x), y(y) {}
    double norm() const { return sqrt(x * x + y * y); }
};

struct TangentResult {
    double tangent_length;
    double half_angle_rad;
    double half_angle_deg;
    Point t1, t2;
    double enclosed_area;
    double power_of_point;
};

TangentResult solve(double a, double r) {
    assert(a > r && r > 0);
    TangentResult res;

    // Tangent length: sqrt(a^2 - r^2)
    res.tangent_length = sqrt(a * a - r * r);

    // Half-angle at P
    res.half_angle_rad = asin(r / a);
    res.half_angle_deg = res.half_angle_rad * 180.0 / M_PI;

    // Tangent points
    double tx = r * r / a;
    double ty = r * sqrt(a * a - r * r) / a;
    res.t1 = Point(tx, ty);
    res.t2 = Point(tx, -ty);

    // Enclosed area = L*r - r^2 * alpha
    res.enclosed_area = res.tangent_length * r - r * r * res.half_angle_rad;

    // Power of point
    res.power_of_point = a * a - r * r;

    return res;
}

void print_result(double a, double r) {
    TangentResult res = solve(a, r);

    cout << fixed << setprecision(6);
    cout << "Circle: center (0,0), radius r = " << r << endl;
    cout << "External point: P = (" << a << ", 0)" << endl;
    cout << string(50, '=') << endl;

    cout << "Tangent length:        L = " << res.tangent_length << endl;
    cout << "Half-angle:            alpha = "
         << setprecision(4) << res.half_angle_deg << " deg  ("
         << setprecision(6) << res.half_angle_rad << " rad)" << endl;

    cout << "Tangent point T1:      (" << res.t1.x << ", " << res.t1.y << ")" << endl;
    cout << "Tangent point T2:      (" << res.t2.x << ", " << res.t2.y << ")" << endl;

    // Tangent line equations: r*x +/- sqrt(a^2-r^2)*y = r*a
    double L = res.tangent_length;
    cout << setprecision(4);
    cout << "Tangent line 1:        " << r << "*x + " << L << "*y = " << r * a << endl;
    cout << "Tangent line 2:        " << r << "*x + " << -L << "*y = " << r * a << endl;

    cout << setprecision(6);
    cout << "Enclosed area:         A = " << res.enclosed_area << endl;
    cout << "Power of point:        " << res.power_of_point
         << "  (= L^2 = " << res.tangent_length * res.tangent_length << ")" << endl;

    // Verification: tangent points lie on circle
    cout << "Verification |OT1|:    " << res.t1.norm() << "  (should be " << r << ")" << endl;
    cout << "Verification |OT2|:    " << res.t2.norm() << "  (should be " << r << ")" << endl;

    // Verification: OT perpendicular to PT
    double dot1 = res.t1.x * (res.t1.x - a) + res.t1.y * res.t1.y;
    double dot2 = res.t2.x * (res.t2.x - a) + res.t2.y * res.t2.y;
    cout << "Verification OT.PT (T1): " << scientific << dot1 << "  (should be 0)" << endl;
    cout << "Verification OT.PT (T2): " << dot2 << "  (should be 0)" << endl;
    cout << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cout << string(60, '=') << endl;
    cout << "  PROBLEM 410: CIRCLE AND TANGENT LINE" << endl;
    cout << string(60, '=') << endl << endl;

    // Primary example
    print_result(3.0, 1.0);

    cout << string(60, '=') << endl;
    cout << "Additional examples:" << endl;
    cout << string(60, '=') << endl << endl;

    // Additional examples
    vector<pair<double, double>> cases = {{5, 2}, {10, 1}, {2, 1}};
    for (auto& [a, r] : cases) {
        TangentResult res = solve(a, r);
        cout << fixed << setprecision(4);
        cout << "--- r=" << r << ", P=(" << a << ",0) ---" << endl;
        cout << "  L=" << res.tangent_length
             << ", alpha=" << res.half_angle_deg << " deg"
             << ", area=" << res.enclosed_area << endl;
        cout << "  T1=(" << res.t1.x << ", " << res.t1.y
             << "), T2=(" << res.t2.x << ", " << res.t2.y << ")" << endl;
        cout << endl;
    }

    return 0;
}