Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def nthVampireNumber(n):
- guess = 0
- found = 0
- while found <= n:
- guess += 1
- if isVampireNumber(guess):
- found += 1
- return guess
- def isVampireNumber(n):
- for factor1 in range(1, sqrt(n)+1):
- if n%factor1 == 0:
- factor2 = n//factor1
- if not (factor1 % 10 == 0 and factor2 % 10 == 0):
- if goodLengths(factor1, factor2, n):
- if canRearrange(factor1, factor2, n):
- return True
- return False
- def goodLengths(f1, f2, n):
- return digitCount(f1) == digitCount(f2) == (1/2)*digitCount(n)
- def digitCount(n):
- count = 0
- while n > 0:
- count += 1
- n//10
- return count
- def canRearrange(f1, f2, n):
- overallString = str(f1) + str(f2)
- n = str(n)
- for digit in overallString:
- if overallString.count(digit) != n.count(digit):
- return False
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement