zoukankan      html  css  js  c++  java
  • [Project Euler] Problem 56

    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.

  • 相关阅读:
    代码检查工具介绍
    Eclipse利用代理快速安装插件
    toString结果
    Eclipse查看jdk源码
    java语言基础特性
    TODO、FIXME和XXX转载
    java泛型
    不良代码总结
    mockServer学习
    akka
  • 原文地址:https://www.cnblogs.com/quark/p/2621099.html
Copyright © 2011-2022 走看看