-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimes in numbers.js
67 lines (50 loc) · 1.33 KB
/
primes in numbers.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//https://www.codewars.com/kata/54d512e62a5e54c96200019e/train/javascript
/**
* Given a positive number n > 1 find the prime factor decomposition of n. The result will be a string with the following form :
"(p1**n1)(p2**n2)...(pk**nk)"
where a ** b means a to the power of b
with the p(i) in increasing order and n(i) empty if n(i) is 1.
Example: n = 86240 should return "(2**5)(5)(7**2)(11)"
*/
function primeFactors(n) {
if (!n || n < 2)
return [];
let f = [];
let prev = 2, counter = -1, arrayCount = [];
for (var i = 2; i <= n; i++){
// console.log("i is: "+i);
while (n % i === 0){
console.log(`prev is ${prev} and i is ${i}`);
f.push(i);
n /= i;
}
}
let newArr = [];
let temp;
temp = f[0];
let count = 0;
for(let i = 0; i < f.length;i++){
if(temp === f[i]){
count++;
} else{
newArr.push([temp, count]);
count = 1;
temp = f[i];
}
if (i === f.length -1){
newArr.push([temp, count]);
}
}
console.log(newArr);
console.log(f);
let res = [];
for(e of newArr){
console.log(e)
if(e[1] >= 2){
res.push("("+e[0]+"**"+e[1]+")");
} else{
res.push("("+e[0]+")");
}
}
return res.join("");
};