Bird
0
0
DSA Cprogramming~10 mins

Check if Number is Prime in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Check if Number is Prime
Start with number n
Check if n <= 1?
YesNot Prime
No
Set divisor = 2
Is divisor * divisor <= n?
NoPrime
Yes
Check if n % divisor == 0?
YesNot Prime
No
Increment divisor by 1
Back to divisor * divisor <= n check
Start by checking if the number is less than or equal to 1 (not prime). Then test divisors from 2 up to the square root of the number. If any divisor divides the number evenly, it is not prime; otherwise, it is prime.
Execution Sample
DSA C
int isPrime(int n) {
  if (n <= 1) return 0;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return 0;
  }
  return 1;
}
This function checks if n is prime by testing divisors from 2 up to sqrt(n). Returns 1 if prime, 0 otherwise.
Execution Table
StepOperationDivisor (i)Condition (i*i <= n)Check n % i == 0ResultReason
1Check if n <= 1---Non=11 > 1, continue
2Set divisor i = 222*2=4 <= 11--Start loop
3Check if 11 % 2 == 024 <= 1111 % 2 = 1No2 does not divide 11
4Increment divisor i = 333*3=9 <= 11--Continue loop
5Check if 11 % 3 == 039 <= 1111 % 3 = 2No3 does not divide 11
6Increment divisor i = 444*4=16 <= 11--Condition false, exit loop
7Return 1 (Prime)---YesNo divisors found, 11 is prime
💡 Loop ends when divisor squared exceeds n; no divisor evenly divides n, so n is prime.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
n1111111111
i (divisor)-2344
Condition i*i <= n-TrueTrueFalseFalse
Result (prime?)----1 (True)
Key Moments - 3 Insights
Why do we check i * i <= n instead of i <= n?
Because if n has a divisor larger than its square root, it must have a smaller one too. Checking up to sqrt(n) is enough and saves time, as shown in steps 2, 4, and 6.
Why return 0 immediately when n % i == 0?
Because finding any divisor means n is not prime. The function stops early to save work, as seen in step 3 and 5 where checks happen.
What happens if n is less than or equal to 1?
The function returns 0 immediately since numbers <= 1 are not prime, as shown in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the divisor i when the loop condition becomes false?
A4
B3
C2
D5
💡 Hint
Check the 'Condition (i*i <= n)' column in step 6 where it becomes false.
At which step does the function confirm that 11 is prime?
AStep 3
BStep 6
CStep 7
DStep 1
💡 Hint
Look for the step where the function returns 1 indicating prime.
If n was 9 instead of 11, at which step would the function return 0?
AStep 3
BStep 5
CStep 7
DStep 2
💡 Hint
Check when n % i == 0 is true for i=3 (since 9 % 3 == 0).
Concept Snapshot
Check if number n is prime:
- If n <= 1, return not prime
- Test divisors i from 2 to sqrt(n)
- If any i divides n evenly, return not prime
- Else, return prime
- Stops early on first divisor found
Full Transcript
This concept checks if a number is prime by testing divisors from 2 up to the square root of the number. It first excludes numbers less than or equal to 1. Then it loops through possible divisors, checking if any divide the number evenly. If yes, it returns not prime immediately. If no divisors are found, it returns prime. The loop condition uses i*i <= n to optimize checks. The execution table traces these steps for n=11, showing divisor values, conditions, and results at each step.