Project Euler Problem 30 (C++和Python)

100 篇文章 3 订阅
87 篇文章 1 订阅

Problem 30 : Digit fifth powers

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84+ 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

C++ source code

#include <iostream>
#include <cmath>
#include <cassert>

using namespace std;

class PE0030
{
private:
    bool checkXthPowersOfDigits(int number, int exp);

public:
    int getSumOfNumbers(int exponent);
};

bool PE0030::checkXthPowersOfDigits(int number, int exp)
{
    int num = number;
    int digit;
    int sum = 0;
    
    while(num > 0)
    {
        digit = num%10;
        sum  += (int)pow((double)digit, exp);
        num  /= 10;
    }

    if (number == sum)
    {
        return true;
    }

    return false;
}

int PE0030::getSumOfNumbers(int exponent)
{
    int N;

    if (4 == exponent)
    {                  
        N = 5*59049; // 99999 > 9^5 + 9^5 + 9^5 + 9^5 + 9^5 = 5*59049
    }
    else //(5 == exponent) // 9^5=  59049  
    {
        N = 6*59049; // 999999 > 9^5 + 9^5 + 9^5 + 9^5 + 9^5 + 9^5 = 6*59049
    }

    int sum = 0;                 
    for(int n = 2; n < N; n++) 
    {
        if (true == checkXthPowersOfDigits(n, exponent))
        {
            sum += n;
        }
    }

    return sum;
}

int main()
{
    PE0030 pe0030;

    assert(19316 == pe0030.getSumOfNumbers(4));

    cout << "The sum of all the numbers that can be written as the sum of ";
    cout << "fifth powers of their digits is " << pe0030.getSumOfNumbers(5) << endl;

    return 0;
}

Python source code

def checkXthPowersOfDigits(number, exponent):
    num, sum = number, 0
    
    while int(num) > 0:
        digit = int(num % 10)
        sum  += pow(digit,exponent)
        num  /= 10

    if number == sum:
        return True

    return False

def getSumOfNumbers(exponent):
    if 4 == exponent:                 
        N = 5*59049 # 99999 > 9^5 + 9^5 + 9^5 + 9^5 + 9^5 = 5*59049
    else: #(5 == exponent) // 9^5=  59049  
        N = 6*59049 # 999999 > 9^5 + 9^5 + 9^5 + 9^5 + 9^5 + 9^5 = 6*59049

    sum = 0                
    for n in range(2, N): 
        if True == checkXthPowersOfDigits(n, exponent):
            sum += n
    return sum

def main():
    assert 19316 == getSumOfNumbers(4)

    print("The sum of all the numbers that can be written as the sum of")
    print("fifth powers of their digits is",getSumOfNumbers(5))

if  __name__ == '__main__':
    main()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值