首页 > 代码库 > Project Euler 92
Project Euler 92
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。