[Python] 56-Powerful digit sum

A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.

Considering natural numbers of the form, ab, where a, b < 100, what is the maximum digital sum?

문제 해결 전략

  1. 제곱을 문자열로 변환하여 문자열을 한자씩 더해 간다.
  2. 문자열을 더하기 위해서 reduce 와 lambda를 활용한다.
def sumOfDigit(i, j):
  powValue = str(i**j)
  result = reduce(lambda x, y: int(x)+int(y), powValue)
  return int(result)

if __name__ == '__main__':
  maxValue = iVal = jVal = 0    
  for i in range(1, 101):      
    for j in range(1, 101):        
      result = sumOfDigit(i, j)
      if (result > maxValue):          
        maxValue, iVal, jVal = result, i, j          
  print "i=%d, j=%d, maximum=%d" % (iVal, jVal, maxValue)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s