quark

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Problem Description

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?

 

Python

Of course python, here is the code:

def Power(a, b) :
    result = 1
    for i in xrange(b):
        result = result * a    
    return result

def GetSum(num) :
    strNum = str(num)
    result = 0;
    for index in xrange(len(strNum)) :
        result = result + int(strNum[index])
    return result

def Problem_56() :
    maxNum = 0
    maxA =0;
    maxB =0;
    for a in xrange(1, 100):
        for b in xrange(1, 100) :
            temp = GetSum(Power(a, b))
            # print "a=%d, b=%d temp=%d" % (a, b, temp)
            if(temp > maxNum) :
                maxNum = temp
                maxA = a
                maxB = b
    print "%d^%d = %d" % (maxA, maxB, maxNum)

Problem_56()

Thi problem is easy, brute force is needed.

posted on 2012-08-03 10:16  QuarkZ  阅读(192)  评论(0编辑  收藏  举报