0
0
DSA Pythonprogramming~20 mins

Check if Number is Prime in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prime Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of prime check for number 29
What is the output of this code when checking if 29 is prime?
DSA Python
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(29))
ATrue
BFalse
CNone
DError
Attempts:
2 left
💡 Hint
Check divisors from 2 up to square root of the number.
Predict Output
intermediate
2:00remaining
Output of prime check for number 1
What is the output of this code when checking if 1 is prime?
DSA Python
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(1))
ATrue
BFalse
CNone
DError
Attempts:
2 left
💡 Hint
Numbers less than or equal to 1 are not prime.
🔧 Debug
advanced
2:00remaining
Identify the error in prime check code
What error does this code produce when checking if 10 is prime?
DSA Python
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(10))
ANo error, outputs False
BTypeError
CNameError
DSyntaxError
Attempts:
2 left
💡 Hint
Check the line with the if statement for missing punctuation.
Predict Output
advanced
2:00remaining
Output of prime check for number 25
What is the output of this code when checking if 25 is prime?
DSA Python
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(25))
AFalse
BTrue
CNone
DError
Attempts:
2 left
💡 Hint
Check if 25 has divisors other than 1 and itself.
🧠 Conceptual
expert
2:00remaining
Why check divisors only up to square root?
Why does the prime check function only test divisors up to the square root of the number?
ABecause the square root is the largest possible divisor
BBecause numbers larger than the square root are always prime
CBecause any factor larger than the square root pairs with a smaller factor already checked
DBecause checking beyond the square root causes runtime errors
Attempts:
2 left
💡 Hint
Think about factor pairs and multiplication.