Handle Base Cases: The function first checks if the number is less than or equal to 1. Prime numbers are defined as natural numbers greater than 1, so these cases immediately return false.
Iterate and Check Divisibility: A for loop iterates from i = 2 up to the square root of the number.
- The loop starts from 2 because 1 is not a prime factor to check, and any number is divisible by 1.
- The loop goes up to Math.sqrt(number) for optimization. If a number n has a divisor d > sqrt(n), then it must also have a divisor n/d < sqrt(n). Therefore, checking up to the square root is sufficient.
Return false if divisible: Inside the loop, number % i === 0 checks if number is perfectly divisible by i. If it is, number has a factor other than 1 and itself, so it is not prime, and the function returns false.
Return true if no divisors found: If the loop completes without finding any divisors, it means the number is only divisible by 1 and itself, making it a prime number. The function then returns true.
Find Live Demo
π» Example: HTML and JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prime NUMBER Checker</title>
<style>
body {font-family: Arial, sans-serif;
display: flex;
justify-content: center;align-items: center;min-height: 100vh;background-color: #f4f4f4;margin: 0;}.container {
background-color: #fff;padding: 30px;
border-radius: 8px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;}INPUT[TYPE="number"] {
padding: 10px;
margin-bottom: 15px;border: 1px solid #ddd;
border-radius: 4px;width: 200px;
font-SIZE: 16px;
}button {padding: 10px 20px;
background-color: #007bff;color: white;
border: NONE;border-radius: 4px;cursor: pointer;
font-SIZE: 16px;
}button:hover {background-color: #0056b3;}#result {margin-top: 20px;font-SIZE: 18px;
font-weight: bold;}</style>
</head>
<body>
<div class="container">
<h1>Prime NUMBER Checker</h1>
<INPUT TYPE="number" id="numberInput" placeholder="Enter a number">
<button onclick="checkPrimeNumber()">Check</button>
<p id="result"></p>
</div>
<script>
FUNCTION isPrime(num) {
IF (num <= 1) {
RETURN FALSE; // Numbers less than OR equal TO 1 are NOT prime
}FOR (let i = 2; i <= Math.sqrt(num); i++) {
IF (num % i === 0) {
RETURN FALSE; // IF num IS divisible BY any NUMBER other than 1 AND itself
}}RETURN TRUE; // IF no divisors were found, num IS a prime NUMBER
}FUNCTION checkPrimeNumber() {
const numberInput = document.getElementById('numberInput');
const resultParagraph = document.getElementById('result');
const num = parseInt(numberInput.value);
IF (isNaN(num)) {
resultParagraph.textContent = "Please enter a valid number.";
resultParagraph.style.color = "red";
RETURN;}IF (isPrime(num)) {
resultParagraph.textContent = `${num} is a prime number.`;
resultParagraph.style.color = "green";
} ELSE {
resultParagraph.textContent = `${num} is not a prime number.`;
resultParagraph.style.color = "red";
}}</script>
</body>
</html>
Further post that would you like to learn in Salesforce
How to check if an input is a prime number?
A prime number is not divisible by any other number except itself and 1. Hence, one possible way to check if n is prime is to ensure that it is not divisible by any number from 2 up to n / 2 (if n / 2 is a decimal number, then round it down to the nearest integer).
How to check whether a number is prime or not?
To check if a number is prime, determine if it is a natural number greater than 1 and if its only divisors are 1 and itself. A common method is trial division: check divisibility by integers from 2 up to the square root of the number; if it's divisible by any of these, it's not prime. If it's not divisible by any number in that range, it is prime.
Is there a trick to find prime numbers?
To determine if a number is prime, use trial division by dividing it by prime numbers starting from 2 up to its square root. First, quickly check for obvious factors: if it's an even number (except 2), it's not prime; if the sum of its digits is divisible by 3, it's not prime. If the number survives these quick checks, then proceed to test divisibility by other primes. If it's not divisible by any prime number up to its square root, then the number is prime.
Related Topics | You May Also Like
|
π Get Free Course β
π Salesforce Administrators π Salesforce Lightning Flow Builder π Salesforce Record Trigger Flow Builder |
π Get Free Course β |







