0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Find Factors of a Number

Use a loop from 1 to the number and check with if (number % i === 0) to find factors; for example, for(let i=1; i<=num; i++){ if(num % i === 0) console.log(i); } prints all factors.
📋

Examples

Input6
Output1 2 3 6
Input13
Output1 13
Input1
Output1
🧠

How to Think About It

To find factors of a number, think about which numbers divide it evenly without leaving a remainder. Start from 1 and go up to the number itself, checking if the number can be divided by each candidate number using the remainder operator %. If the remainder is zero, that candidate is a factor.
📐

Algorithm

1
Get the input number.
2
Create an empty list to store factors.
3
Loop from 1 to the input number inclusive.
4
For each number in the loop, check if it divides the input number evenly using the remainder operator.
5
If yes, add it to the list of factors.
6
After the loop ends, return or print the list of factors.
💻

Code

javascript
function findFactors(num) {
  const factors = [];
  for (let i = 1; i <= num; i++) {
    if (num % i === 0) {
      factors.push(i);
    }
  }
  return factors;
}

const number = 12;
const result = findFactors(number);
console.log(result.join(' '));
Output
1 2 3 4 6 12
🔍

Dry Run

Let's trace the number 6 through the code to find its factors.

1

Initialize factors list

factors = []

2

Start loop from i=1 to 6

i = 1

3

Check if 6 % 1 === 0

True, add 1 to factors: factors = [1]

4

Increment i to 2 and check 6 % 2 === 0

True, add 2: factors = [1, 2]

5

Increment i to 3 and check 6 % 3 === 0

True, add 3: factors = [1, 2, 3]

6

Increment i to 4 and check 6 % 4 === 0

False, do not add

7

Increment i to 5 and check 6 % 5 === 0

False, do not add

8

Increment i to 6 and check 6 % 6 === 0

True, add 6: factors = [1, 2, 3, 6]

9

Loop ends, return factors

Return [1, 2, 3, 6]

inum % i === 0?factors
1true[1]
2true[1, 2]
3true[1, 2, 3]
4false[1, 2, 3]
5false[1, 2, 3]
6true[1, 2, 3, 6]
💡

Why This Works

Step 1: Loop through numbers

We use a loop from 1 to the number because factors cannot be larger than the number itself.

Step 2: Check divisibility

Using num % i === 0 checks if the number divides evenly without remainder, which means i is a factor.

Step 3: Collect factors

When a factor is found, we add it to a list to keep track of all factors.

🔄

Alternative Approaches

Check only up to square root
javascript
function findFactorsSqrt(num) {
  const factors = [];
  for (let i = 1; i <= Math.sqrt(num); i++) {
    if (num % i === 0) {
      factors.push(i);
      if (i !== num / i) {
        factors.push(num / i);
      }
    }
  }
  return factors.sort((a,b) => a - b);
}

console.log(findFactorsSqrt(12).join(' '));
This method is faster for large numbers because it checks fewer candidates but requires sorting at the end.
Using recursion
javascript
function findFactorsRec(num, i = 1, factors = []) {
  if (i > num) return factors;
  if (num % i === 0) factors.push(i);
  return findFactorsRec(num, i + 1, factors);
}

console.log(findFactorsRec(12).join(' '));
Recursion is less efficient and can cause stack overflow for large numbers but shows a different approach.

Complexity: O(n) time, O(n) space

Time Complexity

The loop runs from 1 to n, so it takes O(n) time. Each iteration does a simple modulo check.

Space Complexity

We store factors in a list which can hold up to n elements in the worst case (e.g., number 1).

Which Approach is Fastest?

Checking up to the square root reduces time to O(√n), making it faster for large numbers compared to the simple O(n) loop.

ApproachTimeSpaceBest For
Simple loop 1 to nO(n)O(n)Small to medium numbers, easy to understand
Loop up to sqrt(n)O(√n)O(n)Large numbers, better performance
RecursionO(n)O(n)Learning recursion, not for large inputs
💡
To improve speed, check divisors only up to the square root of the number and add both divisor and quotient as factors.
⚠️
Beginners often forget to include the number itself as a factor or start the loop from 0 causing division errors.