Day 1 — Integers & order of operations

Updated

July 30, 2026

Day 1 — Integers & order of operations

Stage I · concept day
Goal: Master integer structure, signed arithmetic, precedence and associativity with proofs and counterexamples, parity in full, absolute value with the triangle inequality, and expression-tree thinking.

Why this matters

Every later algebra step, complexity calculation, index arithmetic, and modular argument assumes you never lose track of parentheses, signs, and left-to-right rules. Mistakes here cascade into “wrong answers that look like algebra.” Compilers and interpreters encode the same ideas as precedence tables and abstract syntax trees. This day makes integer arithmetic automatic, justified, and checkable.

Theory

The integers \(\mathbb{Z}\)

The set of integers is

\[\mathbb{Z} = \{\ldots,-3,-2,-1,0,1,2,3,\ldots\}.\]

Structural properties (ring axioms, informal list):

Property Statement
Closure under \(+\) \(a,b\in\mathbb{Z}\Rightarrow a+b\in\mathbb{Z}\)
Closure under \(\times\) \(a,b\in\mathbb{Z}\Rightarrow ab\in\mathbb{Z}\)
Closure under \(-\) \(a,b\in\mathbb{Z}\Rightarrow a-b\in\mathbb{Z}\)
Associativity of \(+\) \((a+b)+c=a+(b+c)\)
Commutativity of \(+\) \(a+b=b+a\)
Additive identity \(a+0=a\)
Additive inverse \(a+(-a)=0\)
Associativity of \(\times\) \((ab)c=a(bc)\)
Commutativity of \(\times\) \(ab=ba\)
Multiplicative identity \(1\cdot a=a\)
Distributivity \(a(b+c)=ab+ac\)

Not closed under division in \(\mathbb{Z}\): \(1/2\notin\mathbb{Z}\). The natural numbers \(\mathbb{N}\) are \(\{0,1,2,\ldots\}\) or \(\{1,2,3,\ldots\}\) by convention; when it matters, say which you use. Zero is an integer; \(0\) is even because \(0=2\cdot 0\).

Even and odd (parity) — full case set

An integer \(n\) is even if \(\exists k\in\mathbb{Z}\) with \(n=2k\).
An integer \(n\) is odd if \(\exists k\in\mathbb{Z}\) with \(n=2k+1\).

Every integer is exactly one of even or odd (remainder \(0\) or \(1\) on division by \(2\); formalized Day 5).

Theorem (parity of sums and products). Let \(m,n\in\mathbb{Z}\).

  1. even \(+\) even \(=\) even
  2. odd \(+\) odd \(=\) even
  3. even \(+\) odd \(=\) odd
  4. even \(\times\) even \(=\) even
  5. even \(\times\) odd \(=\) even
  6. odd \(\times\) odd \(=\) odd

Proof of (1). Write \(m=2k\), \(n=2\ell\). Then \(m+n=2(k+\ell)\), even. \(\square\)

Proof of (2). Write \(m=2k+1\), \(n=2\ell+1\). Then \[m+n=2k+2\ell+2=2(k+\ell+1),\] even. \(\square\)

Proof of (3). \(m=2k\), \(n=2\ell+1\) \(\Rightarrow\) \(m+n=2(k+\ell)+1\), odd. \(\square\)

Proof of (4). \(m=2k\), \(n=2\ell\) \(\Rightarrow\) \(mn=4k\ell=2(2k\ell)\), even. \(\square\)

Proof of (5). \(m=2k\), \(n=2\ell+1\) \(\Rightarrow\) \(mn=2k(2\ell+1)=2(\cdots)\), even. \(\square\)

Proof of (6). \(m=2k+1\), \(n=2\ell+1\) \(\Rightarrow\) \[mn=(2k+1)(2\ell+1)=4k\ell+2k+2\ell+1=2(2k\ell+k+\ell)+1,\] odd. \(\square\)

Corollary. \(n\) even \(\Leftrightarrow\) \(n^2\) even; \(n\) odd \(\Leftrightarrow\) \(n^2\) odd. (Use (4) and (6).)

Order of operations (precedence)

Standard mathematical convention for evaluating an expression:

  1. Grouping — parentheses (and brackets, braces) from the inside out
  2. Exponents — usually right-associative: \(a^{b^c}=a^{(b^c)}\)
  3. Unary minus / signs — careful interaction with exponents (see below)
  4. Multiplication and divisionleft to right (same precedence)
  5. Addition and subtractionleft to right (same precedence)

PEMDAS/BODMAS is incomplete if you forget that \(\times\) and \(\div\) share a level, and \(+\) and \(-\) share a level. Always scan left to right within a level.

Programming note: languages define their own precedence tables. When unsure, parenthesize. Mathematics and code are not guaranteed to match on unary minus and exponentiation.

Associativity and commutativity

Let \(\ast\) be a binary operation on a set.

  • Commutative: \(a\ast b = b\ast a\) for all \(a,b\).
  • Associative: \((a\ast b)\ast c = a\ast(b\ast c)\) for all \(a,b,c\).
Operation Commutative? Associative?
\(+\) yes yes
\(\times\) yes yes
\(-\) no no
\(\div\) no no

Counterexamples (associativity).

\[(5-3)-1=1,\qquad 5-(3-1)=3,\] so \((a-b)-c\neq a-(b-c)\) in general.

\[(16\div 4)\div 2=2,\qquad 16\div(4\div 2)=8.\]

Counterexamples (commutativity). \(3-1\neq 1-3\); \(8\div 2\neq 2\div 8\).

Useful rewrite: define subtraction as adding the inverse, \[a-b := a+(-b).\] Then associativity of \(+\) yields \[(a-b)-c = a+(-b)+(-c)=a-(b+c),\] which is a reliable identity (both sides equal \(a-b-c\) in usual notation).

Theorem. Addition and multiplication on \(\mathbb{Z}\) are associative and commutative (taken as ring axioms / standard facts). Subtraction and division (where defined) are neither.

Unary minus and signs

\(-a\) is the additive inverse of \(a\), not “a binary minus stuck in front.”

Laws.

\[ \begin{align*} -(-a)&=a,\\ (-a)b &= -(ab) = a(-b),\\ (-a)(-b)&=ab,\\ -(a+b)&=(-a)+(-b)=-a-b. \end{align*} \]

Proof sketch of \((-a)(-b)=ab\).
\((-a)(-b)+(-a)b = (-a)\bigl((-b)+b\bigr)=(-a)\cdot 0=0\), so \((-a)(-b)\) is the additive inverse of \((-a)b=-(ab)\). The inverse of \(-(ab)\) is \(ab\). \(\square\)

Convention for \(-3^2\): in most school algebra and many languages that lack ** as a single operator binding tighter than unary minus, \(-3^2=-(3^2)=-9\), not \((-3)^2=9\). Write \((-3)^2\) if you mean the square of negative three.

Integer powers

For \(a\in\mathbb{Z}\) and \(n\) a nonnegative integer:

\[a^0=1\quad(a\neq 0\text{ in some contexts; }0^0\text{ left undefined}),\qquad a^{n+1}=a^n\cdot a.\]

For negative exponents (when \(a\neq 0\) and we allow rationals): \(a^{-n}=1/a^n\). Today stay mostly in nonnegative integer exponents.

Laws (integer \(m,n\ge 0\), with care at zero base):

\[a^m a^n=a^{m+n},\qquad (a^m)^n=a^{mn},\qquad (ab)^n=a^n b^n.\]

Exponent towers: for positive integers, \(a^{b^c}\) is almost always \(a^{(b^c)}\), not \((a^b)^c\). Note \((a^b)^c=a^{bc}\), while \(a^{(b^c)}\) grows much faster when bases and exponents exceed \(1\).

Nested parentheses and expression trees

An arithmetic expression can be viewed as a tree: leaves are integers (or variables later); internal nodes are operators. Evaluation is a post-order traversal: evaluate children, then apply the operator.

Example: \((3+4)\times(2-5)\)

  • Root: \(\times\)
  • Left child: \(+\) of \(3,4\)
  • Right child: \(-\) of \(2,5\)
  • Value: \(7\times(-3)=-21\)

Fully parenthesized forms remove all ambiguity. Precedence is a human/compiler convention that inserts the “missing” parentheses implicitly.

Absolute value

For \(x\in\mathbb{Z}\) (or \(\mathbb{R}\)),

\[|x|=\begin{cases}x & \text{if }x\ge 0,\\ -x & \text{if }x<0.\end{cases}\]

Axiomatic properties (all \(a,b\in\mathbb{Z}\)):

  1. \(|a|\ge 0\), and \(|a|=0\Leftrightarrow a=0\)
  2. \(|-a|=|a|\)
  3. \(|ab|=|a||b|\)
  4. \(|a+b|\le |a|+|b|\) (triangle inequality)

Theorem (triangle inequality). For all integers \(a,b\), \[|a+b|\le |a|+|b|.\]

Proof. Note \(-|a|\le a\le |a|\) and \(-|b|\le b\le |b|\). Adding: \[-(|a|+|b|)\le a+b\le |a|+|b|.\] If a number \(x\) satisfies \(-M\le x\le M\) with \(M\ge 0\), then \(|x|\le M\). Apply with \(x=a+b\) and \(M=|a|+|b|\). \(\square\)

Corollary. \(||a|-|b||\le |a-b|\) (reverse triangle). Sketch: \(|a|=|(a-b)+b|\le |a-b|+|b|\) so \(|a|-|b|\le |a-b|\); swap \(a,b\).

Worked examples

Example 1 — Precedence

Evaluate \(3+4\times 2\).

Multiplication before addition: \(4\times 2=8\), then \(3+8=\mathbf{11}\).
(The incorrect left-to-right “add first” path gives \(14\).)

Example 2 — Left-to-right at one level

Evaluate \(16\div 4\div 2\).

Same precedence, left to right: \((16\div 4)\div 2 = 4\div 2 = \mathbf{2}\).
Not \(16\div(4\div 2)=8\).

Example 3 — Mixed chain

Evaluate \(7-3\times 2+8\div 4\).

First \(\times\) and \(\div\): \(3\times 2=6\), \(8\div 4=2\).
Expression becomes \(7-6+2\). Left to right: \((7-6)+2=1+2=\mathbf{3}\).

Example 4 — Nested parentheses

Evaluate \(2\bigl(3-(4-7)\bigr)^2\).

Innermost: \(4-7=-3\). Then \(3-(-3)=6\). Then \(6^2=36\). Then \(2\cdot 36=\mathbf{72}\).

Example 5 — Exponent tower vs power of a power

Compare \(2^{3^2}\) and \((2^3)^2\).

  • \(2^{3^2}=2^{(3^2)}=2^9=\mathbf{512}\).
  • \((2^3)^2=8^2=\mathbf{64}=2^{6}\).

Different expressions; never conflate them.

Example 6 — Unary minus vs parentheses

Evaluate \(-2^4\) and \((-2)^4\).

\(-2^4=-(2^4)=-16\).
\((-2)^4=16\).
Also \((-2)^3=-8\), while \(-2^3=-8\) (same in this case).

Example 7 — Signed arithmetic chain

Evaluate \(-3-(-5)+(-2)\times 4\).

Product first: \((-2)\times 4=-8\).
Then \(-3-(-5)+(-8)=-3+5-8=2-8=\mathbf{-6}\).

Example 8 — Integer powers and product law

Compute \(2^5\cdot 2^3\) and \((2^5)^3\).

\(2^5\cdot 2^3=2^{8}=256\).
\((2^5)^3=2^{15}=32768\).

Example 9 — Expression tree

Fully parenthesize \(a-b\times c+d\) under standard precedence as \((a-(b\times c))+d\), and evaluate at \(a=10\), \(b=3\), \(c=2\), \(d=1\): \((10-6)+1=\mathbf{5}\).

Example 10 — Absolute value

Compute \(|-7|\), \(|3-11|\), and \(|-3|\cdot|-4|\).

\(7\); \(|-8|=8\); \(3\cdot 4=12\). Check \(|(-3)(-4)|=|12|=12\).

Example 11 — Triangle inequality numeric check

For \(a=5\), \(b=-3\): \(|a+b|=|2|=2\), \(|a|+|b|=8\), and \(2\le 8\).
For \(a=-4\), \(b=-6\): \(|a+b|=10=|a|+|b|\) (equality when same sign).

Example 12 — Full parity proof

Claim. The sum of an even integer and an odd integer is odd.

Proof. Let \(m\) be even and \(n\) odd: \(m=2k\), \(n=2\ell+1\). Then \[m+n=2k+(2\ell+1)=2(k+\ell)+1,\] odd. \(\square\)

Example 13 — Product of odds

Claim. The product of two odds is odd.
Proof as in Theory (6). Numeric check: \(7\cdot 9=63=2\cdot 31+1\).

Example 14 — Identity for subtraction

Show \((a-b)-c=a-(b+c)\) for \(a=10\), \(b=3\), \(c=4\): left \(7-4=3\); right \(10-7=3\).

Exercises

Solve by hand. Show steps. Calculator only after you have an answer.

Easy

  1. Evaluate: \(7-3\times 2+8\div 4\).
  2. Evaluate: \((7-3)\times(2+8)\div 4\).
  3. Evaluate \(-5+(-3)\times 2\) and \(-5+(-3\times 2)\); are they equal?
  4. Compute \(|-12|\), \(|8-15|\), and \(|-2|^3\) (interpret carefully).
  5. Evaluate \(2^{4}\cdot 2^{3}\) and \((2^{4})^{3}\).

Medium

  1. Evaluate both \(2^{3\times 2}\) and \((2^3)\times 2\); compare with \(2^{3^2}\) under tower convention.
  2. Insert parentheses into \(6\div 2\times 3+1\) to obtain \(10\) if possible; also try to obtain \(4\). Write each fully parenthesized form.
  3. True or false: subtraction is associative. If false, give a concrete counterexample with integers.
  4. True or false: \((a-b)-c = a-(b+c)\) for all integers \(a,b,c\). Prove or disprove.
  5. Evaluate \(-2^4\) and \((-2)^4\). Explain the difference in words.
  6. If a language evaluated all binary ops strictly left-to-right with equal precedence (no PEMDAS), what would \(10-3\times 2\) become? What does standard math give?
  7. Draw (or describe) the expression tree for \((2+3)\times(4-1)^2\). Evaluate it.
  8. Prove: \(|ab|=|a||b|\) for all integers \(a,b\) (case analysis on signs, or use \(a=\pm|a|\)).

Hard / proof

  1. Prove: the product of two odd integers is odd. (Use \(2k+1\) forms.)
  2. Prove: the sum of two even integers is even; the sum of two odd integers is even.
  3. Prove: \(n\) is even if and only if \(n^2\) is even. (Both directions; one uses contrapositive or cases.)
  4. Prove the triangle inequality \(|a+b|\le|a|+|b|\) as in the Theory section, writing every step.
  5. Prove \(||a|-|b||\le|a-b|\).
  6. Disambiguate each plain-text string with parentheses in two different ways and evaluate both:
    1. 8/2/2 (b) 2^3^2 (c) -3^2+1
  7. Show that the average of two integers need not be an integer; relate to “not closed under division.”

Challenge / CS-flavored

  1. An array is \(0\)-indexed with length \(n=10\). The last valid index is \(n-1\). Explain why \(n-1\) is not “subtract first then …”; it is ordinary arithmetic. What goes wrong if someone writes \(n+1\) for the last index?
  2. Invent three integer expressions that look “obvious” but trip precedence; write the correct value and the common wrong value.
  3. Prove: if \(a\) and \(b\) are both even or both odd, then \(a+b\) is even and \(a-b\) is even. (Same parity \(\Rightarrow\) difference even.)
  4. For \(a=100\), \(b=-37\), verify triangle inequality and compute the “slack” \((|a|+|b|)-|a+b|\).
  5. Explain why \(0\) is even using the definition \(n=2k\). Is \(0\) positive? Negative? Neither?

CS connection

Operator precedence and integer overflow are everyday programming footguns. Expression trees in compilers are built from formal precedence/associativity tables; when humans and languages disagree, bugs appear as “off by a factor of two” or wrong branch conditions. Absolute value and triangle-style bounds appear in distance metrics, error analysis, and complexity inequalities. Prefer parentheses for anything non-trivial, and never assume school PEMDAS matches your language’s unary-minus or exponent rules. Parity arguments show up in hashing, tiling proofs, and “two-color” invariants.

Common pitfalls

Pitfall What to do instead
Doing \(+\) before \(\times\) Apply precedence deliberately; rewrite in steps
Treating \(\div\) chain as right-to-left Same level → left to right
Assuming \(a^{b^c}=(a^b)^c\) Use tower convention or force parentheses
\(-3^2\) vs \((-3)^2\) Square first unless parentheses group the minus
Sign errors with double minus Rewrite \(a-(-b)\) as \(a+b\)
“Proof by example” for parity laws Use \(2k\) / \(2k+1\) definitions
Forgetting \(|x|\) is never negative \(|x|\ge 0\) always
Claiming subtraction is associative Counterexample with concrete integers

Checkpoint

  • State the ring-like properties of \(\mathbb{Z}\) (closure, identities, inverses for \(+\))
  • State precedence levels from memory (including left-to-right within a level)
  • Evaluate a mixed \(+,-,\times,\div\) chain without a calculator
  • Explain exponent tower convention and give a numeric example
  • Prove all six parity sum/product cases from definitions (or write them cleanly once)
  • State and prove \(|a+b|\le|a|+|b|\)
  • Describe an expression as a tree and evaluate post-order
  • Write one sentence on why parentheses matter in code and math

Write two takeaways in your own words.

Tomorrow

Day 2 — Fractions & rationals. Equality via cross-multiplication, field operations on \(\mathbb{Q}\), reducing by gcd, mixed numbers, complex fractions, rates, density of \(\mathbb{Q}\), and a light look at Egyptian fractions.