forked from rohan-paul/Awesome-JavaScript-Interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisUgly-Find-nth-Ugly.js
50 lines (37 loc) · 1 KB
/
isUgly-Find-nth-Ugly.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
isUgly = (num) => {
if ( num <= 0 ) return false;
let primeFactors = [2, 3, 5];
for ( var i of primeFactors ) {
while ( num % i === 0 ) {
num /= i;
}
}
return num === 1;
}
// console.log(isUgly(9000)); // should output true
// Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. By convention, 1 is included.
// Find n-th ugly number
find_nth_Ugly = n => {
let counter = 0;
for (let i = 0; i < 1000; i++) {
if (isUgly (i)) {
counter++
}
if (counter === n) {
return i;
break;
}
}
}
console.log(find_nth_Ugly(3)) // Should return 3
// Return the series of the first n-th ugly numbers
find_nth_Ugly = n => {
let uglySeries = [];
for (let i = 0; i <= n; i++) {
if (isUgly (i)) {
uglySeries.push(i)
}
}
return uglySeries;
}
console.log(find_nth_Ugly(3)); // Should return [ 1, 2, 3 ]