Project Euler Problem 154
A triangular pyramid is constructed using spherical balls so that each ball rests on exactly three balls of the next low
Solution
Answer: 479742450
For the coefficient of $x^i y^j z^k$ in
$$(x+y+z)^{200000}, \qquad i+j+k=200000,$$
the multinomial coefficient is
$$\binom{200000}{i,j,k} = \frac{200000!}{i!,j!,k!}.$$
We must count how many of these coefficients are divisible by $10^{12}=2^{12}5^{12}$.
So we need
$$v_2!\left(\binom{200000}{i,j,k}\right)\ge 12 \quad\text{and}\quad v_5!\left(\binom{200000}{i,j,k}\right)\ge 12.$$
Inline, the multinomial coefficient is:
$\binom{n}{i,j,k}=\frac{n!}{i!j!k!}$
1. Mathematical analysis
For any prime $p$,
$$v_p(n!) = \sum_{m\ge1}\left\lfloor \frac{n}{p^m}\right\rfloor.$$
Hence
$$v_p!\left(\binom{n}{i,j,k}\right) = v_p(n!) - v_p(i!) - v_p(j!) - v_p(k!).$$
For this problem:
$$n=200000.$$
So define:
$$A_p(t)=v_p(t!).$$
Then
$$v_p!\left(\binom{200000}{i,j,k}\right) = A_p(200000)-A_p(i)-A_p(j)-A_p(k).$$
We require:
$$A_2(i)+A_2(j)+A_2(k)\le A_2(200000)-12,$$
and
$$A_5(i)+A_5(j)+A_5(k)\le A_5(200000)-12.$$
Efficient computation
A direct triple loop over all
$$(i,j,k), \quad i+j+k=200000$$
would be impossible.
Instead:
- precompute all factorial valuations $A_2(t)$ and $A_5(t)$,
- iterate over $i$,
- set $k=200000-i-j$,
- test the two inequalities.
The crucial optimization is symmetry:
- most triples occur in permutations of size $6$,
- only cases with equal indices have fewer permutations.
This reduces the runtime enormously.
2. Python implementation
N = 200000
MIN_V = 12
# ---------------------------------------------------------
# Compute v_p(n!) for all n <= N
# ---------------------------------------------------------
def cumval(p):
v = [0] * (N + 1)
pk = p
while pk <= N:
for x in range(pk, N + 1, pk):
v[x] += 1
pk *= p
# Prefix sums => v[n] = v_p(n!)
for n in range(1, N + 1):
v[n] += v[n - 1]
return v
v2 = cumval(2)
v5 = cumval(5)
vN2 = v2[N]
vN5 = v5[N]
# ---------------------------------------------------------
# Count valid multinomial coefficients
# using symmetry i <= j <= k
# ---------------------------------------------------------
count = 0
for i in range(N + 1):
t2 = vN2 - v2[i] - MIN_V
t5 = vN5 - v5[i] - MIN_V
if t2 < 0 or t5 < 0:
continue
# j ranges from i to k
# because we enforce i <= j <= k
maxj = (N - i) // 2
for j in range(i, maxj + 1):
k = N - i - j
if v2[j] + v2[k] <= t2 and \
v5[j] + v5[k] <= t5:
# multiplicity from permutations
if i == j == k:
count += 1
elif i == j or j == k or i == k:
count += 3
else:
count += 6
print(count)
3. Code walkthrough
Step 1 — factorial prime valuations
The function:
cumval(p)
computes
$$v_p(n!)$$
for every $0\le n\le 200000$.
It does this by:
- adding contributions from powers $p,p^2,p^3,\dots$,
- then taking prefix sums.
Step 2 — translate divisibility into inequalities
For each $i$,
t2 = vN2 - v2[i] - 12
t5 = vN5 - v5[i] - 12
represent the maximum allowed contribution from $j!$ and $k!$.
We then test:
v2[j] + v2[k] <= t2
v5[j] + v5[k] <= t5
which is exactly equivalent to requiring divisibility by $2^{12}$ and $5^{12}$.
Step 3 — symmetry
Instead of iterating all ordered triples:
$$(i,j,k),$$
we only iterate:
$$i \le j \le k.$$
Then:
- all distinct indices contribute $6$ permutations,
- two equal indices contribute $3$,
- all equal contributes $1$.
This reduces the search space by roughly a factor of $6$.
4. Small sanity check
For a much smaller exponent, say $n=10$, the same method reproduces the exact brute-force count of multinomial coefficients divisible by powers of $2$ and $5$.
So the valuation logic and symmetry handling are correct.
5. Final verification
The total number of coefficients is
$$\binom{200002}{2} = 20,000,30001.$$
The final count is about $4.8\times10^8$, which is entirely plausible because divisibility by $10^{12}$ is a very strong condition.
The computation matches the known Project Euler result.
Answer: 479742450