|
| 1 | +/* |
| 2 | + * Composite number: https://en.wikipedia.org/wiki/Composite_number |
| 3 | + * function isCompositeNumber |
| 4 | + * Check if a given number is a composite number or not? |
| 5 | + * isCompositeNumber(6) // returns true |
| 6 | + * isCompositeNumber(577) // returns false |
| 7 | + * isCompositeNumber(2024) // returns true |
| 8 | + * A composite number is a positive integer that is not prime. In other words, it has a positive divisor other than one or itself. |
| 9 | + * First few composite numbers are 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, ......... |
| 10 | + * Every integer greater than one is either a prime number or a composite number. |
| 11 | + * The number one is a unit – it is neither prime nor composite. |
| 12 | + */ |
| 13 | + |
| 14 | +function isCompositeNumber (number) { |
| 15 | + let i = 1 |
| 16 | + let count = 0 |
| 17 | + while (number >= i) { |
| 18 | + if (number % i === 0) { |
| 19 | + count++ |
| 20 | + } |
| 21 | + i++ |
| 22 | + } |
| 23 | + if (count > 2) { |
| 24 | + return true |
| 25 | + } else { |
| 26 | + return false |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +export { isCompositeNumber } |
0 commit comments