JS Learning

Check Prime Number

medium

Topic Overview

Take your time to understand each concept. Practice with the examples below!

Prime numbers are the building blocks of mathematics! Let's create an efficient algorithm to detect them.

What is a Prime Number? A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Your Task:

Write a function that checks if a given number is prime. Return true if it is prime, false otherwise.

Examples:

  • 2 → true (smallest prime)
  • 7 → true (only divisible by 1 and 7)
  • 10 → false (divisible by 1, 2, 5, 10)
  • 29 → true
  • 1 → false (not considered prime)

Algorithm Tips:

  • Start by handling edge cases (n < 2)
  • You only need to check up to √n - why?
  • Even numbers (except 2) can't be prime
  • Skip even divisors for efficiency

Real-World Applications:

Prime numbers are crucial in cryptography, random number generation, and hash table implementations!

Ready to practice? Try the exercise below to reinforce your learning!

JAVASCRIPT Editor
Loading...