Skip to content

Commit ff86ae7

Browse files
committed
Factors of N-Natural Numbers
1 parent b781acf commit ff86ae7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Maths/factor-of-natural-number.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// n = 10 , output : 1,2,5,10
2+
3+
function printDivisors(n)
4+
{
5+
for (i=1;i<=n;i++){
6+
if (n%i==0){
7+
console.log(i)
8+
}
9+
}
10+
}
11+
12+
function printDivisorsOptimized(n)
13+
{
14+
for(let i = 1; i <= Math.sqrt(n); i++)
15+
{
16+
if (n % i == 0)
17+
{
18+
if (parseInt(n / i, 10) == i){
19+
console.log(i)
20+
}
21+
else{
22+
console.log(i)
23+
console.log(parseInt(n / i, 10));
24+
}
25+
}
26+
}
27+
}
28+
29+
printDivisors(10) // Time Complexity : O(n)
30+
printDivisorsOptimized(10) // Time Complexity : O ( sqrt(n))
31+

0 commit comments

Comments
 (0)