DeepSeek’s Chain of Thought: Debugging a Python Prime Number Function


DeepSeek’s Chain of Thought: Debugging a Python Prime Number Function

Introduction: Why Problem-Solving Approaches Matter 

One of the most fascinating things about AI models isn’t just that they can answer questions—it’s how they arrive at their solutions. While many AI models, including ChatGPT, tend to provide polished answers, DeepSeek’s Chain of Thought (CoT) approach allows us to peek into its reasoning process. This makes it feel more like a real-time problem solver rather than a magic black box.

Today, we’ll examine a Python debugging challenge where DeepSeek walks through its problem-solving process step by step, correcting a broken function that’s supposed to return prime numbers. 

The Problem: Debugging a Prime Number Function 

A developer wrote the following Python function, intending for it to return the first n prime numbers. However, when they call get_primes(5), expecting [2, 3, 5, 7, 11], something goes wrong.

Python
def get_primes(n):
    primes = []
    num = 2
    while len(primes) < n:
        for i in range(2, num):
            if num % i == 0:
                break
        primes.append(num)  # This is likely where the bug is!
        num += 1
    return primes

print(get_primes(5))  # Expected output: [2, 3, 5, 7, 11]

The output, however, includes non-prime numbers. What’s wrong, and how do we fix it?

DeepSeek’s Chain of Thought (Full Debugging Session)

Below is the AI’s full thought process:

Okay, let's take a look at this Python function. It's supposed to return 
the first n prime numbers, but it's not working correctly. The user says 
that when they print get_primes(5), they expect [2, 3, 5, 7, 11], but 
maybe the current code isn't doing that. Let me try to figure out what's 
wrong.

First, let me read through the code step by step. The function get_primes 
starts with an empty list called primes. The variable num is initialized 
to 2. Then there's a while loop that runs as long as the length of primes 
is less than n. Inside that loop, there's a for loop that goes from 2 to 
num (but not including num). For each i in that range, it checks if num 
is divisible by i. If it is, it breaks out of the loop. Then, regardless 
of whether it broke or not, it appends num to primes and increments num by 1.

Wait a minute, that's the problem! The code appends num to primes even 
if it found a divisor. Because after checking all possible divisors 
(from 2 to num-1), if any of them divides num, it breaks. But even if 
it breaks (meaning num is not a prime), the code still appends it to 
primes. That's definitely wrong. The append should only happen if the 
for loop completes without finding any divisors.

For example, take num=4. The for loop runs i=2. 4%2 ==0, so it breaks. 
Then primes.append(4), which is incorrect. So primes would collect 
non-primes. Hence, the output would include composite numbers like 
4, 6, etc., which is not right.

So the main issue is that the code appends num to primes in every iteration 
of the while loop, without checking whether it actually passed the 
prime test. The fix here would be to only append num if no divisors 
were found.

How to do that? Well, in Python, a for loop can have an else clause 
that executes when the loop completes normally (i.e., not broken by 
a break statement). So, in the current code, the for loop checks 
if num is divisible by any i. If it finds a divisor, breaks. If it 
goes through all i's and doesn't break, then the else block is executed. 
So, the code should append num to primes only in the else case.

Looking at the current code, after the for loop, primes.append is called 
unconditionally. That's the mistake. Instead, primes.append should be 
inside the else clause of the for loop.

So the corrected code would move the append into the else block. 
Let me adjust the code.

 DeepSeek’s Corrected Code

Python
def get_primes(n):
    primes = []
    num = 2
    while len(primes) < n:
        for i in range(2, num):
            if num % i == 0:
                break
        else:  # This else executes only if the loop was *not* broken
            primes.append(num)
        num += 1
    return primes

print(get_primes(5))  # Output: [2, 3, 5, 7, 11]

Analyzing DeepSeek’s Thought Process

DeepSeek’s approach is remarkably structured and human-like in a way that makes it easy to follow. Here’s what stands out:

1. It reads the code like a human would.
  • It first walks through each line, describing what it does.
  • It narrates its understanding rather than jumping to conclusions.
2. It finds the bug using a specific test case.
  • It picks num=4 as an example and explains why it incorrectly gets added.
  • It generalizes this to show how non-prime numbers are mistakenly appended.
3. It introduces an important Python concept: the for-else construct.
  • Many developers (especially beginners) don’t know about for-else.
  • DeepSeek uses it naturally, explaining why it works in this case.
4. It thinks about efficiency but doesn’t get distracted.
  • It brings up the idea that checking up to sqrt(n) would be more efficient.
  • But it wisely decides to focus on correctness first, since efficiency isn't the main issue.
5. It verifies its fix step by step.
  • After making the correction, it mentally steps through the execution.
  • This kind of validation process is crucial in real-world debugging.

Comparing DeepSeek and ChatGPT CoT 

DeepSeek’s reasoning process is far more transparent than ChatGPT’s typical CoT approach. Here’s why:

FeatureDeepSeek’s CoTChatGPT’s CoT
Thinking Out LoudYes—fully narrates thoughtsLimited—tends to summarize
Exploration of AlternativesYes—considers and rejects ideasNo—usually presents a final answer
Step-by-Step DebuggingYes—tests its own fixRare—fix is often assumed correct
Teaching ApproachHigh—explains logic deeplyMedium—gives direct solutions
Self-CorrectionYes—second-guesses and refinesNo—typically presents only final thoughts

DeepSeek’s approach feels like watching an experienced developer debug rather than just seeing a final answer. This makes it incredibly valuable for learning.

Final Thoughts: Why This Matters for Developers 

This level of transparent reasoning makes DeepSeek an amazing tool for:

  • Learning debugging strategies instead of just getting answers.
  • Seeing alternative solutions and understanding why they work.
  • Practicing structured thinking, which is key for coding interviews and real-world problem-solving.

Need ChatGPT or DeepSeek Expertise?

If you're looking for guidance on ChatGPT or DeepSeek challenges or want to collaborate, feel free to reach out! We'd love to help you tackle your projects. 🚀

Email us at: info@pacificw.com


Image: Gemini

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process