function isPrime(num) { // returns boolean if (num <= 1) return false; // negatives if (num % 2 == 0 && num > 2) return false; // even numbers const s = Math.sqrt(num); // store the square to loop faster for(let i = 3; i <= s; i += 2) { // start from 3, stop at the square, increment in twos if(num % i === 0) return false; // modulo shows a divisor was found } return true; } console.log(isPrime(121));
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter