Discover the simple trick that turns a slow number check into a lightning-fast prime test!
Why Check if Number is Prime in DSA Typescript?
Imagine you want to find out if a number is prime by checking every smaller number one by one, like counting all the way from 1 to that number and seeing if it divides evenly.
This manual way is very slow and tiring, especially for big numbers. It takes a lot of time and you can easily make mistakes by missing some numbers or checking too many unnecessary ones.
Using a smart method to check if a number is prime, we only test divisors up to the square root of the number. This saves time and avoids extra work, making the check fast and reliable.
function isPrime(number: number): boolean {
for (let i = 2; i < number; i++) {
if (number % i === 0) return false;
}
return number > 1;
}function isPrime(number: number): boolean {
if (number <= 1) return false;
for (let i = 2; i * i <= number; i++) {
if (number % i === 0) return false;
}
return true;
}This method lets us quickly and correctly find out if numbers are prime, even when they are very large.
Prime numbers are used in secret codes for online banking and secure messages, so checking primes fast helps keep information safe.
Manual checking every number is slow and error-prone.
Checking up to the square root is faster and smarter.
Fast prime checks help in security and math problems.
Practice
prime?Solution
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.Step 2: Compare options with the definition
Only It has exactly two distinct positive divisors: 1 and itself. correctly states this definition.Final Answer:
It has exactly two distinct positive divisors: 1 and itself. -> Option DQuick Check:
Prime number = two divisors [OK]
- Thinking even numbers are always prime
- Confusing prime with odd numbers
- Believing prime numbers have multiple divisors
Solution
Step 1: Identify correct parameter and return types
The function should take a number and return a boolean indicating prime or not.Step 2: Match options with correct signature
function isPrime(n: number): boolean matches: input number, output boolean.Final Answer:
function isPrime(n: number): boolean -> Option CQuick Check:
Input number, output boolean [OK]
- Using wrong input types like string or boolean
- Returning wrong types like number or void
- Missing function parameters
function isPrime(n: number): boolean {
if (n <= 1) return false;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) return false;
}
return true;
}
console.log(isPrime(11));Solution
Step 1: Check input and initial condition
Input is 11, which is greater than 1, so continue checking.Step 2: Loop from 2 to sqrt(11) ≈ 3.3
Check divisibility by 2 and 3. 11 % 2 != 0 and 11 % 3 != 0, so no divisor found.Final Answer:
true -> Option AQuick Check:
11 is prime = true [OK]
- Checking divisors beyond sqrt(n)
- Returning true too early
- Not handling n <= 1 correctly
function isPrime(n: number): boolean {
if (n < 2) return false;
for (let i = 2; i < n / 2; i++) {
if (n % i === 0) return false;
}
return true;
}Solution
Step 1: Identify the logic error in loop condition
The loop uses i < n / 2. For n=4, n/2=2, i=2 < 2 false; loop skips check for 2, returns true (but 4 not prime).Step 2: Correct the loop bound
For efficiency and correctness, the loop should run until i <= Math.sqrt(n) instead of n/2.Final Answer:
Loop should run until i <= Math.sqrt(n) for efficiency -> Option AQuick Check:
Loop bound to sqrt(n) is optimal and correct [OK]
- Using i < n/2 instead of i <= n/2 in loop
- Wrong return type
- Incorrect boundary condition for n
function isPrime(n: number): boolean {
if (n <= 1) return false;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) return false;
}
return true;
}Which of the following arrays correctly lists these primes?
Solution
Step 1: Identify prime numbers between 10 and 20
Check each number: 11 (prime), 12 (not), 13 (prime), 14 (not), 15 (not), 16 (not), 17 (prime), 18 (not), 19 (prime), 20 (not).Step 2: List only primes in the array
The primes are 11, 13, 17, and 19.Final Answer:
[11, 13, 17, 19] -> Option BQuick Check:
Primes 10-20 = [11,13,17,19] [OK]
- Including non-prime even numbers
- Including numbers less than 10 or greater than 20
- Confusing primes with odd numbers
Ready to prove it?
Pass the test and this pattern moves unknown
