码迷,mamicode.com
首页 > 其他好文 > 详细

Project Euler 92

时间:2014-12-12 22:15:46      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:算法   数学   


0.57s,

import itertools
import time

def conquer():
    ans = 0
    DIGIT_LIMIT = 7
    ITER_STR = "0123456789"
    sum_square = lambda ss: sum( int( s ) ** 2 for s in str( ss ) )
    fact = lambda num: reduce( lambda x, y: x * y, xrange( 1, num + 1 ) )
    combinations = itertools.combinations_with_replacement( ITER_STR,
                                                            DIGIT_LIMIT )
    
    def combinatorial_num( num ):
        res = fact( DIGIT_LIMIT )
        s = str( num )
        for ss in ITER_STR:
            count = s.count( ss )
            if count > 1: res /= fact( count )
        return res
    
    for c in combinations:
        num = int( "".join( c ) )
        if num == 0: continue
        while num != 89 and num != 1:
            num = sum_square( num )
        if num == 89:
            ans += combinatorial_num( c )

    print ans

begin = time.time()
conquer()
end = time.time()
print end - begin


Project Euler 92

标签:算法   数学   

原文地址:http://blog.csdn.net/pandora_madara/article/details/41900605

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!