Skip to content

hard problems

why it works

what it is

Some problems have no fast method that anyone knows. This page says what fast means, why a table can look fast and not be, and what P and NP mean.

Take some numbers and a target. Can some of the numbers add up to exactly the target? Say 2, 3, 4 and 6, and a target of 7. One method fills a table: a column for each number, a row for each total from 0 to the target. A cell says yes when its total can be reached with the numbers up to its column. That is 4 columns of 8 cells, 32 in all, one step each. 7 is reached: 3 + 4.

totalwith 2with 3with 4with 6
0yesyesyesyes
1
2yesyesyesyes
3yesyesyes
4yesyes
5yesyesyes
6yesyes
7yesyes
The table for 2, 3, 4 and 6, and a target of 7. Each column keeps the yeses of the one before, and adds its number to each of them. A blank is no.

A method is called fast when its steps grow no faster than a fixed power of the input's length: the length, its square, its cube. Books call that polynomial time. The length is the count of digits in the input. 2, 3, 4, 6 and 7 are 5 digits.

The table has a row for each total, so the target's value sets its cells, not how many digits the target has. With 4 numbers, a target of 100, 3 digits, takes 404 cells. One digit more, 1000, takes 4,004. The input grew by 1 digit, and the table about 10 times. Question 1 shows that this outgrows any fixed power of the target's digits. Books call that pseudo-polynomial time: fast while the numbers stay short.

Another method tries every set of the numbers, adding each one up. The size of the numbers does not matter now. But each extra number doubles the sets: 4 numbers make 16 sets, and 20 make 1,048,576. Now say someone hands you a set and says it adds to the target. To check, add it up: for 20 numbers, at most 19 additions. Nobody knows a fast way to find a set for every input. Checking one is fast.

numberssets to tryadditions to check one
5324
101,0249
201,048,57619
401,099,511,627,77639
Finding against checking. The sets double with each number, and the additions grow by 1.

Problems whose answers can be found fast are called P. Problems where a proposed yes can be checked fast are called NP, and adding up to a target is one. Every problem in P is in NP: if you can solve it fast, solving it is a fast check. Is every problem in NP also in P? Nobody knows. The Clay Mathematics Institute has offered a million dollars for the answer since 2000.

One problem can turn into another. The books' volume four, chapter 43 turns a roster into boxes. Ana, Bo and Cass: at least one works on Saturday, and not all three. Draw a box for each way to meet a condition, such as A1 for Ana works and A0 for Ana is off. A1, B1 and C1 meet at least one works; A0, B0 and C0 meet not all three. Pick one box from each condition. Lines join boxes you cannot pick together: two of the same condition, and a person working and off. Two boxes with no line give a roster. A1 and B0 say Ana works and Bo is off; Cass can be either. Turning one problem into another quickly, with the same answer, is called a reduction.

A1B1C1A0B0C0
The roster as boxes. The top 3 are the ways to meet at least one works, the bottom 3 not all three. Pick one from each. Lines join two of one condition, and a person working and off. A1 and B0 have no line: Ana works, Bo is off.

In 1971 Cook showed that every problem in NP reduces to one question. Can a list of yes or no conditions, each saying at least one of a few things holds, all be met? Levin, in the Soviet Union, found the same kind of result on his own, published in 1973. A problem in NP that every problem in NP reduces to is called NP-complete. Garey and Johnson's book of 1979 lists over 300 of them. Given a fast method for one, turn any problem into it quickly, then solve that fast. So a fast method for one would give a fast method for all. But NP-complete is a claim about the worst input, not yours. The table above took 32 cells. Knapsack's page counts such a table, and the travelling salesperson's tries every order of its towns.

SRTC
S is adding up to a target, R the roster and T asks whether the towns have a round trip of at most a given length. C is the list of conditions. Each arrow turns a problem into C quickly, so a fast method for C would solve all three fast.

where the formulas come from

Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about nn, or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.

question 1At least how many cells does the table fill for 4 numbers and a target of dd digits? Let CC be the cells for the smallest such target. A bigger target only adds cells. A floor is a number the cells never go below.
  1. the move: the smallest target of d digits

    the line:
    the smallest targetof d digits: 1 andd−1 zeros\begin{gathered} \text{the smallest target} \\ \text{of } d \text{ digits: 1 and} \\ d - 1 \text{ zeros} \end{gathered}

    why it is allowed: Its first digit is at least 1, and every other digit at least 0. So the smallest is 1 and then d−1d - 1 zeros. For 3 digits, 100.

  2. the move: as tens multiplied

    the line:
    that targetis 10d−1\begin{gathered} \text{that target} \\ \text{is } 10^{d - 1} \end{gathered}

    why it is allowed: 1 and then d−1d - 1 zeros is d−1d - 1 tens multiplied together. For 3 digits, 100.

  3. the move: count the cells

    the line:
    C=4(10d−1+1)C = 4(10^{d - 1} + 1)

    why it is allowed: For the smallest target, each column has a cell for each total from 0 to the target, so the target plus 1 cells. There are 4 columns. For 3 digits, 4 × 101 = 404.

  4. the move: leave out the 4 extra cells

    the line:
    C≥4×10d−1C \geq 4 \times 10^{d - 1}
    digitssmallest targetcellsfloor
    1184
    2104440
    3100404400
    410004,0044,000
    The cells by line 3 and the floor by line 4.

    why it is allowed: Line 3, less the 4 cells for the total of 0. For 3 digits, 404 is at least 400.

  5. the move: one digit more

    the line:
    one digit more,10 times the floor\begin{gathered} \text{one digit more,} \\ 10 \text{ times the floor} \end{gathered}
    digitssixth power of the digitsfloor
    26440
    3729400
    44,0964,000
    515,62540,000
    646,656400,000
    The sixth power starts above the floor. From 5 digits the floor is above, and each digit more widens its lead, as line 6 shows.

    why it is allowed: One digit more multiplies 10d−110^{d - 1} by one more 10. The input grows by 1 digit, and the floor by exactly 10 times. The cells come close, as 404 to 4,004 is about 9.9 times.

  6. the move: against any fixed power

    the line:
    one digit more turns dkinto (d+1)k, which is dktimes (1+1d)k,falling toward 1\begin{gathered} \text{one digit more turns } d^k \\ \text{into } (d + 1)^k \text{, which is } d^k \\ \text{times } (1 + \frac{1}{d})^k \text{,} \\ \text{falling toward 1} \end{gathered}

    why it is allowed: Here kk is the fixed power. One digit more turns dkd^k into (d+1)k(d + 1)^k, which is dkd^k times (1+1d)k(1 + \frac{1}{d})^k. For the sixth power, from 4 digits to 5, 4,096 times about 3.8 is 15,625. As dd grows, 1d\frac{1}{d} shrinks, so (1+1d)k(1 + \frac{1}{d})^k falls toward 1. Once it is below 5, each digit more multiplies the floor by 10 and the power by less than 5, so the floor divided by the power at least doubles. Doubling again and again passes any number, so the floor passes the power and stays above.

try it with a small dd: at d=2d = 2, line 3 gives C=44C = 44, which you can work out by hand

question 2How many sets, SS, can you make from nn numbers? Taking none counts too.
  1. the move: in or out

    the line:
    each number isin or out\begin{gathered} \text{each number is} \\ \text{in or out} \end{gathered}

    why it is allowed: A set is fixed once you say, for each number, whether it is in. 1 number gives 2 sets, with it and without it.

  2. the move: one more doubles

    the line:
    one number moredoubles the sets\begin{gathered} \text{one number more} \\ \text{doubles the sets} \end{gathered}
    setadds to
    none0
    22
    33
    2, 35
    44
    2, 46
    3, 47
    2, 3, 49
    The 8 sets of 2, 3 and 4. The first 4 are the sets of 2 and 3, and the last 4 are the same with 4 put in.

    why it is allowed: Every set so far comes back twice, once without the new number and once with it.

  3. the move: double n times

    the line:
    S=2nS = 2^n

    why it is allowed: Line 1 gives 2 sets for 1 number, and line 2 doubles them for each number after it. So nn numbers give nn 2s multiplied together. For 3 numbers, 8.

try it with a small nn: at n=3n = 3, line 3 gives S=8S = 8, which you can work out by hand

question 3How many sets, TT, do n+10n + 10 numbers make, against 2n2^n for nn?
  1. the move: the sets for n + 10

    the line:
    T=2n+10T = 2^{n + 10}

    why it is allowed: Question 2, for n+10n + 10 numbers.

  2. the move: split the 2s

    the line:
    T=2n×210T = 2^n \times 2^{10}

    why it is allowed: n+10n + 10 2s multiplied are nn 2s, times 10 more.

  3. the move: work out 10 2s

    the line:
    T=1024×2nT = 1024 \times 2^n

    why it is allowed: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1,024. For 3 numbers, 8 sets become 8,192.

  4. the move: what it says

    the line:
    10 more numbers,over 1,000 times the sets\begin{gathered} \text{10 more numbers,} \\ \text{over 1,000 times the sets} \end{gathered}

    why it is allowed: By line 3, 1,024 is more than 1,000. Count one step per set. A machine 1,000 times faster cannot try every set of 10 more numbers in the same time.

try it with a small nn: at n=3n = 3, line 1 gives T=8,192T = 8,192, which you can work out by hand

question 4How many additions, KK, does checking a set chosen from nn numbers take at most?
  1. the move: add along the set

    the line:
    a set of m numbers, m at least 1,takes m−1 additions\begin{gathered} \text{a set of } m \text{ numbers, } m \text{ at least 1,} \\ \text{takes } m - 1 \text{ additions} \end{gathered}

    why it is allowed: Here mm is how many numbers the set holds. Start from the first and add each other number once. Then compare the total with the target. For 3 and 4, 3 + 4 = 7, 1 addition.

  2. the move: the biggest set

    the line:
    K=n−1K = n - 1

    why it is allowed: The biggest set holds all nn numbers, so by line 1 it takes the most. For 20 numbers, 19, against 1,048,576 sets to try.

try it with a small nn: at n=3n = 3, line 2 gives K=2K = 2, which you can work out by hand

a worked example

Fill the table by hand for 3, 5 and 6, and a target of 11. Write each column as the totals it reaches.

  1. Before any number, only the total 0 is reached: take nothing.
  2. With 3: keep 0, and 0 + 3 = 3. Reached: 0 and 3.
  3. With 5: 0 + 5 = 5 and 3 + 5 = 8. Reached: 0, 3, 5 and 8.
  4. With 6: 0 + 6 = 6, 3 + 6 = 9, 5 + 6 = 11, and 8 + 6 = 14, past 11. 11 is reached: 5 + 6.
  5. The table has 3 columns of 12 cells, 36 in all. Checking the answer takes 1 addition, 5 + 6 = 11. Trying every set instead makes 8.
withtotals reachedhow many
30, 32
50, 3, 5, 84
60, 3, 5, 6, 8, 9, 117
The totals each column reaches. Each keeps the totals before it, and adds its number to each.

the trap

check yourself

  1. The table has a column for each of 5 numbers and a cell for each total from 0 to the target 9,999. How many cells? And with the target 99,999?

    the answer

    5 × 10,000 = 50,000 cells, then 5 × 100,000 = 500,000. One digit more, 10 times the cells.

  2. You have 30 numbers. How many sets does trying every set make? How many additions at most check one proposed set?

    the answer

    30 2s multiplied, 1,073,741,824 sets. Checking takes at most 29 additions, then one comparison.

  3. Adapted from the books' volume four, chapter 43. Trying every set of 40 numbers takes exactly a day, one step per set. A machine 1,000 times faster runs for a day. How many numbers can it handle?

    the answer

    49. By question 3, 10 more numbers need 1,024 times the sets, more than 1,000. 9 more need 512 times, which fits.