Logic and proofs — guided practice with the alarm’s rules, eight-row truth tables and an induction that falls like dominoes (and the table I handed in with a row missing)

In the first classroom practical of Discrete Mathematics I handed in a truth table with seven rows and did not notice until the lecturer put it on the projector: I had skipped the F F T row. I had been filling in the combinations “by eye”, in no particular order, and one got away. The last column had seven perfect letters and was still wrong, because in the problems exam what you choose is the whole last column, and mine was missing one. Since then I always do the same thing: I count 2n2^n before starting and write the variables in the fixed order from the course notes.

That is what this practice is: the classroom problems of topic 1, worked out step by step and explained the way I would have liked them explained to me, with the home alarm from the logic and proofs theory as the thread. There are seven problems: formalising sentences, an eight-row table, equivalences with tables and with laws, the consistency of some specifications, quantifiers, three proofs (contrapositive, induction and counterexample) and, at the end, a truth table in the format of the problems exam, which is what comes up most from this topic. Each one has its “Check it with Python” so you can see the working adds up.

Before you start

It helps to be clear about:

  • The tables of the six connectives, and above all that p→qp \to q is only F in the T → F row.
  • That “pp only if qq” is p→qp \to q, and that what is sufficient goes on the left of the arrow and what is necessary on the right.
  • De Morgan’s laws, the distributive law and p→q≡¬p∨qp \to q \equiv \neg p \lor q.
  • How to negate a quantifier: ¬∀x P(x)≡∃x ¬P(x)\neg \forall x\, P(x) \equiv \exists x\, \neg P(x).

If any of these sound shaky, review the theory before carrying on.

In every problem I use the same letters: pp, the door is open; qq, the sensor detects motion; rr, night mode is on; and ss, the alarm rings. The Python blocks have an “Open in the playground” button so you can change them and run them on the site, without installing anything.

How to solve a logic problem

Every problem in this practice follows the same four-step scheme:

  1. Data: which propositions there are and what is being asked (a formula, a table, a classification, a proof).
  2. Method: truth table (always works, but grows like 2n2^n), equivalence laws (shorter, but each law must be named) or a proof method.
  3. Solve showing each step: one column per subformula, one law per line.
  4. Check: with a particular row, with a case that makes sense in real life or with Python.

Problem 1: the alarm’s rules, as formulas

Statement. With pp, qq, rr and ss as above, write in formal language:

a) The alarm rings only if night mode is on. b) The door being open is sufficient for it to ring, provided night mode is on. c) The alarm does not ring unless the door is open or there is motion. d) The alarm rings if and only if night mode is on and the door is open or there is motion.

Then write the converse, the inverse and the contrapositive of “if there is motion, the hallway light comes on”, with ℓ\ell: the hallway light comes on.

The idea

Formalising means finding the connective behind each turn of phrase. The key words are “only if”, “sufficient”, “necessary”, “unless” and “if and only if”. Before writing anything, decide what is the hypothesis and what is the conclusion: the conclusion is what is guaranteed when the hypothesis holds.

Before formalising a sentence with “only if”, ask yourself: “if the left-hand side happens, what must have happened?”. What must have happened is the conclusion.

Step by step

a) “It rings only if night mode is on”: if it rings, night mode was on. The hypothesis is ss and the conclusion rr:

s→r s \to r

Notice that it is not r→sr \to s: night mode does not guarantee that it rings (in a quiet house it does not).

b) “Door open is sufficient for it to ring” is p→sp \to s, but only “provided night mode is on”, so that condition also goes into the hypothesis:

(p∧r)→s (p \land r) \to s

Another equally good form is r→(p→s)r \to (p \to s): they are equivalent, and we check it with Python.

c) “It does not ring unless XX” means “if XX does not happen, it does not ring”: ¬X→¬s\neg X \to \neg s. With X=p∨qX = p \lor q:

¬(p∨q)→¬s≡s→(p∨q) \neg(p \lor q) \to \neg s \quad \equiv \quad s \to (p \lor q)

The second form is the contrapositive of the first: “if it rings, the door was open or there was motion”. Both are fine.

d) The “if and only if” joins two whole sides; the right-hand side needs brackets so that the “and” applies to the disjunction:

s↔(r∧(p∨q)) s \leftrightarrow \big(r \land (p \lor q)\big)

Without the inner brackets, r∧p∨qr \land p \lor q would read as (r∧p)∨q(r \land p) \lor q by precedence, and the alarm would ring with motion even when night mode was off.

The three variants of q→ℓq \to \ell:

NameFormulaIn words
Implicationq→ℓq \to \ellIf there is motion, the light comes on
Converseℓ→q\ell \to qIf the light comes on, there is motion
Inverse¬q→¬ℓ\neg q \to \neg \ellIf there is no motion, the light does not come on
Contrapositive¬ℓ→¬q\neg \ell \to \neg qIf the light does not come on, there is no motion

Only the contrapositive says the same as the original. The converse fails in real life: the light can be switched on by hand without anybody in front of the sensor.

Check it with Python

We check that the two forms in part b) are equivalent and that the converse is not: go through the rows and compare.

from itertools import product

def implies(a, b):
    return (not a) or b

# b) (p ∧ r) → s  versus  r → (p → s): 3 variables, 8 rows
same = all(implies(p and r, s) == implies(r, implies(p, s))
           for p, r, s in product([True, False], repeat=3))
print("b) both forms are equivalent:", same)

# q → l versus its converse and its contrapositive (2 variables, 4 rows)
for name, f in [("converse", lambda q, l: implies(l, q)),
                ("contrapositive", lambda q, l: implies(not l, not q))]:
    eq = all(implies(q, l) == f(q, l) for q, l in product([True, False], repeat=2))
    print(name, "equivalent to the original:", eq)

Output:

b) both forms are equivalent: True
converse equivalent to the original: False
contrapositive equivalent to the original: True

The first line confirms that in b) it does not matter which one you write. The other two are the theory’s table of variants summed up in one word: the contrapositive matches in all four rows and the converse does not.

Questions you might ask

Why is r→sr \to s wrong in a)? Because “only if” puts the necessary condition on the right. r→sr \to s would say that in night mode it always rings, which is not the case.

What if I write c) as (p∨q)→s(p \lor q) \to s? That would be the converse of what the sentence says. “It does not ring unless there is motion” does not promise that it rings with motion (night mode is missing); it only says that without motion or door it does not ring.

Can I drop the brackets in d)? The outer ones, yes, because ↔\leftrightarrow has the lowest priority. The inner ones, no: r∧(p∨q)r \land (p \lor q) and r∧p∨qr \land p \lor q are different formulas.

Is there only one correct answer when formalising? No: any equivalent formula is fine (like the two in b) or the two in c)). That is why the exam gives you options and you have to recognise the one equivalent to yours.

Translating “only if” the same as “if”. “I go to class only if there is a test” is class→test\text{class} \to \text{test}; “I go to class if there is a test” is test→class\text{test} \to \text{class}. They swap sides.

Try it yourself

  • Formalise “for the alarm to ring it is necessary that there is motion or the door is open” and check that it is the same formula as part c).
  • Write the converse, the inverse and the contrapositive of “if the alarm rings, I wake up” (s→ws \to w) and say which one says the same as the original.

Problem 2: an eight-row table that is always T

Statement. Build the truth table of

[(p→q)∧(q→r)]→(p→r) \big[(p \to q) \land (q \to r)\big] \to (p \to r)

and say whether it is a tautology, a contradiction or a contingency.

The idea

It is the hypothetical syllogism from the theory: if qq follows from pp and rr follows from qq, then rr follows from pp. We expect a tautology, but we have to show it with the table. There are three variables, so 23=82^3 = 8 rows, and the formula’s tree says which columns we need: p→qp \to q, q→rq \to r, their conjunction, p→rp \to r and the final implication.

Before starting, write the column headings from left to right in the order they are computed. If a column depends on one you do not have yet, the order is wrong.

Step by step

First, the three variable columns in the fixed order: pp changes every 4 rows, qq every 2 and rr on every row. Then each column with its connective:

  • p→qp \to q: F only when pp is T and qq is F (rows 3 and 4).
  • q→rq \to r: F only when qq is T and rr is F (rows 2 and 6).
  • (p→q)∧(q→r)(p \to q) \land (q \to r): T only if both previous ones are T (rows 1, 5, 7 and 8).
  • p→rp \to r: F only when pp is T and rr is F (rows 2 and 4).
  • The last one: F only if the conjunction is T and p→rp \to r is F. The conjunction is T in rows 1, 5, 7 and 8, and in those four p→rp \to r is T. There is no T → F row, so the last column is T everywhere.
Truth table step by stepTruth table with one column per subformula and the last column highlighted.pqrp → qq → r(p → q) ∧ (q → r)p → r[…] → (p → r)TTTTTTTTTTFTFFFTTFTFTFTTTFFFTFFTFTTTTTTTFTFTFFTTFFTTTTTTFFFTTTTT
The last column is all T: the hypothetical syllogism is a tautology.
pqrp → qq → r(p → q) ∧ (q → r)p → rLast
TTTTTTTT
TTFTFFFT
TFTFTFTT
TFFFTFFT
FTTTTTTT
FTFTFFTT
FFTTTTTT
FFFTTTTT

The last column is all T: it is a tautology. That is why the hypothetical syllogism is a valid rule of inference.

Check

You only need to look at the one way the last column could be F: hypothesis T and conclusion F. For p→rp \to r to be F we need pp T and rr F (rows 2 and 4). In row 2, q→rq \to r is F; in row 4, p→qp \to q is F. In both, the hypothesis is F, and the last column survives. This reasoning, “I look for the row that would make it false and see that it does not exist”, is faster than the table and is a good way to check it.

Check it with Python

from itertools import product

def implies(a, b):
    return (not a) or b

def tf(x):
    return "T" if x else "F"

last = []
for p, q, r in product([True, False], repeat=3):
    hyp = implies(p, q) and implies(q, r)      # (p→q) ∧ (q→r)
    f = implies(hyp, implies(p, r))            # [...] → (p→r)
    last.append(tf(f))
print("last column:", " ".join(last))
print("tautology" if last.count("T") == 8 else "not a tautology")

Output:

last column: T T T T T T T T
tautology

The last column comes out with eight T, just like the table by hand.

Questions you might ask

Why T first? It is the order in the course notes and in the exam. If you start with F the table is just as correct, but the last column comes out upside down, and comparing it with the options can throw you.

Do I need the conjunction column? Yes. Without it you would have to do two operations in your head at once in the last column, which is exactly where people slip.

Why don’t I get T everywhere if I write (p→q)∧(q→r)→p→r(p \to q) \land (q \to r) \to p \to r without square brackets? Because →\to is not associative: a→(b→c)a \to (b \to c) and (a→b)→c(a \to b) \to c are different. Always keep the brackets the statement gives.

Is a tautology the same as “always happens” in real life? It is “always T because of its form”, whatever pp, qq and rr are. Swap the alarm for anything else and it is still T.

Filling in q→rq \to r while looking at columns pp and qq out of habit. Before each column, point with your finger at the two columns it depends on.

Try it yourself

  • Build the table of [(p→q)∧q]→p\big[(p \to q) \land q\big] \to p (affirming the consequent) and find the row that makes it false.
  • Build the table of [(p∨q)∧¬p]→q\big[(p \lor q) \land \neg p\big] \to q (disjunctive syllogism): tautology?

Problem 3: equivalences, with tables and with laws

Statement.

a) Check with a table that ¬(p↔q)≡p⊕q\neg(p \leftrightarrow q) \equiv p \oplus q. b) Prove with laws that (p→q)∧(p→r)≡p→(q∧r)(p \to q) \land (p \to r) \equiv p \to (q \land r). c) Simplify ¬(p∨(¬p∧q))\neg\big(p \lor (\neg p \land q)\big) as far as you can.

The idea

Two formulas are equivalent if their last columns match. With two variables the table is quick (4 rows). With laws, the route is almost always: remove arrows, push negations in with De Morgan and group with the distributive law, naming the law on each line.

If you get stuck with laws, build the table of both formulas: at least you will know whether they are equivalent before you keep looking for the route.

Step by step

a) Table with the two columns to compare:

pqp ↔ q¬(p ↔ q)p ⊕ q
TTTFF
TFFTT
FTFTT
FFTFF

The last two columns match row by row: they are equivalent. It makes sense: “they do not have the same value” is “exactly one is true”.

b) Start from the left and remove the arrows:

(p→q)∧(p→r)≡(¬p∨q)∧(¬p∨r)(p→q≡¬p∨q) (p \to q) \land (p \to r) \equiv (\neg p \lor q) \land (\neg p \lor r) \quad (p \to q \equiv \neg p \lor q)
≡¬p∨(q∧r)(distributive law, backwards) \equiv \neg p \lor (q \land r) \quad \text{(distributive law, backwards)}
≡p→(q∧r)(p→q≡¬p∨q, backwards) \equiv p \to (q \land r) \quad (p \to q \equiv \neg p \lor q \text{, backwards})

With the alarm: “if the door opens, it rings” and “if the door opens, the light comes on” is the same as “if the door opens, it rings and the light comes on”.

c) From the outside in:

¬(p∨(¬p∧q))≡¬p∧¬(¬p∧q)(De Morgan) \neg\big(p \lor (\neg p \land q)\big) \equiv \neg p \land \neg(\neg p \land q) \quad \text{(De Morgan)}
≡¬p∧(¬¬p∨¬q)(De Morgan)≡¬p∧(p∨¬q)(double negation) \equiv \neg p \land (\neg\neg p \lor \neg q) \quad \text{(De Morgan)} \qquad \equiv \neg p \land (p \lor \neg q) \quad \text{(double negation)}
≡(¬p∧p)∨(¬p∧¬q)(distributive)≡F∨(¬p∧¬q)(negation) \equiv (\neg p \land p) \lor (\neg p \land \neg q) \quad \text{(distributive)} \qquad \equiv \mathbf{F} \lor (\neg p \land \neg q) \quad \text{(negation)}
≡¬p∧¬q(identity) \equiv \neg p \land \neg q \quad \text{(identity)}

Result: ¬p∧¬q\neg p \land \neg q, “neither the door open nor motion”.

Check it with Python

from itertools import product

def implies(a, b):
    return (not a) or b

def equivalent(f, g, n):
    """True if f and g agree on all 2^n rows."""
    return all(f(*v) == g(*v) for v in product([True, False], repeat=n))

print("a)", equivalent(lambda p, q: not (p == q), lambda p, q: p != q, 2))
print("b)", equivalent(lambda p, q, r: implies(p, q) and implies(p, r),
                       lambda p, q, r: implies(p, q and r), 3))
print("c)", equivalent(lambda p, q: not (p or (not p and q)),
                       lambda p, q: (not p) and (not q), 2))

Output:

a) True
b) True
c) True

In Python, p↔qp \leftrightarrow q is p == q and p⊕qp \oplus q is p != q when they are Booleans. All three parts give True.

Questions you might ask

Do I have to name the law at every step? In an equivalence proof, yes: that is what makes it a proof and not a list of formulas. If you take two steps at once, name both laws.

Can I use the distributive law “backwards”? Yes: an equivalence can be used in both directions. In b) it is used to take ¬p\neg p out as a common factor.

Why doesn’t c) stop at ¬p∧(p∨¬q)\neg p \land (p \lor \neg q)? Because it still simplifies: distributing produces ¬p∧p\neg p \land p, which is a contradiction and disappears.

What do I do if the table says they are not equivalent? Find the row where they differ: that row is the counterexample, and it is enough to justify the answer.

Applying De Morgan without changing the connective: ¬(p∨q)≡¬p∨¬q\neg(p \lor q) \equiv \neg p \lor \neg q is false. The negation goes in and the connective changes: ∨\lor becomes ∧\land and vice versa.

Try it yourself

  • Prove with laws that (p→r)∧(q→r)≡(p∨q)→r(p \to r) \land (q \to r) \equiv (p \lor q) \to r.
  • Simplify (p∧q)∨(p∧¬q)∨¬p(p \land q) \lor (p \land \neg q) \lor \neg p and check that it is a tautology.

Problem 4: are the alarm’s specifications consistent?

Statement. The alarm’s manual says:

  • S1: the alarm rings if and only if night mode is on and the door is open or there is motion: s↔(r∧(p∨q))s \leftrightarrow (r \land (p \lor q)).
  • S2: if the door is open, the alarm rings: p→sp \to s.
  • S3: night mode is on: rr.
  • S4: there is motion or night mode is off: q∨¬rq \lor \neg r.
  • S5: the alarm does not ring: ¬s\neg s.

Are they consistent, that is, is there any situation in which all five hold?

The idea

There are four variables, so the table would have 16 rows. It is quicker to pull the thread: start with the specifications that fix a value without doubt (S3 and S5) and deduce the rest. If we reach the conclusion that one of them has to be false, they are not consistent.

The specifications that are a single letter (or its negation) are the first ones to use: they leave no choice.

Step by step

Consistency of the specificationsChain of deductions: E5 gives s false, E1 and E3 give p and q false, and then E4 is false.S5¬ss = FS1s ↔ (r ∧ (p ∨ q))r ∧ (p ∨ q) = FS3rr = TS1p ∨ q = F → p = F, q = FS2p → sF → F = TS4q ∨ ¬rF ∨ F = FS4 fails: no assignment makes all five true
Start with the specification that fixes a value without doubt and pull the thread.
  1. S5 forces s=s = F.
  2. S1 is a biconditional: since ss is F, the other side must be F as well: r∧(p∨q)=r \land (p \lor q) = F.
  3. S3 forces r=r = T. For r∧(p∨q)r \land (p \lor q) to be F with rr T, we need p∨q=p \lor q = F, that is, p=p = F and q=q = F.
  4. S2: p→sp \to s is F → F, which is T. It holds.
  5. S4: q∨¬rq \lor \neg r is F ∨ F, which is F. It does not hold.

S1, S3 and S5 force everything else, and with those values S4 fails: they are not consistent. Read aloud: if night mode is on and the alarm does not ring, there cannot be motion; but S4 says that, in night mode, there is motion.

Check it with Python

With four variables, Python tries the 16 situations and counts how many satisfy all five:

from itertools import product

def implies(a, b):
    return (not a) or b

good = []
for p, q, r, s in product([True, False], repeat=4):     # 2^4 = 16
    s1 = s == (r and (p or q))
    s2 = implies(p, s)
    s3 = r
    s4 = q or not r
    s5 = not s
    if s1 and s2 and s3 and s4 and s5:
        good.append((p, q, r, s))
print("situations satisfying all five:", len(good))

# and if we drop S4?
without_s4 = [(p, q, r, s) for p, q, r, s in product([True, False], repeat=4)
              if s == (r and (p or q)) and implies(p, s) and r and not s]
print("without S4:", without_s4)

Output:

situations satisfying all five: 0
without S4: [(False, False, True, False)]

Zero situations: they are not consistent. Without S4 there is exactly one left, the one we found by hand (everything F except night mode).

Questions you might ask

Is consistent the same as tautology? No. Consistent means that there exists a row in which they are all T (the conjunction is satisfiable). A tautology would mean they are all T in every row.

Why don’t I need to check the 16 rows by hand? Because each deduction removes rows: S5 leaves 8, S3 leaves 4 and S1 leaves 1. All that remains is to check that one.

And if a specification fixes nothing? Leave it for the end; sometimes you have to try two cases (T and F) for a variable and see whether either works.

What happens in real life with inconsistent specifications? The system cannot satisfy them all: one is badly written. Here, S4 looks like a mistake in the manual.

Reading S1 as an implication and deducing only in one direction. With a biconditional, if one side is F so is the other; with an implication s→…s \to \dots, an F in ss says nothing about the other side.

Try it yourself

  • Change S5 to ss (the alarm rings). Are they consistent now? How many situations satisfy them?
  • Add to the original five S6: ¬p\neg p. Does anything change?

Problem 5: quantifiers in a small domain

Statement. With the domain D={−2,−1,0,1,2}D = \{-2, -1, 0, 1, 2\} for both xx and yy, give the truth value of each statement and write its negation with no ¬\neg in front of any quantifier:

a) ∀x (x2≥x)\forall x\, (x^2 \ge x) b) ∃x (x2=x+2)\exists x\, (x^2 = x + 2) c) ∀x ∃y (x+y=0)\forall x\, \exists y\, (x + y = 0) d) ∀x ∃y (x<y)\forall x\, \exists y\, (x < y)

The idea

With a finite domain, ∀\forall is a conjunction of five cases and ∃\exists a disjunction. For a ∀\forall, look for a case that fails (if there is none, it is T); for an ∃\exists, a case that works. To negate, the ¬\neg moves inside, flipping each quantifier, and the predicate is negated.

When negating the predicate, watch out for inequalities: the negation of x2≥xx^2 \ge x is x2<xx^2 < x, not x2≤xx^2 \le x.

Step by step

a) x2≥xx^2 \ge x for each xx: 4≥−24 \ge -2, 1≥−11 \ge -1, 0≥00 \ge 0, 1≥11 \ge 1, 4≥24 \ge 2. All T: the statement is T. Negation: ∃x (x2<x)\exists x\, (x^2 < x), which is F (there is no case).

b) x2=x+2x^2 = x + 2: with x=2x = 2, 4=44 = 4. There is already one: T (also x=−1x = -1: 1=11 = 1). Negation: ∀x (x2≠x+2)\forall x\, (x^2 \ne x + 2).

c) For each xx we need a yy in DD with x+y=0x + y = 0, that is, y=−xy = -x. Since DD is symmetric (if xx is in it, so is −x-x), it always exists: T. Negation: ∃x ∀y (x+y≠0)\exists x\, \forall y\, (x + y \ne 0).

d) For each xx, a larger yy. For x=2x = 2 there is none in DD: x=2x = 2 is a counterexample and the statement is F. Negation: ∃x ∀y (x≥y)\exists x\, \forall y\, (x \ge y), which is T with x=2x = 2. Notice that over the integers (an infinite domain) d) would be T: the domain changes the truth value.

Check it with Python

Python’s quantifiers are all (∀\forall) and any (∃\exists); nesting them means putting one inside the other.

D = [-2, -1, 0, 1, 2]

a = all(x**2 >= x for x in D)
b = any(x**2 == x + 2 for x in D)
c = all(any(x + y == 0 for y in D) for x in D)
d = all(any(x < y for y in D) for x in D)
print("a)", a, " b)", b, " c)", c, " d)", d)

# the negations must give exactly the opposite
print("¬a)", any(x**2 < x for x in D))
print("¬d)", any(all(x >= y for y in D) for x in D))
print("witnesses for b):", [x for x in D if x**2 == x + 2])

Output:

a) True  b) True  c) True  d) False
¬a) False
¬d) True
witnesses for b): [-1, 2]

Each negation gives the opposite of its statement, as it should, and the list of witnesses for b) is the two values we found by hand.

Questions you might ask

Why does it matter in c) that DD is symmetric? Because yy has to be in the domain. With D={0,1,2}D = \{0, 1, 2\}, for x=1x = 1 we would need y=−1y = -1, which is not there, and c) would be F.

How do you negate ∀x ∃y\forall x\, \exists y? Step by step: ¬∀x ∃y P≡∃x ¬∃y P≡∃x ∀y ¬P\neg \forall x\, \exists y\, P \equiv \exists x\, \neg \exists y\, P \equiv \exists x\, \forall y\, \neg P. Each quantifier flips and the negation ends up on the predicate.

Does a counterexample work for an ∃\exists? No: an ∃\exists is only knocked down by checking that all cases fail. Counterexamples knock down ∀\forall.

What if the domain is empty? ∀x P(x)\forall x\, P(x) is T (nobody fails) and ∃x P(x)\exists x\, P(x) is F. It is the same idea as the vacuously true implication.

Changing the order of the quantifiers when negating. ¬∀x ∃y\neg \forall x\, \exists y is ∃x ∀y\exists x\, \forall y: same order of variables, quantifiers flipped.

Try it yourself

  • With the same DD, what is the value of ∃y ∀x (x⋅y=0)\exists y\, \forall x\, (x \cdot y = 0)? And of ∃y ∀x (x+y=0)\exists y\, \forall x\, (x + y = 0)?
  • Write the negation of ∃x ∀y (x≤y→x2≤y2)\exists x\, \forall y\, (x \le y \to x^2 \le y^2) with no ¬\neg in front of the quantifiers.

Problem 6: three proofs

Statement.

a) Prove by contrapositive: if 3n+23n + 2 is odd, nn is odd (nn an integer). b) Prove by induction: for every n≥1n \ge 1, 1+3+5+⋯+(2n−1)=n21 + 3 + 5 + \dots + (2n – 1) = n^2. c) Show that this is false: “for every integer n≥1n \ge 1, 2n>n22^n > n^2“.

The idea

a) Directly, “3n+23n + 2 is odd” gives you nothing convenient about nn. The contrapositive, “if nn is even, 3n+23n + 2 is even”, starts from something you can work with (n=2kn = 2k). b) It is a “for every n≥1n \ge 1” about a sum that grows one term at a time: textbook induction. c) It is a false ∀\forall: one counterexample is enough.

In induction, before the inductive step write P(k)P(k) and P(k+1)P(k+1) in full. Having in front of you where you want to get is half the work.

Step by step

a) Contrapositive: “if nn is even, 3n+23n + 2 is even”.

If nn is even, n=2kn = 2k with kk an integer. Then 3n+2=6k+2=2(3k+1)3n + 2 = 6k + 2 = 2(3k + 1), which has the form 2m2m with m=3k+1m = 3k + 1 an integer: it is even. Since the contrapositive is true and equivalent to the original, so is the original. ∎

b) Let P(n)P(n): 1+3+⋯+(2n−1)=n21 + 3 + \dots + (2n – 1) = n^2.

Base case (n=1n = 1): on the left there is only the first odd number, 2⋅1−1=12 \cdot 1 – 1 = 1; on the right, 12=11^2 = 1. They match.

Inductive step. Assume P(k)P(k): 1+3+⋯+(2k−1)=k21 + 3 + \dots + (2k – 1) = k^2. We want P(k+1)P(k+1): 1+3+⋯+(2k−1)+(2k+1)=(k+1)21 + 3 + \dots + (2k – 1) + (2k + 1) = (k + 1)^2. The last term is 2(k+1)−1=2k+12(k+1) – 1 = 2k + 1. Start from the left:

(1+3+⋯+(2k−1))+(2k+1)=k2+2k+1=(k+1)2 \big(1 + 3 + \dots + (2k – 1)\big) + (2k + 1) = k^2 + 2k + 1 = (k + 1)^2

where the first step uses the induction hypothesis. That is P(k+1)P(k+1). By induction, P(n)P(n) is true for every n≥1n \ge 1. ∎

Check with n=5n = 5: 1+3+5+7+9=25=521 + 3 + 5 + 7 + 9 = 25 = 5^2.

c) Try small values: n=1n = 1: 2>12 > 1, T. n=2n = 2: 4>44 > 4, F. n=2n = 2 settles it: it is a counterexample and the statement is false. (It also fails for n=3n = 3, with 8>98 > 9, and n=4n = 4, with 16>1616 > 16; from n=5n = 5 on it does hold, and that could be proved by induction starting at 5.)

Check it with Python

Python does not prove anything (it checks cases), but it helps you spot a mistake in a formula or find counterexamples:

# a) for even n, 3n + 2 is even (checked over a range)
print("a)", all((3*n + 2) % 2 == 0 for n in range(-100, 101, 2)))

# b) sum of the first n odd numbers versus n^2
for n in [1, 2, 5, 10, 100]:
    total = sum(2*i - 1 for i in range(1, n + 1))
    print(f"b) n = {n:3}: sum = {total:5}, n^2 = {n**2:5}")

# c) look for counterexamples to 2^n > n^2
print("c) counterexamples:", [n for n in range(1, 20) if not 2**n > n**2])

Output:

a) True
b) n =   1: sum =     1, n^2 =     1
b) n =   2: sum =     4, n^2 =     4
b) n =   5: sum =    25, n^2 =    25
b) n =  10: sum =   100, n^2 =   100
b) n = 100: sum = 10000, n^2 = 10000
c) counterexamples: [2, 3, 4]

The counterexamples to c) are 2, 3 and 4, and from 5 onwards it holds. For b), the sums match n2n^2 in every case tried; that gives confidence, but the proof is the induction.

Questions you might ask

Why not prove a) directly? You can, but you have to work with “3n+2=2k+13n + 2 = 2k + 1“, rearrange to 3n=2k−13n = 2k – 1 and reason about the parity of 3n3n: it is longer. The contrapositive starts from n=2kn = 2k and is done in one line.

Where is the induction hypothesis used in b)? At the first equals sign: the sum up to 2k−12k – 1 is replaced by k2k^2. If you do not use it, it is not a proof by induction.

Why is the last term of P(k+1)P(k+1) 2k+12k + 1 and not 2k−12k – 1? Because it is the general term 2n−12n – 1 with n=k+1n = k + 1. It is the most common mistake when writing P(k+1)P(k+1).

Does “I checked it with Python up to 100” count as a proof of b)? No: checking cases never proves a ∀\forall over infinitely many values. It does count for c), because there one case is enough.

In the inductive step, starting from P(k+1)P(k+1) and “arriving” at P(k)P(k), or assuming what you want to prove. Start from the expression for k+1k+1, use P(k)P(k) in one step and arrive at the form of P(k+1)P(k+1).

Try it yourself

  • Prove by induction that 2+4+⋯+2n=n(n+1)2 + 4 + \dots + 2n = n(n+1) for every n≥1n \ge 1.
  • Prove by contrapositive that, if n2n^2 is odd, nn is odd.

Problem 7: this is how the exam asks it

The classroom problems exam is done on the Campus: problems you work out and whose answer you choose from options or fill in, worth 1 or 1.5 points. From topic 1, the main thing that comes up is getting the truth table of a proposition. This is an original problem in that format.

Problem (1 point). Build the truth table of the proposition   (¬p∨q)→(r↔p)  \;(\neg p \lor q) \to (r \leftrightarrow p)\; with the rows in the usual order (pp, qq, rr from TTT to FFF) and choose the last column, read from top to bottom: a) T F T F T F T F · b) F T F F T F T F · c) T F T T F T F T · d) T T T T F T F T. Also say whether the proposition is a tautology, a contradiction or a contingency.

The idea

Three variables, eight rows. The formula’s tree gives the columns: ¬p\neg p, ¬p∨q\neg p \lor q, r↔pr \leftrightarrow p and the final implication. For the last one, the golden rule: F only when the left side is T and the right side F.

The exam’s options usually differ in only a few rows. Before ticking, check two or three individual rows of the option you chose against the original formula, without looking at your table.

Step by step

Column by column:

  • ¬p\neg p: F in the first four rows, T in the last four.
  • ¬p∨q\neg p \lor q: T if ¬p\neg p is T or qq is T. It is F only in rows 3 and 4 (pp T and qq F).
  • r↔pr \leftrightarrow p: T when rr and pp match: rows 1 and 3 (T, T) and rows 6 and 8 (F, F).
  • Last: F only if ¬p∨q\neg p \lor q is T and r↔pr \leftrightarrow p is F. That happens in rows 2, 5 and 7.
Truth table step by stepTruth table with one column per subformula and the last column highlighted.pqr¬p¬p ∨ qr ↔ p(¬p ∨ q) → (r ↔ p)TTTFTTTTTFFTFFTFTFFTTTFFFFFTFTTTTFFFTFTTTTFFTTTFFFFFTTTT
Read from top to bottom, the last column is T F T T F T F T: that is option c.
pqr¬p¬p ∨ qr ↔ p(¬p ∨ q) → (r ↔ p)
TTTFTTT
TTFFTFF
TFTFFTT
TFFFFFT
FTTTTFF
FTFTTTT
FFTTTFF
FFFTTTT

The last column, read from top to bottom, is T F T T F T F T: option c. It has T rows and F rows: it is a contingency.

Check it with Python

from itertools import product

def implies(a, b):
    return (not a) or b

options = {"a": "TFTFTFTF", "b": "FTFFTFTF", "c": "TFTTFTFT", "d": "TTTTFTFT"}
col = "".join("T" if implies((not p) or q, r == p) else "F"
              for p, q, r in product([True, False], repeat=3))
print("last column:", col)
print("correct option:", [k for k, v in options.items() if v == col])
print("contingency" if "T" in col and "F" in col else "not a contingency")

Output:

last column: TFTTFTFT
correct option: ['c']
contingency

Check and how it is marked

Three individual rows against the formula, without looking at the table:

  • Row 2 (T, T, F): ¬p∨q\neg p \lor q = F ∨ T = T; r↔pr \leftrightarrow p = F ↔ T = F; T → F = F. Option c has F in second place.
  • Row 4 (T, F, F): ¬p∨q\neg p \lor q = F ∨ F = F; F → anything = T. Option d also has T there, but a does not.
  • Row 7 (F, F, T): ¬p∨q\neg p \lor q = T ∨ F = T; r↔pr \leftrightarrow p = T ↔ F = F; T → F = F. That rules out d, which has T in seventh place.

This is how it is marked on the Campus (a rough guide, not official): the question is worth the full point only with the correct option; if it is single-answer and the penalty applies, a wrong option can subtract. That is why it pays to build the whole table and do the three-row check before ticking.

Mixing up r↔pr \leftrightarrow p with r→pr \to p. They differ in the rows where rr is F and pp is T (rows 2 and 4): there r→pr \to p is F → T, which is T, but the biconditional is F because they do not match. With that mix-up, row 2 comes out T and you end up ticking d.

Questions you might ask

What if the rows come in a different order in the exam? Read the statement: if it gives another order, use it. If it says nothing, use the course notes’ order (TTT to FFF). With another order the last column is the same, but permuted.

Can I skip the intermediate columns? Nobody sees your paper on the Campus, but without them you will make more mistakes. What saves time is the “where is it F” rule.

What if my column matches none of the options? Review the column with the most operations (usually the second to last) and check the rows where your column differs from the closest option.

How long should it take me? With practice, about 5 minutes for three variables. The whole problems quiz lasts 1 h 35, so there is time to check.

Try it yourself

In the same format: the truth table of (p⊕q)→(¬r∧q)(p \oplus q) \to (\neg r \land q) and its last column.

See the solution to the Try it yourself

Columns: p⊕qp \oplus q is T in rows 3, 4, 5 and 6; ¬r∧q\neg r \land q is T in rows 2 and 6. The last column is F when p⊕qp \oplus q is T and ¬r∧q\neg r \land q is F: rows 3, 4 and 5.

pqrp ⊕ q¬r ∧ qLast
TTTFFT
TTFFTT
TFTTFF
TFFTFF
FTTTFF
FTFTTT
FFTFFT
FFFFFT

Last column: T T F F F T T T. It is a contingency.

Common mistakes in this practice

  • Counting the rows wrong. With nn variables there are 2n2^n. With three, eight; with four, sixteen. Count them before you start.
  • Filling in the variables in no order. That way combinations get repeated or lost. pp changes every 2n−12^{n-1} rows, the next one every 2n−22^{n-2}…
  • Reading “only if” as “if”. “pp only if qq” is p→qp \to q: the necessary condition goes on the right.
  • De Morgan without changing the connective. ¬(p∧q)≡¬p∨¬q\neg(p \land q) \equiv \neg p \lor \neg q, with ∨\lor.
  • Negating an “all” with a “none”. ¬∀x P(x)\neg \forall x\, P(x) is “some do not”, not “none do”.
  • Using examples to prove a “for all”. They only work to refute it.

Check it with Python: everything together

If you want a tool for your own tables, this program takes the formula as Python text and prints the table and the classification. Change the formula in the last line for the one you are practising:

from itertools import product

def table(variables, formula):
    """formula: Python text with and, or, not; for → use imp(a, b)."""
    imp = lambda a, b: (not a) or b
    col = []
    for vals in product([True, False], repeat=len(variables)):
        env = dict(zip(variables, vals), imp=imp)
        v = eval(formula, {}, env)
        col.append("T" if v else "F")
        print(" ".join("T" if x else "F" for x in vals), "|", col[-1])
    kind = ("tautology" if "F" not in col else
            "contradiction" if "T" not in col else "contingency")
    print("".join(col), "->", kind)

table(["p", "q", "r"], "imp((not p) or q, r == p)")

Output:

T T T | T
T T F | F
T F T | T
T F F | T
F T T | F
F T F | T
F F T | F
F F F | T
TFTTFTFT -> contingency

It is the same table as in problem 7, now row by row. For ↔\leftrightarrow use == and for ⊕\oplus, !=.

Summary

  • Formalising means finding the connective behind each turn of phrase: “only if” and “necessary” put the condition on the right of the arrow.
  • A truth table is always built the same way: 2n2^n rows in a fixed order and one column per subformula, from the inside out.
  • For an implication column, look only for the T → F rows; all the others are T.
  • Two formulas are equivalent if they agree on every row; with laws, name one law per step.
  • A set of specifications is consistent if some assignment makes them all T: pull the thread from the ones that fix a value.
  • To prove: contrapositive if the hypothesis gives you nothing; induction for “every n≥1n \ge 1“; a counterexample to knock down a “for all”.

Checklist: have you practised it?

  • Can you formalise “only if”, “unless” and “necessary and sufficient” without hesitating?
  • Can you build a three-variable table in the course notes’ order without missing rows?
  • Can you prove an equivalence with laws, naming each one?
  • Do you know how to negate a statement with two quantifiers?
  • Have you run the programs and checked the outputs?
  • Have you tried the “Try it yourself” tasks without looking at the theory?

If you said yes to almost everything, you are ready for the logic and proofs exercises, where I no longer give you the solution up front: first the statement and, folded underneath, the worked solution. The topic’s theory quiz, like the ones on the Campus, is there too. And if you want to check your tables instantly, the “Truth table and equivalences” simulator builds them column by column. See you there!

Next step: exercisesLogic and proofs — exam-style exercises with truth tables, arguments with a catch and a divisibility induction (and the neighbour who did not call)

Did this article help you? Here is how you can support SergioLearns →