AP COMPUTER SCIENCE PRINCIPLES • ALGORITHMS AND PROGRAMMING

Undecidable Problems

Some problems can never be solved by any algorithm, no matter how powerful the computer.

Historical Context & Motivation

In the early twentieth century, mathematicians believed that every well-posed mathematical question could, in principle, be answered by a systematic procedure. David Hilbert famously articulated this optimism in 1928 when he posed the Entscheidungsproblem (the "decision problem"), asking whether there exists a general algorithm that can determine the truth or falsity of any mathematical statement. This question drove some of the most profound discoveries in the foundations of computer science, ultimately revealing that computation itself has inherent, unavoidable limits.

1928
Hilbert's Entscheidungsproblem
David Hilbert and Wilhelm Ackermann formally pose the decision problem: can an algorithm always determine whether a statement in first-order logic is universally valid?
1931
Gödel's Incompleteness Theorems
Kurt Gödel proves that any sufficiently powerful formal system contains true statements that cannot be proven within the system, foreshadowing undecidability.
1936
Turing & Church Prove Undecidability
Alan Turing introduces the Turing machine and proves the halting problem is undecidable. Alonzo Church independently reaches the same conclusion using lambda calculus.
1970
Matiyasevich's Theorem
Yuri Matiyasevich completes the proof that Hilbert's tenth problem — determining whether a Diophantine equation has integer solutions — is undecidable, extending undecidability into number theory.

Turing's 1936 result was transformative: it showed that no single algorithm can analyze arbitrary programs and always correctly determine whether they will halt or run forever. This halting problem became the canonical example of an undecidable problem and permanently reshaped our understanding of what computers can and cannot do. For the AP Computer Science Principles exam, understanding undecidability means recognizing that the power of algorithms has fundamental boundaries — not because of engineering limitations, but because of mathematical impossibility.

Core Principles & Definitions

To understand undecidable problems, we must first distinguish between different categories of problems based on whether algorithms can solve them. A decidable problem is one for which an algorithm exists that can provide a correct yes-or-no answer for every possible input in a finite amount of time. An undecidable problem is one for which no such algorithm can ever exist — not because we haven't found one yet, but because it has been mathematically proven that no algorithm can correctly solve all instances of the problem.

1

Decidable Problem

An algorithm exists that always terminates and gives a correct yes/no answer for every input. Example: "Is this number even?"
2

Undecidable Problem

No algorithm can correctly answer yes/no for all inputs in finite time. It is mathematically impossible — not just currently unsolved.
3

The Halting Problem

Given an arbitrary program and its input, determine whether the program will eventually halt or run forever. This is the most famous undecidable problem.
4

Proof by Contradiction

Undecidability is typically proven by assuming a solution exists and showing it leads to a logical contradiction, making the assumption impossible.
KEY TAKEAWAY
KEY TAKEAWAY

Visualizing the Halting Problem

The diagram traces the proof by contradiction for the halting problem. We assume a decider H(P, I) exists, then construct a program D that does the opposite of whatever H predicts. Running D(D) creates a logical contradiction, proving that H cannot exist.

The key insight of this proof is the technique of self-reference. We construct program D that deliberately does the opposite of whatever the supposed halting decider H predicts. When we then ask H to decide whether D halts on itself, H is forced into a contradiction no matter what it answers. If H says "halts," D loops forever; if H says "loops," D halts. Since both outcomes are impossible, our assumption that H exists must be false. This diagonalization argument is the same structural technique Cantor used to prove the uncountability of real numbers.

How the Proof Works Step by Step

Formal Structure of the Halting Problem Proof

The proof proceeds via proof by contradiction (also called reductio ad absurdum). We assume the thing we want to disprove — that a universal halting decider exists — and then show this assumption leads to an impossible situation. The argument requires only basic logic and is accessible without advanced mathematics.

ASSUMPTION
Assume H(P, I) exists, where H always halts and correctly outputs "HALTS" if P(I) halts, or "LOOPS" if P(I) runs forever.
H = hypothetical halting decider; P = any program; I = any input to P
CONSTRUCTION OF D
D(P) = { loop forever if H(P, P) = "HALTS"; halt if H(P, P) = "LOOPS" }
D is a new program that uses H as a subroutine and deliberately contradicts H's output.
THE CONTRADICTION
D(D): If H(D, D) = "HALTS" → D loops ∴ H was wrong. If H(D, D) = "LOOPS" → D halts ∴ H was wrong.
In either case, H gives the wrong answer. Since H is supposed to be correct on all inputs, we have a contradiction. Therefore, H cannot exist.

This proof is significant because it makes no assumptions about the speed, memory, or cleverness of H. It could be running on a quantum computer with infinite memory, and the contradiction still holds. The impossibility is inherent in the logical structure of the problem, not in the limitations of any particular machine. For the AP exam, remember that undecidable does not mean "hard" — it means "provably impossible to solve in general."

Decidable vs. Undecidable Problems

It is crucial to understand that undecidability is a property of problems in general, not of specific instances. A single instance of the halting problem — for example, asking whether a program that prints "hello" and stops will halt — may be trivially answerable. The undecidability means that no single algorithm can correctly handle every possible program-input pair. This distinction between individual instances and the general problem is a key conceptual point on the AP exam.

This Venn-style diagram shows how problems are classified. Efficiently solvable problems sit inside decidable problems, while undecidable problems lie outside the decidable boundary entirely — no algorithm of any speed can solve them in general.
Examples of decidable and undecidable problems
ProblemDecidable?Why?
Is integer n even?YesCheck last bit / modulo 2; always terminates with correct answer.
Is a list sorted?YesScan once comparing adjacent elements; finite and deterministic.
Does program P halt on input I?NoTuring proved no algorithm can decide this for all P and I.
Do programs P and Q produce the same output for all inputs?NoReducible to the halting problem; checking equivalence in general is undecidable.
Does a given program contain a virus?No (in general)By Rice's theorem, any non-trivial semantic property of programs is undecidable.

Worked Example: Reasoning About Undecidability

Suppose a software company claims to have built a tool that can analyze any program and determine whether it will produce an output within 60 seconds. Let's reason step by step about whether this claim can be true in general.

1
Step 1 — Identify the Problem StructureThe claim states: for any program P and input I, the tool returns "YES" (outputs within 60s) or "NO" (does not output within 60s). This is asking us to decide a property about the runtime behavior of arbitrary programs.
This is a question about the behavior of arbitrary programs — a red flag for undecidability.
2
Step 2 — Relate to the Halting ProblemIf we could solve this problem for all programs and inputs, we could also solve the halting problem. Here's how: given any program P and input I, we could modify P to ignore any time limit and simply check if it ever produces output. If our tool could correctly predict whether any program produces output, it could determine whether any program halts — but we know that's impossible.
The tool's supposed capability reduces to (is at least as hard as) the halting problem.
3
Step 3 — Apply UndecidabilitySince the halting problem is undecidable, any problem that is at least as hard as the halting problem must also be undecidable. Therefore, no tool can correctly determine for all programs whether they produce output within a given time.
The company's claim cannot be true in the general case. Their tool may work for many programs but will necessarily fail on some.
4
Step 4 — Important CaveatThis does not mean the tool is useless. In practice, tools can analyze many specific programs correctly or use heuristics and approximations. Undecidability means no tool can be correct for every possible program — there will always exist some program for which it gives the wrong answer or cannot decide.
Undecidability is a statement about the general case, not about individual instances.

Practical Implications & Limitations

Understanding undecidability has important practical consequences for software engineering, cybersecurity, and artificial intelligence. Although we cannot build perfect general-purpose program analyzers, we can build tools that work well in restricted domains or that provide approximate answers. The following table contrasts what is and is not possible given undecidability.

Practical boundaries imposed by undecidability
What We CAN DoWhat We CANNOT Do
Analyze specific programs for halting using testing, simulation, or restricted analysisBuild a single algorithm that correctly determines halting for every possible program
Use heuristic virus scanners that catch most known malwareBuild a perfect virus detector that identifies all malicious programs with zero false negatives
Prove correctness for programs written in restricted subsets of programming languagesAutomatically verify correctness of all programs in a Turing-complete language
Use timeouts and approximations to give useful (but incomplete) answersGuarantee that every answer from a general program analyzer will be correct
KEY TAKEAWAY
KEY TAKEAWAY

Connection to Advanced Computability Theory

The AP Computer Science Principles exam focuses on recognizing that undecidable problems exist and understanding the halting problem as the primary example. In more advanced computer science courses, you would encounter a rich hierarchy of undecidable problems, techniques for classifying their relative difficulty, and broader frameworks for understanding computation's limits.

AP level vs. advanced computability theory
AP CSP LevelAdvanced CS Theory
Know that undecidable problems existClassify undecidable problems into the arithmetic hierarchy
Understand the halting problem as the key exampleUse reductions to prove new problems undecidable by relating them to the halting problem
Recognize that some problems cannot be solved by any algorithmStudy Rice's theorem: all non-trivial semantic properties of programs are undecidable
Distinguish decidable from undecidableDistinguish semi-decidable (recognizable) from fully undecidable (unrecognizable)

One important concept you may encounter beyond the AP exam is semi-decidability (also called recognizability). The halting problem is actually semi-decidable: if a program does halt, we can eventually confirm that by simply running it. But if it loops forever, we can never be certain — we'd be waiting indefinitely. A fully decidable problem requires that we always get a definitive answer in finite time for both "yes" and "no" cases.

Practice Problems

1
Which of the following best describes an undecidable problem?
2
A programmer writes a program that takes another program as input and runs it for 1,000 steps. If the input program halts within 1,000 steps, it reports "halts." Otherwise, it reports "does not halt." Why does this NOT solve the halting problem?
3
A company claims their new AI tool can determine, for any two programs, whether they always produce the same output for every possible input. Select the two statements that are true about this claim.
PROBLEM 4APPLIED
A security researcher wants to build a tool that examines any program and determines whether it will ever access a user's private files without permission. Explain why this goal is unachievable in general and describe what practical approaches the researcher could use instead.
PROBLEM 5CRITICAL THINKING
Consider the following claim: "The halting problem proves that we can never know whether any specific program halts." Evaluate this claim. In your response: (a) explain why this claim is incorrect, (b) clarify what the halting problem actually proves, (c) give an example of a program whose halting behavior IS decidable, and (d) explain the difference between solving a specific instance and solving the general problem.
Varsity Tutors • AP Computer Science Principles • Undecidable Problems