Checking semiprime numbers - JavaScript



We are required to write a JavaScript function that takes in a number and the function establishes if the provided number is a semiprime or not.

Semiprime

A semiprime number is that number which is a special type of composite number that is a product of two prime numbers. For example: 6, 15, 10, 77 are all semiprime. The square of a prime number is also semiprime, like 4, 9, 25 etc.

Example

Following is the code to check semi-prime numbers −

const num = 141; const checkSemiprime = num => {    let cnt = 0;    for (let i = 2; cnt < 2 && i * i <= num; ++i){       while (num % i == 0){          num /= i, ++cnt;       }    }    if (num > 1){       ++cnt;    }    // Return '1' if count is equal to '2' else    // return '0'    return cnt === 2; } console.log(checkSemiprime(num));

Output

Following is the output in the console −

true
Updated on: 2020-09-18T09:11:12+05:30

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements