Problem 317: Firecracker
View on Project EulerProject Euler Problem 317 Solution
EulerSolve provides an optimized solution for Project Euler Problem 317, Firecracker, with C++, Python, Java, and a step-by-step mathematical explanation.
Problem Summary A firecracker explodes at height $h=100\ \text{m}$ above the ground. Every tiny fragment leaves the explosion point with the same speed $v=20\ \text{m/s}$ but in an arbitrary direction. Gravity is constant: $g=9.81\ \text{m/s}^2.$ Ignoring air resistance, we must find the volume of the whole region visited by the fragments before they hit the ground. Mathematical Approach 1) Rotational symmetry reduces the problem to a 2D cross-section. Because the launch speed is the same in every direction, the reachable region is symmetric around the vertical axis through the explosion point. So it is enough to work in cylindrical coordinates: $r=\sqrt{x^2+z^2},\qquad y=\text{height above ground}.$ Once we know the boundary curve \(y(r)\) in the \((r,y)\)-plane, the full 3D region is obtained by revolving that curve around the \(y\)-axis. 2) One projectile trajectory. Let \(\theta\) be the launch angle above the horizontal. Then the fragment satisfies $r(t)=v\cos\theta\; t,$ $y(t)=h+v\sin\theta\; t-\frac12gt^2.$ Eliminating \(t\) using \(t=r/(v\cos\theta)\) gives the familiar projectile parabola $y(r,\theta)=h+r\tan\theta-\frac{gr^2}{2v^2\cos^2\theta}.$ 3) Rewrite with \(u=\tan\theta\). Since $\frac1{\cos^2\theta}=1+\tan^2\theta=1+u^2,$ we can write $y(r,u)=h+ru-\frac{gr^2}{2v^2}(1+u^2).$ For fixed \(r\), this is a concave quadratic polynomial in \(u\)....
Detailed mathematical approach
Problem Summary
A firecracker explodes at height
$h=100\ \text{m}$
above the ground. Every tiny fragment leaves the explosion point with the same speed
$v=20\ \text{m/s}$
but in an arbitrary direction. Gravity is constant:
$g=9.81\ \text{m/s}^2.$
Ignoring air resistance, we must find the volume of the whole region visited by the fragments before they hit the ground.
Mathematical Approach
1) Rotational symmetry reduces the problem to a 2D cross-section.
Because the launch speed is the same in every direction, the reachable region is symmetric around the vertical axis through the explosion point. So it is enough to work in cylindrical coordinates:
$r=\sqrt{x^2+z^2},\qquad y=\text{height above ground}.$
Once we know the boundary curve \(y(r)\) in the \((r,y)\)-plane, the full 3D region is obtained by revolving that curve around the \(y\)-axis.
2) One projectile trajectory.
Let \(\theta\) be the launch angle above the horizontal. Then the fragment satisfies
$r(t)=v\cos\theta\; t,$
$y(t)=h+v\sin\theta\; t-\frac12gt^2.$
Eliminating \(t\) using \(t=r/(v\cos\theta)\) gives the familiar projectile parabola
$y(r,\theta)=h+r\tan\theta-\frac{gr^2}{2v^2\cos^2\theta}.$
3) Rewrite with \(u=\tan\theta\).
Since
$\frac1{\cos^2\theta}=1+\tan^2\theta=1+u^2,$
we can write
$y(r,u)=h+ru-\frac{gr^2}{2v^2}(1+u^2).$
For fixed \(r\), this is a concave quadratic polynomial in \(u\). Therefore its maximum over all launch angles is found by differentiating with respect to \(u\).
4) The envelope of all trajectories.
Differentiate:
$\frac{\partial y}{\partial u}=r-\frac{gr^2}{v^2}u.$
Setting this to zero yields the maximizing slope
$u=\tan\theta=\frac{v^2}{gr}.$
Substituting back gives the upper envelope
$y_{\max}(r)=h+\frac{v^2}{2g}-\frac{gr^2}{2v^2}.$
It is convenient to abbreviate
$a=h+\frac{v^2}{2g},\qquad b=\frac{g}{2v^2},$
so that
$y_{\max}(r)=a-br^2.$
This is the paraboloid boundary used by the code.
5) Why the whole region below the envelope is actually filled.
For fixed \(r\), the value \(y(r,u)\) depends continuously on \(u\). One choice of \(u\) makes the fragment hit the ground exactly at that radius, so \(y=0\) is achievable there. Another choice gives the maximal height \(y_{\max}(r)\). By continuity, every intermediate height between \(0\) and \(y_{\max}(r)\) is also reached by some launch angle.
So the cross-section at radius \(r\) is not just a boundary point: it is the full vertical interval
$0\le y\le y_{\max}(r).$
6) Where does the region stop?
The outer radius is where the envelope reaches ground level:
$a-br_{\max}^2=0,$
hence
$r_{\max}^2=\frac{a}{b}.$
With the numerical constants,
$a\approx120.3873598369,\qquad b=0.0122625,\qquad r_{\max}\approx99.0834077898.$
7) Volume by washers.
At height \(y\), the cross-section is a disk of radius \(r(y)\), where
$r(y)^2=\frac{a-y}{b}\qquad (0\le y\le a).$
Therefore
$V=\pi\int_0^a r(y)^2\,dy=\pi\int_0^a \frac{a-y}{b}\,dy.$
Evaluating the integral gives
$V=\frac{\pi a^2}{2b}.$
8) Equivalent shell integral.
The same answer appears if we integrate cylindrical shells:
$V=2\pi\int_0^{r_{\max}} r\,y_{\max}(r)\,dr$
$=2\pi\int_0^{r_{\max}} r(a-br^2)\,dr=\frac{\pi a^2}{2b}.$
This is exactly the comment embedded in the C++ solution.
9) Final closed form.
Substituting the definitions of \(a\) and \(b\), we obtain
$V=\pi\frac{v^2}{g}\left(h+\frac{v^2}{2g}\right)^2.$
This is why the program is only a direct evaluation of a closed formula.
10) Useful checkpoint: \(h=0\).
If the explosion happens on the ground, then
$a=\frac{v^2}{2g},\qquad b=\frac{g}{2v^2},$
so
$V=\frac{\pi a^2}{2b}=\frac{\pi v^6}{4g^3}.$
This is the exact checkpoint used in the source code.
Algorithm
1) Read \(h\), \(v\), and \(g\).
2) Compute
$a=h+\frac{v^2}{2g},\qquad b=\frac{g}{2v^2}.$
3) Return
$V=\frac{\pi a^2}{2b}.$
4) Print the result rounded to four decimal places.
Complexity Analysis
Everything is closed form, so the computation costs
$O(1)$
time and
$O(1)$
memory.
Checks And Final Result
The code checks the special case
$h=0\implies V=\frac{\pi v^6}{4g^3}.$
For the actual problem data \((h,v,g)=(100,20,9.81)\), the computed volume is
$1856532.8455.$
Further Reading
- Problem page: https://projecteuler.net/problem=317
- Projectile motion: https://en.wikipedia.org/wiki/Projectile_motion
- Solid of revolution: https://en.wikipedia.org/wiki/Solid_of_revolution