Skip to content

Instantly share code, notes, and snippets.

@theabbie
Created January 1, 2022 13:51
Show Gist options
  • Select an option

  • Save theabbie/ff67bc78cfe03f403a794d6052220b86 to your computer and use it in GitHub Desktop.

Select an option

Save theabbie/ff67bc78cfe03f403a794d6052220b86 to your computer and use it in GitHub Desktop.
Nth Ugly Number
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