-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisSuperUgly.js
More file actions
executable file
·55 lines (35 loc) · 1.4 KB
/
isSuperUgly.js
File metadata and controls
executable file
·55 lines (35 loc) · 1.4 KB
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
/* https://leetcode.com/problems/super-ugly-number/description/
- NOTE I HAVE NOT YET DONE THIS PROBLEM
Write a program to find the nth super ugly number.
Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k.
Example:
Input: n = 12, primes = [2,7,13,19]
Output: 32
Explanation: [1,2,4,7,8,13,14,16,19,26,28,32] is the sequence of the first 12 super ugly numbers given primes = [2,7,13,19] of size 4.
A NICE ALGO AND STEP TO SOLVE IS GIVEN IN - https://www.geeksforgeeks.org/super-ugly-number-number-whose-prime-factors-given-set/
*/
isSuperUgly = (n, primes) => {
for (i of primes) {
while (n % i === 0) {
n /= i;
}
}
// After the while loops runs completely if the final number is 1 then its a superUgly. Else not, because if it was divided by any other number apart from the given set of primes, then the final number would be a decimal and will NOT be equal to 1
return n === 1
}
console.log(isSuperUgly(19, [2,7,13,19])) // => true
console.log(isSuperUgly(32, [2,7,13,19])) // => true
/*
var nthSuperUglyNumber = function (n, primes) {
let counter = 0;
for (let i = 0; i < 10000; i++ ) {
if (isSuperUgly(i, primes)) {
counter++
}
if (counter === n) {
return i;
break;
}
}
};
console.log(nthSuperUglyNumber(12, [2,7,13,19])) */