Project Euler Problem 29 (C++和Python)

探讨了在2≤a≤100和2≤b≤100的范围内,由ab生成的序列中不同项的数量。通过C++和Python代码实现,去除重复项后,得到序列中独特项的总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 29 : Distinct powers

Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

C++ source code

#include <iostream>
#include <set>
#include <string>
#include <cassert>

using namespace std;

class PE0029
{
private:
    static const int MAX_DIGITS = 256;
    int m_digitsArray[MAX_DIGITS+1]; 

    void myPower(int a, int b);
    void adjustDigitsArray();
    string getPowerDigitsString(int a, int b);  // a^b

public:
    int getDistinctPowersSeq(int x, int y);
};

void PE0029::adjustDigitsArray()
{        
    for(int j=0; j<MAX_DIGITS-1; j++)
    {
        m_digitsArray[j+1] += m_digitsArray[j]/10;
        m_digitsArray[j]   %= 10;
    }
}

void PE0029::myPower(int a, int b) // a^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; 
        }
    
        adjustDigitsArray();
    }
}

string PE0029::getPowerDigitsString(int a, int b)  // a^b
{
    char m_digitsString[MAX_DIGITS+1];

    myPower(a, b);  // a^b, digits are in m_digitsArray[]

    for(int i=0; i<MAX_DIGITS; i++)
    {
        m_digitsString[i] = (char)(m_digitsArray[i]+'0'); // 0 -> '0', 1 -> '1'
    }

    string digitsString; 
    digitsString.append(m_digitsString);

    return digitsString;
}

int PE0029::getDistinctPowersSeq(int x, int y)
{
    set<string> distinctPowers;
    string digitsString;

    for (int a=2; a<=x; a++)
    {
        for(int b=2; b<=y; b++)
        {
            digitsString = getPowerDigitsString(a, b);
            distinctPowers.insert(digitsString);
        }
    }

    return distinctPowers.size();
}

int main()
{
    PE0029 pe0029;

    assert(15 == pe0029.getDistinctPowersSeq(5, 5));

    cout << pe0029.getDistinctPowersSeq(100, 100) << " distinct terms are in ";
    cout << "the sequence generated by a^b for 2<=a<=100 and 2<=b<=100 " << endl;

    return 0;
}

Python source code

def getDistinctPowersSeq(x, y):
    distinctPowersArray = []
    for a in range(2, x+1):
        for b in range(2, y+1):
            distinctPowersArray += [int(pow(a,b))]
    return len(list(set(distinctPowersArray)))
        
def main():
    assert 15 == getDistinctPowersSeq(5, 5)
    print(getDistinctPowersSeq(100, 100),"distinct terms are in the sequence")
    print("generated by a^b for 2<=a<=100 and 2<=b<=100 ")
    
if  __name__ == '__main__':
    main()

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值