Algorithmintro
Hamilton's method
Also called largest remainder method, Hare-Niemeyer method.
Gives each party the integer part of its quota and the remaining seats to the parties with the largest fractional remainders.
- Time
- O(n log n) worst
- Space
- O(n) extra
- Where
- h = the number of seats; n = the number of states or parties
- Model
- Word RAM
- Properties
- comparison baseddeterministiciterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Alexander Hamilton, 1792
Hamilton's method apportions seats among states or parties in proportion to their populations or votes. Each one first gets the whole part of its exact share, its quota, and the few seats that are left go to those with the largest fractional parts. The result always stays within quota, and it is the closest integer vector to the quotas. But it can take a seat away from a state when the house grows (the Alabama paradox) or when the state grows faster than another.
In plain words
Work out the fair share of each state, which is usually not a whole number, for example 3.48 seats. Give each state the whole part of its share, here 3 seats. Some seats are still free, fewer than the number of states. Give them, one each, to the states whose shares were cut the most, that is, to the largest fractional parts. No state ends up more than one seat away from its fair share. The method is easy to explain and to check by hand.
The problem
The input is the populations , integers with sum , and the house size . The quota of state is , its exact share, and . The output is integers with . See the apportionment problem for the general setting and the other methods.
The method writes with , gives to every state and one more seat to each of the states with the largest . Ties go to the smallest index.
The algorithm
All arithmetic is on integers: is integer division, and the remainder is , so it orders the states in the same way as .
HAMILTON(p, h) ▷ p = (p_1, ..., p_n), h seats- 1
P ← p_1 + ... + p_n - 2
for i ← 1 to n do a_i ← ⌊h p_i / P⌋; r_i ← h p_i mod P - 3
s ← h - (a_1 + ... + a_n) ▷ 0 ≤ s < n - 4
order the states by decreasing r_i, the smallest index among ties - 5
for each of the first s states i in this order do - 6
a_i ← a_i + 1 - 7
return a
Line L2 gives the lower quotas and the remainders, line L3 counts the free seats, and lines L4 to L6 give them to the largest remainders.
Example
Four parties have 100000, 80000, 30000 and 20000 votes, so , and seats are to be filled.
| party | votes | quota | remainder | seats | |
|---|---|---|---|---|---|
| 1 | 100000 | 3.478 | 3 | 110000 | 3 |
| 2 | 80000 | 2.783 | 2 | 180000 | 3 |
| 3 | 30000 | 1.043 | 1 | 10000 | 1 |
| 4 | 20000 | 0.696 | 0 | 160000 | 1 |
The lower quotas sum to 6, so seats are free. The two largest remainders are those of parties 2 and 4, and the result is . The Sainte-Lague method and the Huntington-Hill method give the same result here, and the D'Hondt method gives , which takes a seat from the smallest party.
Correctness
The proof shows that , so , and that a state with never gets an extra seat. So the seats sum to and each state gets or : the method satisfies quota. It also shows that the output minimizes over all integer vectors with sum . A transfer of one seat from to changes this sum by , where , so in an optimum all lie within 1 of each other, and the best choice of the extra seats is the one with the largest fractional parts.
Proof: Correctness and optimality of Hamilton's methodfull
Let be integers with , and let be an integer. The quota of state is , so . Write with .
Claim 1 (the integer form). Line L2 sets and .
Proof. Division with remainder gives with . Divide by .
So ordering the states by is the same as ordering them by , and the method needs no fractions.
Claim 2 (the number of extra seats). The number of line L3 is , and . Moreover is less than the number of states with , unless .
Proof. . Each is in , so the sum of the positive ones is less than their number.
Claim 3 (output). The method returns integers with and for every (the quota property).
Proof. Lines L5 and L6 add 1 to exactly of the , so the sum is . Each state gets or . By Claim 2 and the order of line L4, a state that gets the extra seat has , so is not an integer and .
Claim 4 (least squares). Among all integer vectors with , the output minimizes . If the remainders are distinct, it is the only minimizer.
Proof. Let be a minimizer and , so . Moving one seat from state to state changes by
Let have the largest and the smallest , and suppose . Then : if , then , so every with sum 0, every , and there is no gap. So the move is allowed, and it lowers , which is a contradiction. So a minimizer has . Since the sum to 0, , so every is in . If some , then , so every with sum 0, and every , a contradiction. In the same way no . So , that is, with , when , and .
For such a vector, . This is smallest when the states with are states with the largest , which is the choice of lines L4 to L6. If the are distinct, this choice is unique.
The same argument works for with any , since only the order of the matters.
Claim 5 (the Alabama paradox). The method is not house monotone: with , it gives for and for .
Proof. For the quotas are : the integer parts are , , and the remainders give the seat to state 3. For the quotas are : the integer parts are , , and the remainders give the two seats to states 1 and 2.
Complexity
Line L2 takes operations on numbers of the size of . The sort of line L4 takes comparisons, and the rest is . A sort is not needed: a selection algorithm finds the -th largest pair in worst-case time, by median of medians, and then one pass gives the extra seats. So the problem is solved in time, independent of . Extra space is .
Properties
- Quota. Each state gets its lower or its upper quota, by construction.
- Least squares. The output minimizes , and also for every , among all apportionments.
- Alabama paradox. With populations , gives and gives : the third state loses its seat when the house grows. See the Alabama paradox.
- Population paradox. With , populations give , and populations give . State 1 grew and lost a seat to state 2, which did not change.
- New states paradox. Populations with give . Add a state of population 3 and one seat for it: populations with give , so the old states change their seats.
- No divisor form. Hamilton's method is not a divisor method. By the Balinski-Young theorem, no method that satisfies quota is free of the population paradox.
Variants and improvements
- Other quotas. Largest remainder methods for party lists divide the votes by a quota and give the remaining seats to the largest remainders. The Hare quota gives Hamilton's method. The Droop quota and the Imperiali quota give fewer free seats, and with them the lower quotas can sum to more than .
- The quota method. Balinski and Young (1975) gave a method that gives seats one at a time, as divisor methods do, but only to states that stay within upper quota. It satisfies quota and is house monotone, but it has the population paradox.
- Divisor methods. The D'Hondt, Sainte-Lague and Huntington-Hill methods have no Alabama or population paradox, but they can violate quota.
- Lotteries. A randomized method can give each state or seats with expected value exactly . Gölz, Peters and Procaccia (2022) give one that is also house monotone.
When to use it
Use Hamilton's method when the rule must be easy to explain and each party must get its quota rounded up or down. It suits a fixed house and a single election. It is a poor choice when the house size or the populations change over time and the changes must look fair, as in a census apportionment, because of the Alabama and population paradoxes.
Pitfalls
- Floating point. Computing in floating point can misorder two close remainders or even get the floor wrong. Use integer division and the remainder .
- Ties. Two states with equal remainders can compete for the last seat. A law must say how to break the tie; this page uses the smallest index.
- Zero populations. A state with has remainder 0 and never gets a seat. Do not let it win a tie at remainder 0; with the order of line L4 it cannot, because is less than the number of positive remainders.
- The quota is not the result. The method stays within quota, but it is not monotone in . Do not assume that a larger house keeps the old seats.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hamilton-method/. The authors are listed in prov/S01MQ4.jsonl.
from fractions import Fraction
from typing import Sequence
def quotas(populations: Sequence[int], h: int) -> list[Fraction]:
"""Return the exact quotas h p_i / P."""
total = sum(populations)
return [Fraction(h * p, total) for p in populations]
def hamilton_method(populations: Sequence[int], h: int) -> list[int]:
"""Apportion h seats by Hamilton's method (largest remainders).
populations are nonnegative integers with a positive sum. Each state gets the integer
part of its quota h p_i / P, and the s seats that are left go to the s states with the
largest remainders, the smallest index among ties. All arithmetic is on integers: the
remainder of state i is h p_i mod P, which is P times the fractional part of its quota.
"""
if h < 0 or any(p < 0 for p in populations) or sum(populations) <= 0:
raise ValueError("need h >= 0, populations >= 0 and a positive total")
n = len(populations)
total = sum(populations) # @line L1
a = [h * p // total for p in populations] # @line L2
r = [h * p % total for p in populations]
s = h - sum(a) # @line L3
order = sorted(range(n), key=lambda i: (-r[i], i)) # @line L4
for i in order[:s]: # @line L5
a[i] += 1 # @line L6
return a # @line L7
test_hamilton_method.py 101 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/hamilton-method/. The authors are listed in prov/S01MQ4.jsonl.
import random
import unittest
from fractions import Fraction
from hamilton_method import hamilton_method, quotas
def compositions(h, n):
"""Yield every list of n nonnegative integers with sum h."""
if n == 1:
yield [h]
return
for x in range(h + 1):
for rest in compositions(h - x, n - 1):
yield [x] + rest
def cost(a, q):
return sum((x - y) ** 2 for x, y in zip(a, q))
def by_fractions(populations, h):
"""A second implementation with Fraction quotas and their fractional parts."""
q = quotas(populations, h)
a = [x.numerator // x.denominator for x in q]
frac = [x - y for x, y in zip(q, a)]
for i in sorted(range(len(q)), key=lambda i: (-frac[i], i))[: h - sum(a)]:
a[i] += 1
return a
class TestCheckValues(unittest.TestCase):
def test_four_parties(self):
self.assertEqual(hamilton_method([100000, 80000, 30000, 20000], 8), [3, 3, 1, 1])
def test_alabama_paradox(self):
self.assertEqual(hamilton_method([3, 3, 1], 3), [1, 1, 1])
self.assertEqual(hamilton_method([3, 3, 1], 4), [2, 2, 0])
def test_population_paradox(self):
self.assertEqual(hamilton_method([5, 1, 3], 5), [3, 0, 2])
self.assertEqual(hamilton_method([6, 1, 6], 5), [2, 1, 2])
def test_new_states_paradox(self):
self.assertEqual(hamilton_method([8, 1], 4), [4, 0])
self.assertEqual(hamilton_method([8, 1, 3], 5), [3, 1, 1])
def test_errors(self):
for p, h in (([0, 0], 3), ([1, -1, 2], 2), ([1, 2], -1)):
with self.assertRaises(ValueError):
hamilton_method(p, h)
class TestProperties(unittest.TestCase):
def test_sum_and_quota(self):
rng = random.Random(21)
for _ in range(3000):
n = rng.randint(1, 8)
p = [rng.randint(0, 1000) for _ in range(n)]
if sum(p) == 0:
continue
h = rng.randint(0, 40)
a = hamilton_method(p, h)
self.assertEqual(sum(a), h)
for x, q in zip(a, quotas(p, h)):
self.assertTrue(q.numerator // q.denominator <= x <= -(-q.numerator // q.denominator))
self.assertEqual(a, by_fractions(p, h))
def test_least_squares_by_brute_force(self):
rng = random.Random(22)
for _ in range(250):
n = rng.randint(1, 4)
p = [rng.randint(1, 60) for _ in range(n)]
h = rng.randint(0, 9)
q = quotas(p, h)
a = hamilton_method(p, h)
best = min(cost(b, q) for b in compositions(h, n))
self.assertEqual(cost(a, q), best)
r = [x - x.numerator // x.denominator for x in q]
if len(set(r)) == n:
self.assertEqual([b for b in compositions(h, n) if cost(b, q) == best], [a])
def test_alabama_paradox_is_common(self):
found = 0
for p in ([3, 3, 1], [5, 5, 2], [7, 6, 2]):
for h in range(1, 20):
a, b = hamilton_method(p, h), hamilton_method(p, h + 1)
if any(y < x for x, y in zip(a, b)):
found += 1
self.assertGreater(found, 0)
def test_quota_sums(self):
self.assertEqual(sum(quotas([100000, 80000, 30000, 20000], 8)), 8)
self.assertEqual(quotas([3, 3, 1], 4), [Fraction(12, 7), Fraction(12, 7), Fraction(4, 7)])
if __name__ == "__main__":
unittest.main()
History
Alexander Hamilton proposed the method for the first apportionment of the US House after the census of 1790. Congress passed it in 1792, and George Washington vetoed the bill, his first veto; Jefferson's method was used instead. The method came back in 1850, proposed by Samuel Vinton, and was used with changes until the census of 1900. In 1880 C. W. Seaton, the chief clerk of the Census Office, found that Alabama would get 8 seats in a house of 299 and only 7 in a house of 300, the paradox that now carries its name. The population paradox appeared around 1900, and the new states paradox when Oklahoma joined in 1907. In Europe the same rule is called the Hare-Niemeyer method, and Germany used it for the Bundestag from 1987 to 2005. Balinski and Young (1975, 1982) studied it in their theory of apportionment.
References
- Michel L. Balinski and H. Peyton Young, Fair Representation: Meeting the Ideal of One Man, One Vote, 2nd edition, Brookings Institution Press, 2001, Chapter 3 (The Methods of Jefferson and Hamilton) and Chapter 5 (Paradoxes)
- M. L. Balinski and H. P. Young, The quota method of apportionment, American Mathematical Monthly 82(7) (1975), 701-730, the quota property and house monotonicity, and why Hamilton's method has the first and not the second
- Paul Gölz, Dominik Peters and Ariel D. Procaccia, In this apportionment lottery, the House always wins, Operations Research 74(1) (2026), 390-407, Section 3.1, Theorem 1: no deterministic method satisfies quota and population monotonicity
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag S01MQ4 Edit this page History Report an error Markdown