Created
January 1, 2022 13:51
-
-
Save theabbie/ff67bc78cfe03f403a794d6052220b86 to your computer and use it in GitHub Desktop.
Nth Ugly Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def nthUglyNumber(self, n): | |
| N = 10000 | |
| uglies = {} | |
| uglies[1] = True | |
| factors = [([2], 2), ([3], 3), ([5], 5)] | |
| while len(factors) > 0: | |
| factor, p = factors.pop(0) | |
| uglies[p] = True | |
| for nn in [2, 3, 5]: | |
| np = p * nn | |
| if np <= N: | |
| factors.append((factor + [nn], np)) | |
| return sorted(uglies.keys())[n - 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment