Find Factors of a Number – JavaScript Program.

ðŸ’¡ What are the Factors of any number?

Factors of a number (n) will be those numbers that exactly divide it and give a remainder as 0.

This means that the two whole numbers whose product is given number (n) are the factors of that number.

ðŸ’¡ What are the Prime numbers?

Prime numbers are natural numbers that are divisible by only 1 and the number itself.

In other words, prime numbers are positive integers greater than 1 with exactly two factors, 1 and the number itself.

The first ten primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.

2 is the smallest prime number and is the only even prime number.

ðŸ’¡What are Composite Numbers?

In Mathematics, composite numbers are numbers that have more than two factors. These numbers are also called composites.

Composite numbers are just the opposite of prime numbers.

All the natural numbers which are not prime numbers are composite numbers as they can be divided by more than two numbers.

For example, 6 is a composite number because it is divisible by 1, 2, 3, and even by 6.

The smallest composite number is 4.

1 only has one factor which is 1, 2 has two factors that is 1 & 2, and 3 also has two factors that is 1 & 3.

4 have three factors that are 1, 2, and 4. So 4 is the smallest composite number.

NOTE: Always remember that 1 is neither prime nor composite.

ðŸ’» Program to find factors of a given number.

```function printFactors(n) {
let factors = '';
for (let index = 1; index <= n; index++) {
if (n % index === 0) {
factors += index + ' ';
}
}
console.log(factors);
return factors;
}
```
``````printFactors(15) // 1 3 5 15
printFactors(150) // 1 2 3 5 6 10 15 25 30 50 75 150
printFactors(100)  // 1 2 4 5 10 20 25 50 100 ``````

Time Complexity of the above program – O(n).

ðŸ’» Optimized version of factor program – O(SQRT(n)).

``````Factor of 100 > 1 2 4 5 10 20 25 50 100
100 / 1 = 100
100 / 2 = 50
100 / 4 = 25
100 / 5 = 20
100 / 10 = 10``````

With the above factors, we can clearly see that if `i` is a factor of a given number then` ``n/i` is also a factor of that number.

So instead of running the loop till n, we can run that loop to SQRT(n) and read both i and n / i numbers.

```function printFactorsOptimized(n) {
let factors = '';
const sqrtOfN = Math.floor(Math.sqrt(n));
for (let index = 1; index <= sqrtOfN; index++) {
if (n % index === 0) {
factors += index + ' ';
if (index !== n / index) {
factors += n / index + ' ';
}
}
}
console.log(factors);
return factors;
}```

The second way to write the same above program without using the Math SQRT method –

```function printFactorsOptimized2(n) {
let factors = '';
for (let index = 1; index <= n; index++) {
if (index * index > n) {
break;
}
if (n % index === 0) {
factors += index + ' ';
if (index !== n / index) {
factors += n / index + ' ';
}
}
}
console.log('printFactorsOptimized2 -', factors);
return factors;
}
```

So in the above code – If n is 100 & i is 11 then 11 * 11 = 121 which is > 100 , means there is no any factors above i > 10. We will terminate our iteration when `i * i > n`.

ðŸ’» Program to write Count number of factors

```function countFactors(n) {
let count = 0;
const sqrtOfN = Math.floor(Math.sqrt(n));
for (let i = 1; i <= sqrtOfN; i++) {
if (n % i === 0) {
if (i === n / i) {
count++;
} else {
count += 2;
}
}
}
console.log(count)
return count;
}
```
``````countFactors(100) // 9
countFactors(97) // 2
countFactors(11) // 2``````