|
5 | 5 | * They can be represented in the form 2^a * 3^b * 5*c. By convention, 1 is also considered to be
|
6 | 6 | * an ugly number.
|
7 | 7 | * The first few terms of the sequence are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20...
|
8 |
| - * |
| 8 | + * |
9 | 9 | * For the provided n, the nth ugly number shall be computed.
|
10 | 10 | * @see [GeeksForGeeks](https://www.geeksforgeeks.org/ugly-numbers/)
|
11 | 11 | */
|
12 |
| -function* UglyNumbers() { |
13 |
| - yield 1 |
| 12 | +function* uglyNumbers() { |
| 13 | + yield 1; |
14 | 14 |
|
15 |
| - let idx2 = 0, idx3 = 0, idx5 = 0 |
16 |
| - const uglyNumbers = [1] |
| 15 | + let idx2 = 0, |
| 16 | + idx3 = 0, |
| 17 | + idx5 = 0; |
| 18 | + const uglyNums: number[] = [1]; |
17 | 19 |
|
18 |
| - let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number |
| 20 | + let nextx2: number, nextx3: number, nextx5: number, nextUglyNum: number; |
19 | 21 |
|
20 |
| - while(true) { |
21 |
| - nextx2 = uglyNumbers[idx2] * 2 |
22 |
| - nextx3 = uglyNumbers[idx3] * 3 |
23 |
| - nextx5 = uglyNumbers[idx5] * 5 |
| 22 | + while (true) { |
| 23 | + nextx2 = uglyNums[idx2] * 2; |
| 24 | + nextx3 = uglyNums[idx3] * 3; |
| 25 | + nextx5 = uglyNums[idx5] * 5; |
24 | 26 |
|
25 |
| - nextUglyNum = Math.min(nextx2, nextx3, nextx5) |
26 |
| - yield nextUglyNum |
| 27 | + nextUglyNum = Math.min(nextx2, nextx3, nextx5); |
| 28 | + yield nextUglyNum; |
27 | 29 |
|
28 |
| - if(nextx2 === nextUglyNum) idx2++ |
29 |
| - if(nextx3 === nextUglyNum) idx3++ |
30 |
| - if(nextx5 === nextUglyNum) idx5++ |
31 |
| - |
32 |
| - uglyNumbers.push(nextUglyNum) |
| 30 | + if (nextx2 === nextUglyNum) idx2++; |
| 31 | + if (nextx3 === nextUglyNum) idx3++; |
| 32 | + if (nextx5 === nextUglyNum) idx5++; |
| 33 | + |
| 34 | + uglyNums.push(nextUglyNum); |
33 | 35 | }
|
34 | 36 | }
|
35 | 37 |
|
36 |
| -export { UglyNumbers } |
| 38 | +export { uglyNumbers }; |
0 commit comments