Question Bank

Prime and Sieve Quiz

Difficulty: Medium

Code-first drills on primality testing, the Sieve of Eratosthenes, and smallest-prime-factor tables. Includes complexity reasoning and one prime-factorization walk.

Question Bank
/

Prime and Sieve Quiz

Prime and Sieve Quiz

Code-first drills on primality testing, the Sieve of Eratosthenes, and smallest-prime-factor tables. Includes complexity reasoning and one prime-factorization walk.

Question Bank
Medium
JavaScript
4 questions
sieve-of-eratosthenes
prime-factorization
number-theory
interview-prep

729 views

8

Implement a trial-division primality test that runs in O(sqrt(n)). Explain the i * i <= n loop bound.

Examples

Example 1:

Input: n = 97
Output: true
Explanation: If n = a * b with a <= b, then a <= sqrt(n). So the smallest non-trivial divisor (if any) lies at or below sqrt(n). Loop while i * i <= n. 97 has no divisor up to 9, so it is prime.

Example 2:

Input: n = 100
Output: false
Explanation: 100 = 2 * 50, caught immediately by the parity check. The function returns false without entering the odd-divisor loop.