Project Euler Problem 222 Solution

Project Euler Problem 222 Solution

Sphere Packing

by {BetaProjects} | Project Euler & HackerRank
Difficulty: Easy

Project Euler Problem 222 Statement

What is the length of the shortest pipe, of internal radius ${50 mm}$, that can fully contain $21$ balls of radii ${30 mm}, {31 mm}, \dots, {50 mm}$?

Give your answer in micrometres (${10^{-6} m}$) rounded to the nearest integer.

Solution

Python Source Code

import math

r = [x * 1000 for x in [50, 48, 46, 44, 42, 40, 38, 36, 34, 32, 30, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]]
D = 100 * 1000
total_h = r[0] + sum(math.sqrt(2*D*(a + b) - D**2) for a, b in zip(r, r[1:])) + r[-1]
print(round(total_h))