AP Computer Science Principles • Algorithms and Programming

Undecidable Problems

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

Computer science principles illustration with a network, database, and code file
Inside this lesson 9 sections 5 questions 8 figures

SECTION 1

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.

SECTION 2

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.

SECTION 3

Visualizing the Halting Problem

The Halting Problem — Proof by ContradictionProgram P+ Input IH(P, I)Supposed halting deciderOutputs:"HALTS""LOOPS"Now build a contradicting program D:D(P):if H(P, P) == "HALTS": loop foreverif H(P, P) == "LOOPS": haltFeed D to itself: D(D)If H says D halts → D loopsIf H says D loops → D haltsCONTRADICTION!∴ H cannot exist
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.

SECTION 4

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

AssumeH(P,I)exists,whereHalwayshaltsandcorrectlyoutputs"HALTS"ifP(I)halts,or"LOOPS"ifP(I)runsforever.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)=loopforeverifH(P,P)="HALTS";haltifH(P,P)="LOOPS"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):IfH(D,D)="HALTS"→Dloops∴Hwaswrong.IfH(D,D)="LOOPS"→Dhalts∴Hwaswrong.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."

SECTION 5

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.

Problem Classification HierarchyAll ProblemsDecidable ProblemsEfficiently SolvableIs n even?Sort a listShortest pathDecidable butintractableTraveling Salesman(optimal)UndecidableHalting ProblemPost CorrespondenceRice's Theorem casesProgram equivalence
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.
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.

Examples of decidable and undecidable problems

SECTION 6

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.

Worked example

  1. 1

    Step 1 — Identify the Problem Structure

    The 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. 2

    Step 2 — Relate to the Halting Problem

    If 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. 3

    Step 3 — Apply Undecidability

    Since 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. 4

    Step 4 — Important Caveat

    This 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.

SECTION 7

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.

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

Practical boundaries imposed by undecidability

SECTION 8

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 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)

AP level vs. advanced computability theory

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.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

Which of the following best describes an undecidable problem?
  • A. A problem that takes too long to solve on current hardware
  • B. A problem for which no algorithm can provide a correct yes/no answer for all possible inputs
  • C. A problem that has not yet been solved but may be solvable in the future
  • D. A problem that requires exponential time to solve
Show answer (B)
B is correct. An undecidable problem is one for which it has been mathematically proven that no algorithm can correctly answer yes or no for every possible input in finite time. A is wrong because undecidability is not about hardware limitations or speed — it is a mathematical impossibility. C is wrong because undecidability is not about a temporary gap in knowledge; it is a proven permanent limitation. D is wrong because problems requiring exponential time are intractable but still decidable — an algorithm exists that will eventually produce the correct answer.

Question 2: BASIC

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?
  • A. It only checks 1,000 steps; a program might halt at step 1,001 but would be reported as non-halting
  • B. The program cannot take other programs as input
  • C. The program will loop forever because the halting problem is undecidable
  • D. A 1,000-step timeout is too short; a longer timeout would solve the halting problem
Show answer (A)
A is correct. The tool might incorrectly report "does not halt" for a program that halts at step 1,001 or later; it does not give correct answers for all inputs. B is wrong because programs can indeed accept other programs as input (programs are just data). C is wrong because the analyzer itself always halts after 1,000 steps — it is the answer that may be incorrect, not the analyzer's own termination. D is wrong because no finite timeout solves the problem: for any timeout N, there exist programs that halt at step N+1. And an infinite timeout would mean the analyzer never finishes.

Question 3: INTERMEDIATE

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.
  • A. This problem (program equivalence) is undecidable, so the tool cannot work correctly for all pairs of programs
  • B. The tool could still be useful for checking equivalence of many specific program pairs in practice
  • C. With sufficient computing power, the tool could be made to work for all programs
  • D. Program equivalence is decidable if both programs are written in the same language
Show answer (A,B)
A and B are correct. Program equivalence is undecidable (reducible to the halting problem), so no tool can guarantee correct answers for all program pairs (A). However, practical tools can use heuristics, testing, and restricted analysis to correctly compare many specific programs (B). C is wrong because undecidability is not about computing power — it is a mathematical impossibility regardless of resources. D is wrong because the programming language is irrelevant; any Turing-complete language can express programs for which equivalence is undecidable.

Question 4: APPLIED

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.
Show answer
Determining whether an arbitrary program will access private files without permission is an undecidable problem. By Rice's theorem, any non-trivial property about what a program does (its semantic behavior) cannot be decided algorithmically for all programs. This means any tool will inevitably produce false positives (flagging safe programs) or false negatives (missing malicious ones) on some inputs. Rubric: (1 point) Correctly identifies this as undecidable by referencing the halting problem or Rice's theorem. (1 point) Explains that undecidability applies to the general case — individual instances may be checkable, but no single algorithm handles all programs correctly. (1 point) Offers at least one practical alternative: heuristic virus scanners, sandboxing (running programs in isolated environments and observing behavior), permission-based access controls, or signature matching against known malware patterns.

Question 5: CRITICAL 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.
Show answer
(a) The claim is a common and important misconception. The halting problem does NOT say that we can never determine whether a specific program halts. Many individual programs can be easily analyzed. (b) What the halting problem actually proves is that there is no single algorithm that can correctly determine, for every possible program and every possible input, whether the program will halt. The impossibility is about building a universal halting decider, not about analyzing individual cases. (c) Consider the program: print('hello'). This program clearly halts — it executes one statement and terminates. Similarly, while True: pass clearly never halts. Both of these are trivially decidable instances. (d) The distinction between instances and the general problem is crucial. A specific instance is one particular program with one particular input — and we may be able to determine its halting behavior through testing, inspection, or formal reasoning. The general problem asks for an algorithm that handles every instance correctly. Undecidability means that for any proposed universal algorithm, there will always exist some program-input pair that causes it to fail. Rubric: (1 point) Identifies the claim as a misconception. (1 point) Accurately describes the halting problem's actual claim about the non-existence of a universal algorithm. (1 point) Gives a concrete example of an easily analyzable program. (1 point) Clearly articulates the instance vs. general problem distinction.

Varsity Tutors • AP Computer Science Principles • Undecidable Problems