Project Euler Problem 56 (C++和Python)

部署运行你感兴趣的模型镜像

Problem 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?

C++代码

#include <iostream>

using namespace std;

//#define UNIT_TEST

class PE0056
{
private:
    static const int max_digits = 200;
    int m_digitsArray[max_digits]; 

    void adjustMyDigitArray(int myDigitArray[]);
    int getPowerDigitsSum(int a, int b);

public:
    int getMaxDigitsSum();
};


void PE0056::adjustMyDigitArray(int myDigitArray[])
{        
    for(int j=0; j<max_digits-1; j++)
    {
        myDigitArray[j+1] += myDigitArray[j]/10;
        myDigitArray[j] %= 10;
    }
}

int PE0056::getPowerDigitsSum(int a, int b)
{
    for(int j=0; j<max_digits; j++)
    {
        m_digitsArray[j] = 0;
    }

    m_digitsArray[0] = 1;
    for(int i=1; i<=b; i++)
    {
        for(int j=0; j<max_digits; j++)
        {
            m_digitsArray[j] *= a; // a^b
        }
    
        adjustMyDigitArray(m_digitsArray);
    }

    int digits_sum = 0;
    for(int j=0; j<max_digits; j++)
    {
        digits_sum += m_digitsArray[j];
    }

    return digits_sum;
}

int PE0056::getMaxDigitsSum()
{
    int digits_sum_max = 0;
    int a_max = 0, b_max = 0;
    int digits_sum = 0;

    for (int a=2; a<100; a++)
    {
        for(int b=2; b<100; b++)
        {
            digits_sum = getPowerDigitsSum(a, b);
            if (digits_sum > digits_sum_max)
            {
                digits_sum_max = digits_sum;
                a_max = a;
                b_max = b;
            }
        }
    }

#ifdef UNIT_TEST
    cout << "While a = " << a_max << " and b = " << b_max << ", "; 
#endif

    return digits_sum_max;
}
    
int main()
{
    PE0056 pe0056;

    cout << "the maximum digits sum is " << pe0056.getMaxDigitsSum();
    cout << "." << endl;

    return 0;
}

Python 代码

def getPowerDigitsSum(a, b):
    p = 1
    for i in range(b):
        p *= a
    digits_list = [ int(s) for s in list(str(p)) ]
    return sum(digits_list)

def getPowerDigitsSumNew(a, b):
    return sum(map(int, str(a**b)))
    
def getMaxDigitsSum():
    digits_sum_max, a_max, b_max, digits_sum = 0, 0, 0, 0

    for a in range(50, 100):
        for b in range(50, 100):
            digits_sum = getPowerDigitsSumNew(a, b)
            if digits_sum > digits_sum_max:
                digits_sum_max = digits_sum
                a_max, b_max   = a, b

    print("Where a = ", a_max, "and b = ", b_max, ", ")
    return digits_sum_max

def getMaxDigitsSumNew():
    digits_sum_max = max((getPowerDigitsSum(a, b)) \
        for a in range(50, 100) \
            for b in range(50, 100))
    return digits_sum_max

def main():
    assert 1 == getPowerDigitsSum(10, 100)
    assert 1 == getPowerDigitsSumNew(100, 100)
    print("The maxium digits sum is", getMaxDigitsSumNew(), ".")

if  __name__ == '__main__':
    main()        

您可能感兴趣的与本文相关的镜像

Python3.11

Python3.11

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值