Pirate Treasure
November 11, 2025Problem Statement
There are n pirates dividing a treasure.
- Pirate 1 takes
of the total treasure. - Pirate 2 takes
of what remains. - Pirate 3 takes
of the remaining amount. - …
- Pirate i takes
of the remaining pile at his turn.
We must determine the index of the pirate who gets the largest share
(older pirates have smaller indices, so answer is between 1 and n).
Example
If :
| Pirate | Share Taken |
|---|---|
| 1 | |
| 2 | |
| 3 | |
The largest share is , taken by Pirate 2.
Formal Definition
Let be the share of Pirate .
The product term is the fraction of treasure remaining after pirates 1 to have taken their shares.
Our goal:
Comparing Consecutive Shares
Instead of computing every share, compare two neighbors:
- If > 1, shares are increasing at
- If < 1, shares are decreasing after
- So the maximum occurs where increasing stops and decreasing starts
This happens when:
which simplifies to:
This is a quadratic equation in .
Solving the Quadratic
For:
Using the quadratic formula:
We only take the positive root since pirate indices are positive.
Let:
Now check:
- If
, then pirates p and p+1 tie → choose the earlier pirate → return p - Otherwise, the maximum occurs at p + 1
Python Implementation (Big-Integer Safe)
Using Integer Square Root
from math import isqrt def pirate_with_largest_share(n): # Compute p = floor((sqrt(1 + 4n) - 1) / 2) using integer sqrt to avoid precision issues p = (isqrt(1 + 4*n) - 1) // 2 # Check for tie case return p if p * (p + 1) == n else p + 1
Using brute force
def pirate_largest_bruteforce_float(n: int) -> int: remaining = 1.0 best_i, best_share = 1, 0.0 for i in range(1, n + 1): share = (i / n) * remaining if share > best_share: best_share, best_i = share, i remaining -= share # same as: remaining *= (1 - i/n) return best_i