Topic 3.18: Undecidable Problems | AP CSP Big Idea 3 | APCSExamPrep.com
Undecidable Problems
After this lesson, you will be able to:
- Define a decision problem as a yes-or-no question about its input
- Explain that a decidable problem has an algorithm correct for every input that always halts
- Explain that an undecidable problem has no algorithm correct for every possible input
- Identify the halting problem as the classic undecidable problem
- Distinguish undecidable from merely slow or unreasonable-time problems
- Write and trace runnable deciders that print True or False
Computers feel all-powerful, but there are yes-or-no questions no program can ever answer correctly for every case. Not slow to answer. Not answerable with a bigger computer. Genuinely impossible. The most famous is deciding whether a given program will eventually stop or loop forever. This final lesson of Big Idea 3 draws the line between what algorithms can decide and what lies permanently beyond them.
Decision Problems: Yes-or-No Questions
A decision problem is any problem that can be phrased as a yes-or-no question about its input. "Is n even?" is a decision problem. "Does the value x appear in this list?" is a decision problem. "Is this number prime?" is a decision problem. The answer is always exactly one of two things: yes or no, which in code is True or False. A program that answers a decision problem is often called a decider.
def is_even(n): if n % 2 == 0: return True else: return False print(is_even(6))
PROCEDURE isEven(n) { IF (n MOD 2 = 0) { RETURN (true) } ELSE { RETURN (false) } } DISPLAY(isEven(6))
The decider above answers "is n even?" for any integer you hand it. Run it in your head with 6: 6 % 2 is 0, so it returns True. Hand it 7 and it returns False. The important thing is that it always finishes and it is always correct, no matter which integer you choose. That property has a name.
The CED anchor for this topic is AAP-4.B. You are expected to explain the difference between a problem that a computer can always solve correctly and one that no algorithm can ever solve correctly for every input. This is a concept-and-vocabulary topic, so precise definitions matter more than tracing here.
Decidable Problems
A decision problem is decidable if an algorithm can be written that produces a correct yes-or-no answer for every possible input, and always finishes running. The words "every" and "always" carry the whole definition. It is not enough for the algorithm to work on the inputs you tried; it must be guaranteed correct on all of them, including inputs no one has tested yet.
def contains(t, x): for v in t: if v == x: return True return False print(contains([3, 8, 5], 8))
PROCEDURE contains(t, x) { FOR EACH v IN t { IF (v = x) { RETURN (true) } } RETURN (false) } DISPLAY(contains([3, 8, 5], 8))
"Does x appear in list t?" is decidable. The decider walks the list, and because the list is finite it must reach the end, so the loop always stops and the answer is always right. "Is n prime?" is decidable too: test every candidate divisor up to n; there are finitely many, so the check always terminates with a correct verdict. Almost every algorithm you have written this year solves a decidable problem.
To argue a problem is decidable, describe a specific algorithm and explain why it (1) is correct for every input and (2) always halts. If you can do both, the problem is decidable.
Undecidable Problems and the Halting Problem
A decision problem is undecidable if no algorithm can be built that always gives a correct yes-or-no answer for every possible input. This is not a statement about today's computers or about clever people who have not tried hard enough. It is a proven mathematical fact: for an undecidable problem, a correct general algorithm cannot exist, ever, on any computer.
The classic example is the halting problem: given an arbitrary program and an input to it, decide whether that program will eventually stop or will run forever. It sounds answerable, and for many specific programs you can tell at a glance. But there is no single algorithm that answers it correctly for every program and input. Alan Turing proved this in 1936.
# IMAGINED function that CANNOT exist def halts(program, data): # would return True if program stops on data, # False if program runs forever ... # The proof of impossibility feeds this idea a copy # of itself, forcing a contradiction either way.
The proof imagines a perfect halting-checker and then builds a program that asks the checker about itself and does the opposite of whatever the checker predicts. If the checker says "halts," the program loops forever; if it says "runs forever," the program stops. Either answer is wrong, so the perfect checker cannot exist. You are not expected to reproduce the proof, only to know that the halting problem is undecidable and is the standard example.
n even?" What does it print for 7?def is_even(n): return n % 2 == 0 print(is_even(7))
Undecidable Is NOT the Same as Slow
This is the single most tested trap in the topic, so read it twice. A problem being slow, even absurdly slow, is completely different from a problem being undecidable.
- Slow / unreasonable time: a correct algorithm exists, but it may take an impractical amount of time (for example, checking every possible password, which grows exponentially). The answer is reachable in principle; you just might wait longer than a lifetime.
- Undecidable: a correct general algorithm does not exist at all. No amount of waiting, faster hardware, or cleverness produces one, because it is impossible.
An exponential-time problem is still decidable. Given unlimited time, its algorithm always finishes with the right answer. The halting problem is in a different league: there is nothing to run to completion because the deciding algorithm cannot be written in the first place.
If an answer choice says a problem is undecidable "because it would take too long" or "because there are too many cases to check," it is describing efficiency, not decidability, and it is wrong. Undecidable means no correct algorithm can exist, period. Also beware the reverse: a problem that can be solved for some inputs but is not guaranteed correct for every input is still undecidable.
Writing Deciders You Can Actually Run
You cannot write a program that solves an undecidable problem, but you can and should master the deciders for decidable problems, because those are exactly what the code section drills. A good decider returns or prints True or False, is correct for every input in its domain, and always terminates. Watch out for the most common decider bug: returning an answer too early inside a loop before all the data has been examined.
True or False, capitalized).t = [2, 4, 6] x = 5 print(x in t)
Key Vocabulary
| Term | Definition | Example |
|---|---|---|
| Decision problem | A problem phrased as a yes-or-no question about its input | Is n even? |
| Decider | An algorithm that answers a decision problem True or False | is_even(n) |
| Decidable | An algorithm exists that is correct for every input and always halts | is x in a list |
| Undecidable | No algorithm can give a correct answer for every possible input | the halting problem |
| Halting problem | Deciding whether an arbitrary program stops or runs forever | proven undecidable |
| Unreasonable time | A correct algorithm exists but is far too slow; still decidable, not undecidable | exponential search |
Every procedure you write for the Create Task solves a decidable problem: it produces a correct result and it always finishes. Understanding decidability lets you reason clearly about your own program instead of just hoping it works.
A point-earning example
Suppose your program includes a decider that reports whether a target value is present in a list:
In your written response you can describe this procedure precisely:
- "This procedure answers a yes-or-no decision problem: it returns
truewhentargetis indataandfalseotherwise." - "Because
datais a finite list, the loop always reaches the end, so the procedure always terminates with a correct result. The problem it solves is decidable." - "The procedure uses a return value that the rest of my program depends on, which is why it is one of my two selected code segments."
The trap to avoid
Do not claim your program "solves" something no algorithm can, and do not confuse a slow procedure with an impossible one. If your algorithm always halts with the right answer, it is decidable, even if it is not the fastest possible. See the full Create Task module →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice, study tips, and exam strategies.
n prime?" What does it display?n % 2 always gives a correct answer for any integer, so it is decidable.- I. Deciding whether a given integer n is prime.
- II. Deciding whether an arbitrary program halts on a given input.
- III. Deciding whether a given value appears in a fixed, finite list.
True when x appears anywhere in list t, and False otherwise. What is the bug?== is the correct comparison operator; = would be assignment and cause an error.if v == x branch, so the comparison does happen first.n is even. Target output: True
x appears in list t, and print True or False. Target output: True
is_even(n) that returns True or False, then print is_even(15). Target output: False
True when x is anywhere in t, but it returns too early. Fix it so it checks the whole list. Target output: True
n is prime and print True or False. A prime is an integer at least 2 with no divisor other than 1 and itself. Target output: True
- eligible when
ageis at least 18 andhas_idis True - otherwise not eligible
t is sorted in non-decreasing order (each element is less than or equal to the next). Print True or False. Target output: True
n is prime and print True or False. Handle values below 2 as not prime. Target output: False
Frequently Asked Questions
🔗 Continue studying
The Superpack includes an editable Topic 3.18 slide deck that animates the halting-problem contradiction, a decidable-versus-undecidable sorting bank, a runnable decider worksheet, and an end-of-unit quiz for Big Idea 3. View what's included →
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]