본문 바로가기

프로그래밍/알고리즘

[EP0062] 같은 숫자로 이루어진 세제곱이 다섯개

반응형
#!/usr/bin/env python
# Project Euler 62
# http://projecteuler.net/index.php?section=problems&id=62
# Problem 62
#
# The cube, 41063625 (3453), can be permuted to produce two other cubes:
# 56623104 (3843) and 66430125 (4053). In fact, 41063625 is the smallest
# cube which has exactly three permutations of its digits which are also
# cube.
#
# Find the smallest cube for which exactly five permutations of its
# digits are cube.

def solve():
    di = {}  # python dictionary, hash

    n = 1

    while True:
        l = list(str(n * n * n))
        l.sort()
        sorted_str = "".join(l)
        if sorted_str not in di:
            di[sorted_str] = []
        di[sorted_str].append(n)
        if len(di[sorted_str]) == 5:
            break
        n += 1

    print(di[sorted_str])
    for n in di[sorted_str]:
        print(sorted_str, n * n * n)

if __name__ == "__main__":
    solve()
728x90