Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Check if Number is Prime in DSA Python - Execution Trace

Choose your learning style10 modes available
Concept Flow - Check if Number is Prime
Start with number n
↓
Check if n <= 1?
Yes→Not prime, stop
No↓
Set divisor i = 2
↓
Check if i * i <= n?
No→Prime, stop
Yes↓
Check if n % i == 0?
Yes→Not prime, stop
No↓
Increment i by 1
↩Back to check i * i <= n
Start from 2 and check divisors up to square root of n; if any divides n, it's not prime.
Execution Sample
DSA Python
def is_prime(n):
    if n <= 1:
        return False
    i = 2
    while i * i <= n:
        if n % i == 0:
            return False
        i += 1
    return True
This code checks if a number n is prime by testing divisors from 2 up to sqrt(n).
Execution Table
StepOperationiCondition i*i <= nCheck n % i == 0ResultNotes
1Check if n <= 1-n=17 > 1--Continue to divisor check
2Set i = 222*2=4 <= 17 True--Start divisor loop
3Check if 17 % 2 == 024 <= 17 True17 % 2 = 1 != 0No divisorIncrement i
4Increment i33*3=9 <= 17 True--Next divisor
5Check if 17 % 3 == 039 <= 17 True17 % 3 = 2 != 0No divisorIncrement i
6Increment i44*4=16 <= 17 True--Next divisor
7Check if 17 % 4 == 0416 <= 17 True17 % 4 = 1 != 0No divisorIncrement i
8Increment i55*5=25 <= 17 False--Exit loop, prime confirmed
9Return True---TrueNumber 17 is prime
💡 Loop ends when i*i > n; no divisor found, so number is prime.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
n171717171717
i-23455
Result-----True
Key Moments - 3 Insights
Why do we check i * i <= n instead of i <= n?
Because if n has a divisor larger than sqrt(n), it must have a smaller one too. Checking up to sqrt(n) is enough and efficient (see execution_table rows 3,5,7,8).
Why return False immediately when n % i == 0?
Because finding any divisor means n is not prime, so no need to check further (see execution_table row 3).
What happens if n is less than or equal to 1?
The function returns False immediately since numbers <= 1 are not prime (see execution_table row 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i when the loop condition i*i <= n becomes false?
A4
B5
C6
D3
💡 Hint
Check the 'Condition i*i <= n' column in rows 7 and 8.
At which step does the function confirm that 17 is prime?
AStep 8
BStep 3
CStep 9
DStep 1
💡 Hint
Look at the 'Result' and 'Notes' columns for when True is returned.
If n was 15 instead of 17, at which step would the function return False?
AStep 5
BStep 3
CStep 7
DStep 9
💡 Hint
Check when n % i == 0 is True for n=15; 15 % 3 == 0.
Concept Snapshot
Check if number n is prime:
- If n <= 1, return False
- For i from 2 to sqrt(n):
  - If n % i == 0, return False
- If no divisor found, return True
Use i*i <= n for efficiency.
Full Transcript
This visualization shows how to check if a number is prime by testing divisors from 2 up to the square root of the number. We start by checking if the number is less than or equal to 1, which is not prime. Then we set a divisor i to 2 and check if i squared is less than or equal to the number. If yes, we check if the number is divisible by i. If divisible, the number is not prime and we stop. If not, we increment i and repeat. If no divisors are found by the time i squared exceeds the number, the number is prime. The execution table traces these steps with the example number 17, showing the divisor checks and the final result.

Practice

(1/5)
1. What does it mean for a number to be prime?
easy
A. It has exactly two distinct positive divisors: 1 and itself.
B. It is divisible by 2 only.
C. It is an even number greater than 2.
D. It has more than two divisors.

Solution

  1. Step 1: Understand the definition of prime number

    A prime number is a number greater than 1 that has no divisors other than 1 and itself.
  2. Step 2: Compare options with definition

    Only It has exactly two distinct positive divisors: 1 and itself. correctly states this definition.
  3. Final Answer:

    It has exactly two distinct positive divisors: 1 and itself. -> Option A
  4. Quick Check:

    Prime number = two divisors [OK]
Hint: Prime means only divisible by 1 and itself [OK]
Common Mistakes:
  • Thinking even numbers are always prime
  • Confusing prime with odd numbers
  • Believing prime numbers have multiple divisors
2. Which of the following Python code snippets correctly checks if a number n is prime?
easy
A. for i in range(2, n): if n % i != 0: return False; return True
B. for i in range(1, n): if n % i == 0: return False; return True
C. for i in range(2, n): if n % i == 0: return False; return True
D. for i in range(2, n): if n % i == 1: return False; return True

Solution

  1. Step 1: Analyze the loop range and condition

    for i in range(2, n): if n % i == 0: return False; return True checks divisibility from 2 to n-1, which is correct for prime checking.
  2. Step 2: Check the condition for returning False

    If any divisor divides n evenly (remainder 0), return False; else True after loop ends.
  3. Final Answer:

    for i in range(2, n): if n % i == 0: return False; return True -> Option C
  4. Quick Check:

    Check divisors from 2 to n-1 for remainder 0 [OK]
Hint: Check divisors from 2 to n-1 for zero remainder [OK]
Common Mistakes:
  • Starting loop from 1 instead of 2
  • Checking for remainder not equal to zero
  • Checking if remainder equals 1 instead of 0
3. What is the output of the following code when n = 11?
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

print(is_prime(11))
medium
A. True
B. False
C. None
D. Error

Solution

  1. Step 1: Check if n is less than or equal to 1

    Since 11 > 1, skip the first return False.
  2. Step 2: Loop from 2 to sqrt(11) + 1 (which is 4)

    Check divisibility by 2 and 3; 11 % 2 != 0 and 11 % 3 != 0, so loop ends without returning False.
  3. Step 3: Return True after loop

    No divisor found, so function returns True.
  4. Final Answer:

    True -> Option A
  5. Quick Check:

    11 is prime, so output is True [OK]
Hint: Check divisors up to sqrt(n) for efficiency [OK]
Common Mistakes:
  • Looping up to n instead of sqrt(n)
  • Returning False too early
  • Not handling n <= 1 case
4. Find the error in this prime checking function and select the correct fix:
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
        else:
            return True
medium
A. Change range to start from 1 instead of 2
B. Change if condition to n % i != 0
C. Add return True inside the else block
D. Remove the else block inside the loop to return True after loop ends

Solution

  1. Step 1: Identify the problem with else inside the loop

    The else causes the function to return True on first non-divisor, which is incorrect.
  2. Step 2: Fix by removing else and returning True after loop

    Return True only after checking all divisors without finding any.
  3. Final Answer:

    Remove the else block inside the loop to return True after loop ends -> Option D
  4. Quick Check:

    Return True only after full divisor check [OK]
Hint: Return True after loop, not inside it [OK]
Common Mistakes:
  • Returning True too early inside loop
  • Starting loop from 1
  • Changing condition to wrong remainder check
5. You want to check if a large number n is prime efficiently. Which approach is best?
hard
A. Check divisibility from 2 to n-1
B. Check divisibility from 2 to sqrt(n)
C. Check divisibility only by 2 and 3
D. Check divisibility from 1 to n

Solution

  1. Step 1: Understand the efficiency of divisor checking

    Checking up to n-1 or n is slow for large n; checking only 2 and 3 misses other divisors.
  2. Step 2: Use the mathematical fact that divisors come in pairs

    If n has a divisor greater than sqrt(n), it must have a corresponding divisor less than sqrt(n).
  3. Step 3: Check divisibility only up to sqrt(n)

    This reduces the number of checks drastically and is the best approach.
  4. Final Answer:

    Check divisibility from 2 to sqrt(n) -> Option B
  5. Quick Check:

    Divisors up to sqrt(n) suffice for prime check [OK]
Hint: Check divisors only up to square root of n [OK]
Common Mistakes:
  • Checking all numbers up to n
  • Checking only 2 and 3
  • Starting divisor check from 1
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard