3
Example Prime Numbers
Tirsvad edited this page 2026-11-25 17:32:28 +01:00

Example: Prime Numbers

🌐 English · Dansk · Bahasa Melayu

A filled-in AI Prompt Template, prompt 1b (no feature questions), for a very small system: a command-line program in C that finds the prime numbers from 0 to 1,000,000, fast, in a small executable.

The other example, Volunteer Football Club, uses prompt 1 and shows the questions an agent raises. This one shows the opposite: the agent asks nothing about features and writes every choice down. It still stops for review at every step.

1. The prompt

Use the create-project skill. Start a new project. Do not ask me feature
questions: decide them yourself.

Idea (one sentence): A command-line program that prints every prime number
  from 0 to 1,000,000, for anyone who needs the list or wants a fast, small
  reference implementation.
Belief that would kill it if false: the full list can be produced in a blink
  with an executable of only a few kilobytes.
Validated or a hunch: hunch (a demonstration project).
Stage: prototype.
Constraints: one week, no budget, one developer, runs on a normal Linux or
  Windows PC, no network.

Product Owner: <name>  (language: English, domain: number theory)
Reviewer for each step: <name, not the author>
Acceptor: <name>
Project name: primes   (kebab-case; also the folder and the git repository)
User interface or API: none (command line); wireframes: none
Security profile: internal documents only   (a local program: no network, no personal data)

Technology (record as ADRs):
  Language / platform: C, ISO/IEC 9899:2024 (C23), GCC or Clang
  Other requirements: use constexpr to make the program fast and keep the
    executable file small

What the system must do:
  - User: list all prime numbers from 0 to 1,000,000

How to decide for me:
- Where this prompt is silent, choose the simplest option that meets the idea
  and the constraints. Do not ask me about it.
- Record every such choice: a feature or rule as an Assumption in the Business
  Case, a technology or design choice as an ADR. Tag it `Decided by default`
  and name the alternative you rejected.
- Ask me, and stop, only if a field above that you cannot do without is empty
  (the Product Owner's language, the reviewer, the acceptor), or if the choice
  involves personal data, security, legal duty or spending money.

Rules:
- Planning first: do not write code or anything under src/ or tests/.
- One step at a time. After each document, stop and wait for my review.
- Do not commit, push or open a pull request unless I ask.

1a. From Business Case to code

The agent walks one reviewed step at a time. Every step ends the same way: you review, the agent records an RC-* review record, and on Go the row is Accepted and the next step may start.

Step Artifact In this example
0 Intake answers written into the Business Case, the registry and the Stakeholder Analysis The prompt above; nothing is asked
1 Business Case BC, with the security requirements and the data classification The Decided by default assumptions
2 Stakeholder Analysis SA The Product Owner, the reviewer and the acceptor
3 Security Baseline SBL: the profile and a decision for each of B01 to B18 Right after the Stakeholder Analysis, before the Project Plan; smallest profile
4 Project Plan PP Three gateways, section 6
5 Stories, use cases, priority US, UCD, MoSCoW label and, with several actors, a role matrix One actor and one goal: one story, one use case, no role matrix
6 Milestones and tasks MIL; each applying control becomes a task The tasks are issues once the milestone is Accepted
7 Threat Model THM After the milestones and before the design; section 8
8 Use cases and design UC, DM, SSD, OC, SD, DCD, ERD as needed, TP with the security tests, ADRs UC-001, the ADRs of section 2, the Test Plan
9 Operations Plan OPS, before the first release None: B10 to B17 are opted out
10 Code src/ and tests/, one pull request per task that Closes #N, reviewed against qc-programming-c.md Only when the milestone is Accepted with a Go review

2. Choices the agent makes instead of asking

These are the same kind of ambiguities that the football example turns into questions. Here each becomes a Decided by default entry for you to challenge at review.

Choice Decided by default Rejected alternative Recorded in
Is 0 or 1 prime? No: a prime is greater than 1, so the first prime is 2 List 0 and 1 as special cases Business Case, Assumptions
Is 1,000,000 inside the range? Yes, inclusive; it is not prime, so the last prime is 999,983 Stop below 1,000,000 Business Case, Assumptions
Output One prime per line in decimal on standard output, ascending, no header A count only; a comma-separated line Business Case, Assumptions
Exit code 0 on success, non-zero if writing fails Always 0 Use case UC-001
Algorithm Sieve of Eratosthenes Trial division (slower), segmented sieve (more code for no gain at 1,000,000) ADR
Sieve storage A bit per odd number only, 500,000 bits (62.5 KB) in zero-initialised memory, so it adds nothing to the file One byte per number (1 MB); a precomputed table in the file ADR
What constexpr does See section 3 Computing the primes at compile time ADR
Small executable Dynamic linking, -Os, stripped, unused sections removed, no printf for the output Static linking ADR
Platform Linux and Windows, one portable C23 source file set Linux only ADR

3. The constexpr decision

The prompt asks for constexpr to make the program fast and small. The agent has to check what C allows, because constexpr means more in C++ than in C.

  • In C23, constexpr applies to objects only. There are no constexpr functions, so a C program cannot run a sieve at compile time.
  • A table of all 78,498 primes baked into the file would be about 314 KB as 32-bit numbers, or about 62.5 KB as an odd-number bit table. Both are far larger than the program's code, which would break "small file".
  • A sieve at run time for 1,000,000 numbers takes milliseconds and its bit table sits in zero-initialised memory, which costs no space in the file.

So the ADR records: use constexpr for the values that fix the sizes and loop limits (the limit, its integer square root, the number of odd candidates, the bit-table size), so the compiler folds them into the code and the table has a fixed size known at build time; sieve at run time. If the Product Owner later wants compile-time computation, the rejected alternative is C++ (constexpr functions) and that is a change of the Business Case constraint, not a coding detail.

4. Use case

ID Actor Goal Main flow Alternative flows
UC-001 User List all prime numbers from 0 to 1,000,000 The user starts the program. The system finds the primes up to the limit and writes them in ascending order, one per line. Standard output is closed or full: stop and return a non-zero exit code

One actor and one goal, so one use case. Building the sieve and formatting the output are plain technical tasks, not use cases.

5. Design

Layers follow the clean-architecture skill. For one use case an ADR merges the application and infrastructure layers into presentation; the domain layer stays separate.

primes/
├── src/
│   ├── domain/         # sieve.h, sieve.c: constexpr limits, prime test, no I/O
│   └── presentation/   # main.c: write the primes, set the exit code
└── tests/              # unit/ (known counts and boundaries), e2e/ (run the program)

Names carry the module prefix because C has no namespaces: sieve_build(), sieve_is_prime().

6. Milestones the agent will propose

Gateway Deliverable Go/No-Go (examples)
MIL-001 Baseline Business Case with the Decided by default assumptions, Stakeholder Analysis, Project Plan Reviewer Go; every default read and kept or changed
MIL-002 Design UC-001, ADRs (algorithm, storage, constexpr, size, platform), Security Baseline, Threat Model Each artifact Go
MIL-003 Release UC-001 working, tests green, size and speed measured Output matches the known counts; size and time within the targets the reviewer set

7. Code stage

Once MIL-003 is Accepted with a Go review and its tasks are issues, the agent may write C under src/ with coding-conventions (C) and clean-architecture, and review it against qc-programming-c.md.

  • Tests first, from facts that need no program to verify: 4 primes up to 10, 25 up to 100, 168 up to 1,000, 78,498 up to 1,000,000; the last prime is 999,983.
  • Boundary values: 0, 1, 2, 3, 4, 999,983, 999,999 and 1,000,000.
  • Build with -std=c23 -Wall -Wextra -Wpedantic, and run once under -fsanitize=address,undefined.
  • Measure, do not assume: record the stripped file size and the run time in the pull request, and compare them with the targets of the milestone.

8. Security and operations

The program has no network, no personal data and nothing to deploy, so the agent applies the smallest profile, which the prompt names, and writes down what it leaves out. How to use each artifact is on Security and operations.

The security artifacts sit in the workflow in three places (see section 1a): the Security Baseline after the Stakeholder Analysis and before the Project Plan, the Threat Model after the milestones and before the design, and the Operations Plan before the first release.

Security Baseline (SBL). Profile: internal documents only (nothing leaves the machine), as the prompt says; internal service does not fit, because there is no service.

Control Decision How it is met
B01 Security requirements and data classification Applies Business Case: the output is public numbers, no personal data
B02 Threat model Applies The Threat Model below; C has no memory safety, so it earns its place
B04 Secure coding and code review Applies Review against qc-programming-c.md before every pull request
B05 Dependency inventory Applies The inventory is "the C standard library only", stated in the Business Case
B07 Static analysis Applies -Wall -Wextra -Wpedantic and a run under -fsanitize=address,undefined
B08 Security and negative tests Applies The tests of the Test Plan below
B09 Reviewed merge Applies A reviewer merges
B10 to B17 Release integrity, deployment, operations Opted out Reason: the program is built and run on one machine, nothing is deployed. Owner: the Product Owner. Approver: the reviewer. Expires: <date>, or when the program is shared with others

Threat Model (THM). Assets: the printed list (integrity), the executable, the process memory. One boundary: standard output to whatever reads it.

Threat Category Rating Mitigation Test
T-001 Write outside the 500,000-bit table Tampering Medium The table size is a constexpr value, indexes are checked against it, sanitizer run TC-010
T-002 Integer overflow in the index near 1,000,000 Tampering Medium Unsigned 32-bit arithmetic with the limit known at compile time TC-011
T-003 A failed write to standard output goes unnoticed Denial of service Low Non-zero exit code (the alternative flow of UC-001) TC-012
T-004 A swapped executable Spoofing Low Accepted: the program is not distributed (B10 opted out) none, expires with the opt-out

Test Plan (TP). Security and negative tests added to the coverage matrix: TC-010 builds and runs under the sanitizers and finds no error; TC-011 checks 999,983, 999,999 and 1,000,000; TC-012 closes standard output and expects a non-zero exit code. Each cites its threat.

Operations Plan (OPS). None, because B10 to B17 are opted out. If the program is later published as a download, the opt-out lapses, B10 (checksums) and B16 (how a defect is reported) apply, and an Operations Plan is written.

9. What to check afterwards

  • Every Decided by default entry names its rejected alternative.
  • The Business Case says 0 and 1 are not prime and that the range includes 1,000,000.
  • The constexpr ADR says what C23 allows and why the sieve runs at run time.
  • The file-size and speed targets are numbers the reviewer set, not words.
  • The count 78,498 is tested.
  • The Security Baseline names the profile, and every opt-out has a reason, an owner, an approver and an expiry date.
  • Every threat rated Medium or higher has a mitigation and a test.
  • Reviews were done by someone other than the author.