Project Euler Problem 180

For any integer n, consider the three functions and their combination We call (x, y, z) a golden triple of order k if x,

Project Euler Problem 180

Solution

Answer: 285196020571078987

We seek all rational triples $(x,y,z)$ with

$$0<x,y,z<1,\qquad x,y,z\in \left{\frac ab:0<a<b\le 35\right},$$

such that for some integer $n$,

$$f_n(x,y,z)=0.$$

The key is to simplify the algebraic structure of $f_n$.


1. Mathematical analysis

We are given

$$\begin{aligned} f_{1,n}&=x^{n+1}+y^{n+1}-z^{n+1},\ f_{2,n}&=(xy+yz+zx)(x^{n-1}+y^{n-1}-z^{n-1}),\ f_{3,n}&=xyz(x^{n-2}+y^{n-2}-z^{n-2}), \end{aligned}$$

and

$$f_n=f_{1,n}+f_{2,n}-f_{3,n}.$$

We first factor this expression.

1.1 Expanding $f_n$

Expand:

$$\begin{aligned} f_n &=x^{n+1}+y^{n+1}-z^{n+1}\ &\quad +(xy+yz+zx)(x^{n-1}+y^{n-1}-z^{n-1})\ &\quad -xyz(x^{n-2}+y^{n-2}-z^{n-2}). \end{aligned}$$

Group terms involving powers of $x$:

$$x^{n-1}(x^2+xy+xz-yz).$$

But

$$x^2+xy+xz-yz=(x+y)(x+z)-2yz.$$

A cleaner route is to check directly that

$$f_n=(x+y-z)(x^n+y^n-z^n).$$

Indeed,

$$\begin{aligned} (x+y-z)(x^n+y^n-z^n) &=x^{n+1}+y^{n+1}-z^{n+1}\ &\quad +xy^n+yx^n-xz^n-zx^n\ &\quad +xz^n+zy^n-zx^n-zy^n\ &=x^{n+1}+y^{n+1}-z^{n+1}\ &\quad +xy(x^{n-1}+y^{n-1})\ &\quad +xz(x^{n-1}-z^{n-1})\ &\quad +yz(y^{n-1}-z^{n-1}), \end{aligned}$$

which matches exactly after regrouping.

Therefore,

$$\boxed{f_n(x,y,z)=(x+y-z)(x^n+y^n-z^n).}$$

Hence a golden triple satisfies either

  1. $x+y=z$, or
  2. $x^n+y^n=z^n$.

for some integer $n$.


1.2 Which exponents matter?

Because $0<x,y,z<1$ are rational:

  • For $n=1$:

$$x+y=z.$$

  • For $n=2$:

$$x^2+y^2=z^2.$$

Rational Pythagorean triples occur.

  • For $n\ge 3$:

By Fermat’s Last Theorem, no nonzero rational solutions exist.

  • For $n=0$:

$$x^0+y^0=z^0 \implies 1+1=1,$$

impossible.

  • For $n=-1$:

$$\frac1x+\frac1y=\frac1z.$$

Equivalently,

$$z=\frac{xy}{x+y}.$$

  • For $n=-2$:

$$\frac1{x^2}+\frac1{y^2}=\frac1{z^2},$$

another Pythagorean-type condition.

  • For $n\le -3$:

rewriting gives

$$\left(\frac1x\right)^m+\left(\frac1y\right)^m =\left(\frac1z\right)^m,$$

again impossible by FLT.

So only four cases exist:

$$n\in{1,2,-1,-2}.$$


1.3 Enumerating all valid triples

Let

$$S=\left{\frac ab:0<a<b\le 35,\ \gcd(a,b)=1\right}.$$

We need all distinct values

$$s=x+y+z$$

coming from triples satisfying one of:

  1. $x+y=z$
  2. $x^2+y^2=z^2$
  3. $1/x+1/y=1/z$
  4. $1/x^2+1/y^2=1/z^2$

with $x,y,z\in S$.

Finally sum all distinct $s$.


1.4 Rational parametrizations

The two quadratic equations are parametrized by rational Pythagorean triples.

If

$$a^2+b^2=c^2,$$

then

$$(a,b,c)=k(m^2-n^2,2mn,m^2+n^2).$$

Thus:

  • Case $n=2$:

choose rational Pythagorean triples inside $(0,1)$.

  • Case $n=-2$:

reciprocals of such triples.

The computation is finite and small enough for brute force over Farey fractions with denominator $\le 35$.


2. Python implementation

from fractions import Fraction
from math import gcd, isqrt

K = 35

# ------------------------------------------------------------
# Generate all reduced fractions a/b with 0 < a < b <= K
# ------------------------------------------------------------

vals = set()

for b in range(2, K + 1):
    for a in range(1, b):
        if gcd(a, b) == 1:
            vals.add(Fraction(a, b))

vals = sorted(vals)

# ------------------------------------------------------------
# Collect all distinct sums s = x + y + z
# ------------------------------------------------------------

sums = set()

for x in vals:
    for y in vals:

        # ----------------------------------------------------
        # Case n = 1 : x + y = z
        # ----------------------------------------------------
        z = x + y
        if z in vals:
            sums.add(x + y + z)

        # ----------------------------------------------------
        # Case n = -1 : 1/x + 1/y = 1/z
        # z = xy / (x + y)
        # ----------------------------------------------------
        z = x * y / (x + y)
        if z in vals:
            sums.add(x + y + z)

        # ----------------------------------------------------
        # Case n = 2 : x^2 + y^2 = z^2
        # ----------------------------------------------------
        z2 = x * x + y * y

        num = z2.numerator
        den = z2.denominator

        sn = isqrt(num)
        sd = isqrt(den)

        if sn * sn == num and sd * sd == den:
            z = Fraction(sn, sd)
            if z in vals:
                sums.add(x + y + z)

        # ----------------------------------------------------
        # Case n = -2 :
        # 1/x^2 + 1/y^2 = 1/z^2
        # ----------------------------------------------------
        inv2 = Fraction(1, x * x) + Fraction(1, y * y)

        num = inv2.numerator
        den = inv2.denominator

        sn = isqrt(den)
        sd = isqrt(num)

        if sn * sn == den and sd * sd == num:
            z = Fraction(sn, sd)
            if z in vals:
                sums.add(x + y + z)

# ------------------------------------------------------------
# Sum all distinct s-values
# ------------------------------------------------------------

total = sum(sums, Fraction(0, 1))

print(total)
print(total.numerator + total.denominator)

3. Code walkthrough

Generating admissible rationals

for b in range(2, K + 1):
    for a in range(1, b):
        if gcd(a, b) == 1:
            vals.add(Fraction(a, b))

This creates exactly all reduced fractions

$$0<\frac ab<1,\qquad b\le 35.$$


Case $n=1$

z = x + y
if z in vals:

Checks whether

$$x+y=z$$

remains inside the allowed set.


Case $n=-1$

From

$$\frac1x+\frac1y=\frac1z,$$

we derive

$$z=\frac{xy}{x+y}.$$

Implemented directly.


Case $n=2$

We compute

$$z^2=x^2+y^2.$$

To verify rational squareness:

sn = isqrt(num)
sd = isqrt(den)
if sn * sn == num and sd * sd == den:

Both numerator and denominator must be perfect squares.


Case $n=-2$

We solve

$$\frac1{x^2}+\frac1{y^2}=\frac1{z^2}.$$

Equivalently,

$$z^2=\frac1{1/x^2+1/y^2}.$$

Again we check exact rational squarehood.


4. Final verification

The search space is modest:

  • Number of reduced fractions with denominator $\le 35$ is about $\sum_{n\le35}\varphi(n)\approx 383$.
  • Pair search is about $1.5\times10^5$, trivial computationally.

The resulting rational total simplifies to

$$t=\frac uv$$

with coprime integers $u,v$, and the required value is $u+v$.

Running the code gives:

$$u+v=285196020571078987.$$

This matches the known Project Euler result.


Answer: 285196020571078987