Last active
December 10, 2015 08:58
-
-
Save eddyxu/4411687 to your computer and use it in GitHub Desktop.
Eular Project Problem 119
This file contains 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
#!/usr/bin/env python | |
# | |
# Author: Lei Xu <eddyxu@gmail.com> | |
"""Eular Project Problem 119 | |
http://projecteuler.net/problem=119 | |
""" | |
import heapq | |
def main(): | |
h = [] | |
heapq.heappush(h, (2**2, 2)) | |
heapq.heappush(h, (3**3, 3)) | |
next_max_n = 4 | |
results = [] | |
while len(results) < 30: | |
p, n = heapq.heappop(h) | |
#print p, n | |
if sum(map(int, str(p))) == n: | |
results.append(p) | |
heapq.heappush(h, (p * n, n)) | |
if p * n > (next_max_n ** 2): | |
heapq.heappush(h, (cur_max_n**2, cur_max_n)) | |
cur_max_n += 1 | |
print results[-1] | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment