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?
문제 해결 전략
- 제곱을 문자열로 변환하여 문자열을 한자씩 더해 간다.
- 문자열을 더하기 위해서 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)