Project Euler P.159

#!/usr/bin/env python
#-*- coding:utf-8 -*-
import math
import operator
"""Project Euler Problem 159"""

def factorizations(n):
  answer = []
  def sub(n, factor=2, factors=[]):
    for x in range(factor, int(math.sqrt(n))+1):
      if n % x == 0:
        sub(n/x, x, factors + [x])      
    answer.append(factors + [n])
  sub(n)
  return answer

def drs(l):
  def sub(n):
    return n if n < 10 else sub(reduce(operator.add, map(int, str(n))))
  return reduce(operator.add, map(sub, l))

def solve():
  answer = 0
  for i in range(2, 1000000):
    m = max(map(drs, factorizations(i)))
    answer += m

  print 'Answer: ', answer

def test(n):
  l = factorizations(n)
  for x in l:
    print x, drs(x)

if __name__ == '__main__':
  solve()

댓글 없음:

댓글 쓰기