# Programming for Mathematics

Mathematics I · Programming Elements · https://tryals.app/learn/mathematics-i/programming-for-mathematics

## Computation as a Mathematical Tool

Computation has become a third pillar alongside theory and experiment, extending mathematics rather than replacing rigour.

| Approach | What it Does | Core Method | Example |
| --- | --- | --- | --- |
| **Numerical** | Solves problems without closed-form solutions **approximately** | Iteration, refinement | Newton's method |
| **Symbolic** | Manipulates expressions **exactly** via **computer algebra systems** | Exact algebra | Factoring, integrals |
| **Simulation** | Turns abstract structures into visible, testable data | **Monte Carlo** random sampling | Estimating probabilities |
| **Verification** | Checks millions of cases or verifies formal logic | **Proof assistants** | Four-colour theorem |

Programming turns static mathematics into something you can probe, but output is only as trustworthy as its logic.

## Pitfalls and Monte Carlo Methods

**Common pitfall:** treating a **program's numerical output as exact mathematical truth**. Floating-point results (Lesson 2) are approximate and accumulate rounding error. A simulation estimates rather than proves, and a bug produces confident wrong answers. Computation supports work, but a printed number is not automatically a theorem.

Programs let you **experiment** with mathematics to test conjectures and visualise systems. A classic example is the **Monte Carlo** simulation:

Random accent points are scattered in a square with an inscribed quarter-circle. The fraction landing inside times 4 estimates pi, converging as more points are added.

$$\pi \approx 4 \cdot \frac{\text{points inside}}{\text{total points}}$$

## Practice questions

7 of this lesson's 13 practice questions, with answers. The full set is in the app.

### 1. Sort each task by the kind of computation it needs.

**Answer:**

- Numerical: Approximate the integral of the Gaussian from 0 to 1 to six decimals, Find a root of a messy equation by Newton's method, Solve a 1000 by 1000 linear system to ten decimal places
- Symbolic: Return an exact formula for the derivative of x cubed times sin x, Factor a polynomial into exact linear factors

**Why:** Symbolic computation manipulates expressions exactly and returns formulas; numerical computation returns approximate numbers where no closed form is available or wanted. The phrase 'to n decimals' is the giveaway that you are in the numerical world.

Page: https://tryals.app/practice/mathematics-i/programming-for-mathematics/sort-each-task-by-the-kind-of-computation-it-needs

### 2. In a Monte Carlo estimate of pi, points are scattered in a unit square containing a quarter-circle and the estimate is four times the fraction landing inside. If 800 of 1000 points land inside, what is the estimate of pi?

**Answer:** 3.2 (within ±0.01)

**Why:** 800/1000 = 0.8, and 4 * 0.8 = 3.2. The true value is 3.14159..., so this run is off by about 0.06, a reminder that a Monte Carlo answer is a sample statistic, not a computation of pi.

Page: https://tryals.app/practice/mathematics-i/programming-for-mathematics/in-a-monte-carlo-estimate-of-pi-points-are-scattered-in-a-unit

### 3. A test suite passes and the program is wrong. Which of these does testing structurally fail to catch?

A. A regression introduced after the tests were written
B. A crash on an input included in the suite
C. An arithmetic error inside a tested function
D. A missing case nobody thought to test

**Answer:** D. A missing case nobody thought to test

**Why:** Tests are a finite sample of intended behaviour, so a case you never considered produces no failing test, there is nothing to fail. That is Dijkstra's point about presence and absence of bugs, and why coverage metrics mislead: they measure lines executed, never the requirement you forgot.

Page: https://tryals.app/practice/mathematics-i/programming-for-mathematics/a-test-suite-passes-and-the-program-is-wrong-which-of-these-does

### 4. Complete the description of a simulated result.

**Answer:** A Monte Carlo estimate of a probability is **a statistical estimate** of the true value.

**Why:** Random sampling yields a statistic with sampling error, not a computed value. It should always be quoted with a sense of how large that error might be, which Unit 9 makes precise.

Page: https://tryals.app/practice/mathematics-i/programming-for-mathematics/complete-the-description-of-a-simulated-result

### 5. Testing a program on a hundred inputs and finding no failures does not prove it correct. Why is testing nonetheless the dominant method, rather than proof?

A. Because programs are too unreliable to reason about formally, so empirical evidence is the only option.
B. Because tests are cheap, catch most defects, and scale to real systems.
C. Because proofs of program correctness are impossible in principle.
D. Because a sufficiently large test suite does constitute a proof of correctness.

**Answer:** B. Because tests are cheap, catch most defects, and scale to real systems.

**Why:** It is the counterexample asymmetry from Unit 1: a universal claim cannot be established by finite checking, and a function on two 64-bit integers has more inputs than could be tested in the lifetime of the universe. Testing dominates on economics rather than logic, and formal verification is real, with seL4 and CompCert both carrying machine-checked proofs.

Page: https://tryals.app/practice/mathematics-i/programming-for-mathematics/testing-a-program-on-a-hundred-inputs-and-finding-no-failures-does

### 6. Order the steps of using a program to investigate a conjecture.

**Answer:**

1. State the conjecture precisely enough to test
2. Write a program that checks it on many cases
3. Run it and inspect any cases that fail
4. If none fails, record the result as evidence rather than proof
5. Seek a general proof, or a counterexample beyond the range tested

**Why:** Precision comes first because the machine needs a decidable question; the computation supplies evidence or a counterexample; and the mathematics resumes afterwards. Euler's prime-generating polynomial survived 40 cases and then failed, evidence never closes the question.

Page: https://tryals.app/practice/mathematics-i/programming-for-mathematics/order-the-steps-of-using-a-program-to-investigate-a-conjecture

### 7. Because a computer performs arithmetic faster and more reliably than a human, a numerical result it prints can be taken as an exact mathematical fact.

**Answer:** False

**Why:** False. Floating-point values are approximate and rounding accumulates, so a printed number carries error even when every step ran correctly. Computation supports mathematical work but its output must be verified with the same scepticism as any argument.

Page: https://tryals.app/practice/mathematics-i/programming-for-mathematics/because-a-computer-performs-arithmetic-faster-and-more-reliably-than
