- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
To Continue Learning Please Login
Login with Google