forked from TheAlgorithms/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactors.ts
More file actions
28 lines (25 loc) · 829 Bytes
/
factors.ts
File metadata and controls
28 lines (25 loc) · 829 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @function findFactors
* @description Find all the factors of a natural number.
* @param {number} num - A natural number.
* @return {Set<number>} - A set of all the factors of given natural number.
* @see https://en.wikipedia.org/wiki/Divisor
* @example findFactors(1) = [1]
* @example findFactors(4) = [1,2,4]
* @example findFactors(16) = [1,3,5,15]
*/
export const findFactors = (num: number): Set<number> => {
if (num <= 0 || !Number.isInteger(num)) {
throw new Error('Only natural numbers are supported.')
}
const res: Set<number> = new Set()
// Iterates from 1 to square root of num & pushes factors into the res set.
for (let i = 1; i * i <= num; i++) {
if (num % i === 0) {
res.add(i)
const sqrtFactor = Math.floor(num / i)
res.add(sqrtFactor)
}
}
return res
}