Topic 3.18: Undecidable Problems | AP CSP Big Idea 3 | APCSExamPrep.com

AP CSP Course Big Idea 3 3.18 Undecidable Problems
3.18
Big Idea 3 • Algorithms & Programming

Undecidable Problems

🕐 ~35 min FREE 📖 6 MCQ questions 🎮 Decidable or Not game 💻 Live Python editor AAP-4.B

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
📈 Exam weight: Undecidability closes Big Idea 3 and shows up as concept and vocabulary multiple-choice items. The examiners love the trap of confusing undecidable with slow, and expect you to name the halting problem as the standard example.
💡 Think about this first

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.

🎯 What the exam expects

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.

📈 The mental test

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.

✍ Mini Exercise 1 • Predict the output
This decides "is 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.

⚠ The classic distractor

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.

✍ Mini Exercise 2 • Pick the undecidable one
Which problem is undecidable (no algorithm can answer it correctly for every possible input)?

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.

✍ Mini Exercise 3 • Fill in the blank
Type exactly what this prints (True or False, capitalized).
t = [2, 4, 6]
x = 5
print(x in t)
prints:

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
📋 Create Performance Task • Your algorithm is a decidable computation

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:

PROCEDURE inList(data, target) { FOR EACH v IN data { IF (v = target) { RETURN (true) } } RETURN (false) }

In your written response you can describe this procedure precisely:

  • "This procedure answers a yes-or-no decision problem: it returns true when target is in data and false otherwise."
  • "Because data is 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 →

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Trace
Predict first: does the loop find any divisor of 9 before it ends?
This decider answers "is n prime?" What does it display?
def d(n): if n < 2: return False k = 2 while k < n: if n % k == 0: return False k = k + 1 return True print(d(9))
Incorrect. When k reaches 3, 9 % 3 is 0, so the decider returns False. It only returns True when no divisor is found.
Correct. The loop tries k = 2 (9 % 2 is 1), then k = 3 (9 % 3 is 0), so it returns False. 9 is not prime.
Incorrect. The function always reaches a return statement and its result is printed, so something is displayed.
Incorrect. The loop is bounded by k < n and always terminates, so there is no error.
Question 2 of 6NOT question
Each option describes a decision problem. Which one is NOT decidable?
Incorrect. A single comparison always answers this correctly for any integer, so it is decidable.
Correct. This is the halting problem. No algorithm can answer it correctly for every program and input, so it is undecidable.
Incorrect. Scanning a fixed, finite dictionary always terminates with a correct answer, so it is decidable.
Incorrect. Checking n % 2 always gives a correct answer for any integer, so it is decidable.
Question 3 of 6I, II, III
Which of the following decision problems are decidable (an algorithm can give a correct yes-or-no answer for every input)?
  • 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.
Incorrect. I is decidable, but III is also decidable: scanning a finite list always terminates with a correct answer.
Correct. I and III each have an algorithm that is correct for every input and always halts. II is the halting problem, which is undecidable.
Incorrect. II is the halting problem, which is undecidable, so any set that includes II is wrong.
Incorrect. II, the halting problem, is undecidable, so not all three are decidable.
Question 4 of 6Spot the bug
This procedure is meant to return True when x appears anywhere in list t, and False otherwise. What is the bug?
def present(t, x): for v in t: if v == x: return True else: return False
Incorrect. == is the correct comparison operator; = would be assignment and cause an error.
Correct. On the first element, if it does not match, the else runs and returns False immediately, so elements after the first are never examined. The False return belongs after the loop.
Incorrect. A for loop iterates a list correctly. The flaw is the premature return, not the loop type.
Incorrect. The return True only runs inside the if v == x branch, so the comparison does happen first.
Question 5 of 6Spot the bug
A student writes: "The halting problem is undecidable because checking every possible program would take an unreasonable amount of time." What is wrong with this reasoning?
Incorrect. Taking a long time describes an inefficient but still decidable problem. Undecidability is a stronger, different claim.
Correct. Undecidable means no correct general algorithm can exist at all. A problem that is merely slow still has a correct algorithm and is decidable. The student confused slowness with impossibility.
Incorrect. The halting problem is undecidable in general; it is not decidable for arbitrary programs of any length.
Incorrect. The error is conceptual (slow versus impossible), not a mix-up with a specific searching algorithm.
Question 6 of 6Definition
A decision problem is decidable only if an algorithm can be written that ALWAYS produces the correct yes-or-no answer. For which inputs must that guarantee hold?
Incorrect. Being correct on one input is far too weak. Decidability requires correctness on all inputs.
Correct. Decidable means the algorithm is correct for every possible input and always halts. Both the coverage (every input) and termination are required.
Incorrect. "Most inputs" and "reasonable time" describe usefulness or efficiency, not decidability, which demands every input.
Incorrect. Speed and hardware are irrelevant to decidability. What matters is that a correct, always-halting algorithm exists for every input.
🎮 Lesson Game
Decidable or Not
Trace each runnable decider and predict the True or False it displays. 8 rounds.
0
Correct
1/8
Round
0
Streak 🔥
🐛 Python • what does the decider display?
0/8
correct answers
💻 Live Python Code Editor
Practice Problems
Real Python runs right here in your browser. The first time you press Run, the Python engine loads (a few seconds); after that it is instant. Problems build from guided to Create-Task level. Use Hint if you are stuck, and check your output against the target.
Hints used: 0 • Solutions viewed: 0
Level 1 • Guided
Problem 1 of 8 • is n even
Write a decider for a yes-or-no problem. Print whether n is even. Target output: True
Level 2 • Membership
Problem 2 of 8 • is x in the list
Decide whether x appears in list t, and print True or False. Target output: True
Level 3 • Decider function
Problem 3 of 8 • return then print
Define a function is_even(n) that returns True or False, then print is_even(15). Target output: False
Level 4 • Fix the bug
Problem 4 of 8 • premature return
This decider should return True when x is anywhere in t, but it returns too early. Fix it so it checks the whole list. Target output: True
Level 5 • Primality
Problem 5 of 8 • a decider that always halts
Decide whether 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
Level 6 • Create Task style
Problem 6 of 8 • combined decider
Decide eligibility and print the answer:
  • eligible when age is at least 18 and has_id is True
  • otherwise not eligible
Target output: True
Level 7 • Challenge
Problem 7 of 8 • you write the whole decider
Open ended. Write the entire program yourself. Decide whether list t is sorted in non-decreasing order (each element is less than or equal to the next). Print True or False. Target output: True
Level 8 • Challenge
Problem 8 of 8 • full primality decider
Open ended. Write the entire program yourself. Decide whether n is prime and print True or False. Handle values below 2 as not prime. Target output: False

Frequently Asked Questions

A decision problem is any problem that can be phrased as a yes-or-no question about its input, such as "is this number prime?" A program that answers it returns or prints True or False and is called a decider.
A decision problem is decidable if an algorithm can be written that gives a correct yes-or-no answer for every possible input and always terminates. The words every and always are essential to the definition.
Undecidable means no algorithm can be built that always gives a correct answer for every possible input. The classic example is the halting problem: deciding whether an arbitrary program will eventually stop or run forever. Alan Turing proved it is undecidable.
No, and this is the most common trap. A slow problem still has a correct algorithm and is decidable; you might just wait a very long time. An undecidable problem has no correct general algorithm at all, no matter how much time or how fast the computer.
Yes, you can often answer specific cases. But if no single algorithm is guaranteed correct for every possible input, the problem is still undecidable. Being solvable for some inputs is not the same as being decidable.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]