Project Euler Problem 334
In Plato's heaven, there exist an infinite number of bowls in a straight line.
Solution
Answer: 150320021261690835
Let the bowls be indexed by integers, and let $a_i$ denote the number of beans in bowl $i$.
We are allowed the move
$$(\ldots,a_{i-1},a_i,a_{i+1},\ldots) \to (\ldots,a_{i-1}+1,a_i-2,a_{i+1}+1,\ldots)$$
whenever $a_i\ge 2$.
The key is to avoid simulating billions of moves.
1. Mathematical analysis
Step 1: Identify invariants
Each move preserves:
Total number of beans
$$N=\sum_i a_i$$
because we remove 2 beans and add 2 beans.
First moment (center of mass)
$$M=\sum_i i,a_i$$
since
$$(i-1)+(i+1)-2i=0.$$
Thus both $N$ and $M$ are invariant.
Step 2: Find a quantity that changes predictably
Consider the quadratic moment
$$Q=\sum_i i^2 a_i.$$
One move at position $i$ changes $Q$ by
$$(i-1)^2+(i+1)^2-2i^2.$$
Expanding:
$$(i^2-2i+1)+(i^2+2i+1)-2i^2=2.$$
So every move increases $Q$ by exactly 2.
Hence:
$$\boxed{ \text{moves} = \frac{Q_{\text{final}}-Q_{\text{initial}}}{2} }$$
So we only need the final stable configuration.
Step 3: Characterize the final stable state
A stable state has only 0 or 1 bean per bowl.
Among all stable states with fixed $N$ and $M$, the reachable one is the unique arrangement minimizing
$$\sum x_i^2$$
subject to:
- $N$ occupied positions,
- distinct integers,
- total sum $M$.
Because the square function is convex, the occupied bowls must be as consecutive as possible.
Thus the final positions are:
$$a,\ a+1,\ \ldots,\ a+N-1$$
except that the largest $r$ positions are shifted by $+1$.
Let
$$T=\frac{N(N-1)}{2}.$$
Then write
$$M-T = aN + r, \qquad 0\le r < N.$$
The final occupied positions are:
$$a,\ a+1,\ldots,a+N-r-1,$$
and
$$a+N-r+1,\ldots,a+N.$$
This lets us compute $Q_{\text{final}}$ directly.
Step 4: Generate the sequence
We are given:
$$t_0=123456$$
and
$$t_i= \begin{cases} t_{i-1}/2 & \text{if even}\ \lfloor t_{i-1}/2\rfloor \oplus 926252 & \text{if odd} \end{cases}$$
with
$$b_i=(t_i\bmod 2^{11})+1.$$
The first terms are:
$$b_1=289,\qquad b_2=145$$
matching the statement.
For the example of only two bowls, the formula gives exactly
$$3419100$$
moves, verifying correctness.
2. Python implementation
def generate_b(n=1500):
t = 123456
b = []
for _ in range(n):
if t % 2 == 0:
t //= 2
else:
t = (t // 2) ^ 926252
b.append((t % (2 ** 11)) + 1)
return b
def sum_squares_arithmetic(start, length):
"""
Sum of:
start^2 + (start+1)^2 + ... + (start+length-1)^2
"""
if length <= 0:
return 0
n = length
return (
n * start * start
+ 2 * start * (n * (n - 1) // 2)
+ (n - 1) * n * (2 * n - 1) // 6
)
# Generate the 1500 bowls
b = generate_b(1500)
# Invariants
N = sum(b)
M = sum(i * x for i, x in enumerate(b))
# Initial quadratic moment
Q_initial = sum(i * i * x for i, x in enumerate(b))
# Determine final stable configuration
T = N * (N - 1) // 2
a = (M - T) // N
r = (M - T) % N
# Final occupied bowls:
# a, a+1, ..., a+N-r-1
# a+N-r+1, ..., a+N
left_count = N - r
Q_final = (
sum_squares_arithmetic(a, left_count)
+ sum_squares_arithmetic(a + left_count + 1, r)
)
# Each move increases Q by exactly 2
moves = (Q_final - Q_initial) // 2
print(moves)
3. Code walkthrough
Generate the sequence
We compute $t_i$ recursively and convert to
$$b_i=(t_i \bmod 2048)+1.$$
This produces:
$$b_1=289,\quad b_2=145,$$
matching the problem statement.
Compute invariants
We calculate:
- total beans:
$$N=\sum b_i$$
- first moment:
$$M=\sum i,b_i$$
- initial quadratic moment:
$$Q_{\text{initial}}$$
Build the final configuration
We solve:
$$M-T=aN+r,$$
where
$$T=\frac{N(N-1)}{2}.$$
This determines the unique stable arrangement.
Compute move count
Since every move increases $Q$ by exactly 2:
$$\text{moves} = \frac{Q_{\text{final}}-Q_{\text{initial}}}{2}.$$
For the sample with $289,145$, this reproduces:
$$3419100.$$
4. Final verification
The result is:
- positive (expected),
- extremely large (reasonable for $1500$ bowls with many beans),
- exactly reproduces the provided 2-bowl example.
Answer: 150320021261690835