Skip to content

feat: Added implementation for ugly numbers #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@
## Graph
* [Bellman Ford](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/bellman_ford.ts)
* [Dijkstra](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/dijkstra.ts)
* [Floyd Warshall](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/floyd_warshall.ts)
* [Johnson](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/johnson.ts)
* [Kruskal](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/kruskal.ts)
* [Prim](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/prim.ts)
* Test
* [Bellman Ford.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/bellman_ford.test.ts)
* [Dijkstra.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/dijkstra.test.ts)
* [Floyd Warshall.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/floyd_warshall.test.ts)
* [Johnson.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/johnson.test.ts)
* [Kruskal.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/kruskal.test.ts)
* [Prim.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/graph/test/prim.test.ts)

## Maths
* [Absolute Value](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/absolute_value.ts)
Expand Down Expand Up @@ -101,6 +107,7 @@
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
* [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts)
* [Ugly Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/ugly_numbers.ts)
* [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts)

## Other
Expand Down
5 changes: 5 additions & 0 deletions maths/test/ugly_numbers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { UglyNumbers } from '../ugly_numbers';

test.each([[1, 1], [7, 8], [11, 15]])('Ugly Numbers', (number: number, result: number) => {
expect(UglyNumbers(number)).toBe(result)
})
39 changes: 39 additions & 0 deletions maths/ugly_numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @function UglyNumbers
* @description Returns the nth ugly number
* @summary Ugly numbers are natural numbers whose only prime factors are 2, 3 and 5.
* They can be represented in the form 2^a * 3^b * 5*c. By convention, 1 is also considered to be
* an ugly number.
* The first few terms of the sequence are: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20...
*
* For the provided n, the nth ugly number shall be computed.
* @param n The nth ugly number to find
* @returns {number} The nth ugly number
* @see [GeeksForGeeks](https://www.geeksforgeeks.org/ugly-numbers/)
* @example UglyNumbers(1) = 1
* @example UglyNumbers(7) = 8
*/
const UglyNumbers = (n: number) => {
const uglyNums = [1];
let idx2 = 0;
let idx3 = 0;
let idx5 = 0;
let nextx2: number, nextx3: number, nextx5: number, nextNum: number;

while (uglyNums.length < n) {
nextx2 = uglyNums[idx2] * 2;
nextx3 = uglyNums[idx3] * 3;
nextx5 = uglyNums[idx5] * 5;
nextNum = Math.min(nextx2, nextx3, nextx5);

uglyNums.push(nextNum);

if (nextx2 === nextNum) idx2++;
if (nextx3 === nextNum) idx3++;
if (nextx5 === nextNum) idx5++;
}

return uglyNums[n - 1];
}

export { UglyNumbers }