Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

Complete solutions in C++, Python & Java — with step-by-step mathematical explanations

All Problems

Problem 525: Rolling Ellipse

View on Project Euler

Project Euler Problem 525 Solution

EulerSolve provides an optimized solution for Project Euler Problem 525, Rolling Ellipse, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary An ellipse with semiaxes \(a\) and \(b\) rolls on a straight line without slipping. Let \(C(a,b)\) denote the arc length of the trajectory traced by its center during one complete revolution. The problem asks for $C(1,4)+C(3,4).$ The implementations do not search for an elementary antiderivative. Instead, they derive an exact speed formula for the center, reduce the geometry to a smooth one-dimensional integral, and evaluate that integral numerically to high precision. Mathematical Approach Work in the ellipse's own coordinate system and parameterize the boundary point that is instantaneously in contact with the ground by $r(t)=(x(t),y(t))=(a\cos t,b\sin t),\qquad 0\le t\le 2\pi.$ As \(t\) runs once around the ellipse, the contact point visits the whole boundary once, so one full revolution of the rolling motion is captured by integrating over one full period of \(t\). Step 1: Parameterize the ellipse and its local geometry The first and second derivatives are $r'(t)=(-a\sin t,b\cos t),\qquad r''(t)=(-a\cos t,-b\sin t).$ The distance from the center of the ellipse to the contact point is $\lVert r(t)\rVert=\sqrt{a^2\cos^2 t+b^2\sin^2 t}.$ This distance varies with \(t\) unless \(a=b\), which is exactly why the center of a rolling ellipse does not move as simply as the center of a rolling circle....

Detailed mathematical approach

Problem Summary

An ellipse with semiaxes \(a\) and \(b\) rolls on a straight line without slipping. Let \(C(a,b)\) denote the arc length of the trajectory traced by its center during one complete revolution. The problem asks for

$C(1,4)+C(3,4).$

The implementations do not search for an elementary antiderivative. Instead, they derive an exact speed formula for the center, reduce the geometry to a smooth one-dimensional integral, and evaluate that integral numerically to high precision.

Mathematical Approach

Work in the ellipse's own coordinate system and parameterize the boundary point that is instantaneously in contact with the ground by

$r(t)=(x(t),y(t))=(a\cos t,b\sin t),\qquad 0\le t\le 2\pi.$

As \(t\) runs once around the ellipse, the contact point visits the whole boundary once, so one full revolution of the rolling motion is captured by integrating over one full period of \(t\).

Step 1: Parameterize the ellipse and its local geometry

The first and second derivatives are

$r'(t)=(-a\sin t,b\cos t),\qquad r''(t)=(-a\cos t,-b\sin t).$

The distance from the center of the ellipse to the contact point is

$\lVert r(t)\rVert=\sqrt{a^2\cos^2 t+b^2\sin^2 t}.$

This distance varies with \(t\) unless \(a=b\), which is exactly why the center of a rolling ellipse does not move as simply as the center of a rolling circle.

Step 2: Turn rolling without slipping into a speed law

For pure rolling, the contact point has zero velocity relative to the ground at the instant of contact. Therefore that point is the instantaneous center of rotation of the rigid body.

If \(\theta(t)\) is the current orientation angle of the ellipse, then the center moves around the contact point with speed

$v(t)=\left|\frac{d\theta}{dt}\right|\lVert r(t)\rVert.$

So the geometric problem is reduced to finding the angular speed \(\theta'(t)\) associated with the changing tangent direction of the ellipse.

Step 3: Differentiate the tangent angle

For a smooth planar parametrized curve, the derivative of the tangent angle is

$\frac{d\theta}{dt}=\frac{x'(t)y''(t)-y'(t)x''(t)}{x'(t)^2+y'(t)^2}.$

Substituting the ellipse derivatives gives

$x'(t)y''(t)-y'(t)x''(t)=ab,$

$x'(t)^2+y'(t)^2=a^2\sin^2 t+b^2\cos^2 t,$

hence

$\frac{d\theta}{dt}=\frac{ab}{a^2\sin^2 t+b^2\cos^2 t}.$

The denominator is always positive for \(a,b>0\), so the angular speed stays positive and no sign ambiguity remains in the arc-length integral.

Step 4: Obtain the closed integrand for the center speed

Combining the previous two steps yields

$v(t)=\frac{ab\sqrt{a^2\cos^2 t+b^2\sin^2 t}}{a^2\sin^2 t+b^2\cos^2 t}.$

Therefore the total center-path length over one revolution is

$C(a,b)=\int_0^{2\pi}\frac{ab\sqrt{a^2\cos^2 t+b^2\sin^2 t}}{a^2\sin^2 t+b^2\cos^2 t}\,dt.$

A useful sanity check is the circular case \(a=b=R\). Then the integrand becomes the constant \(R\), so

$C(R,R)=\int_0^{2\pi}R\,dt=2\pi R,$

which is exactly the expected path length for the center of a rolling circle.

Step 5: Use symmetry to integrate only one quarter-turn

The integrand depends only on \(\sin^2 t\) and \(\cos^2 t\). Consequently

$v(t+\pi)=v(t),\qquad v(\pi-t)=v(t).$

These symmetries imply

$C(a,b)=4\int_0^{\pi/2}\frac{ab\sqrt{a^2\cos^2 t+b^2\sin^2 t}}{a^2\sin^2 t+b^2\cos^2 t}\,dt.$

This is the exact form evaluated by the implementation, and it is numerically convenient because the interval is short and the integrand is smooth on \([0,\pi/2]\).

Step 6: Worked Example for \(C(2,4)\)

For \(a=2\) and \(b=4\), the quarter-turn integrand becomes

$v(t)=\frac{4\sqrt{\cos^2 t+4\sin^2 t}}{\sin^2 t+4\cos^2 t}.$

At the three standard Simpson nodes we get

$v(0)=1,\qquad v\left(\frac{\pi}{4}\right)=\frac{4\sqrt{10}}{5},\qquad v\left(\frac{\pi}{2}\right)=8.$

A single Simpson panel on \([0,\pi/2]\) gives the rough estimate

$S=\frac{\pi/2}{6}\left(1+4\cdot\frac{4\sqrt{10}}{5}+8\right)=\frac{\pi}{12}\left(9+\frac{16\sqrt{10}}{5}\right)\approx 5.0056.$

Multiplying by \(4\) would give about \(20.0224\), which is not yet accurate enough. After adaptive refinement the numerical integral converges to

$C(2,4)\approx 21.38816906,$

showing why an adaptive method is used instead of a single coarse panel.

How the Code Works

The C++, Python, and Java implementations all follow the same algorithm. First they evaluate the closed-form speed formula above. Next they apply Simpson's rule on the quarter interval \([0,\pi/2]\) using the endpoint values and the midpoint value.

They then recurse adaptively. For a current interval, the implementation evaluates the speed at the two quarter points, compares the sum of the left and right Simpson estimates with the Simpson estimate on the whole interval, and interprets the difference as an error signal.

If the estimated local error already satisfies

$|\Delta|\le 15\varepsilon,$

or if the recursion limit has been reached, the code accepts the interval and applies the standard Richardson correction

$S_{\text{refined}}=S_{\text{left}}+S_{\text{right}}+\frac{\Delta}{15}.$

Otherwise it splits the interval in half and repeats the same process on both subintervals. The requested integral is obtained by multiplying the converged quarter-interval value by \(4\). Finally the implementation evaluates \(C(1,4)\) and \(C(3,4)\), adds them, and prints the result to eight decimal places. The numerical parameters are \(\varepsilon=10^{-13}\) and a maximum recursion depth of \(30\).

Complexity Analysis

Adaptive Simpson integration has no fixed iteration count in advance, so it is best analyzed in terms of the number of accepted subintervals. If the recursion finishes with \(N\) accepted leaves, the total time is \(O(N)\) function evaluations up to constant factors, because each subdivision adds only a constant number of new samples. The memory usage is \(O(D)\), where \(D\) is the recursion depth; in the implementation \(D\le 30\).

For this problem the integrand is smooth on \([0,\pi/2]\), so \(N\) stays modest in practice. The adaptive strategy is efficient because it refines only where the shape of the integrand demands it, instead of spending the same resolution everywhere.

Footnotes and References

  1. Problem page: https://projecteuler.net/problem=525
  2. Ellipse: Wikipedia — Ellipse
  3. Arc length: Wikipedia — Arc length
  4. Adaptive Simpson's method: Wikipedia — Adaptive Simpson's method

Mathematical approach · C++ solution · Python solution · Java solution

Previous: Problem 524 · All Project Euler solutions · Next: Problem 526

Need help with a problem? Ask me! 💡
e
✦ Euler GLM 5.2
Hi! I'm Euler. Ask me anything about Project Euler problems, math concepts, or solution approaches! 🧮