Skip to content

Commit 49787e2

Browse files
committed
[Shreyash]: Added code to print prime factors of entered number
1 parent acddf67 commit 49787e2

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
Write a program to compute Factors of a number N using prime factorization method.
3+
Logic -> Traverse till i*i <= N instead of i <= N for efficiency.
4+
O/P -> Print the prime factors of number N.
5+
*/
6+
7+
const prompt = require("prompt-sync")();
8+
console.log();
9+
let n = parseInt(prompt(" Enter the number : "))
10+
console.log(" The Prime Factors are : ");
11+
for (i = 2; i * i <= n; i++) {
12+
while (n % i == 0) {
13+
console.log(" " + i);
14+
n = n / i;
15+
}
16+
}
17+
if (n != 1) {
18+
console.log(" " + n);
19+
}
20+
console.log();

0 commit comments

Comments
 (0)