题目30:找出所有能够写成各位数字5次方之和的数之和
本帖最后由 不二如是 于 2017-6-14 22:04 编辑Digit fifth powers
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
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.
题目:
令人惊奇的,只有三个数能够写成它们各位数的四次方之和:
没有被算作在内因为它不是一个和。
这些数字的和是 1634 + 8208 + 9474 = 19316.
找出所有能被写成各位数字五次方之和的数之和。 结果为:443839
代码如下:
result = []
for i in range(1,10):
if i**5*i < int(str(9)*i):
up_boundary = i**5*i
for j in range(2,up_boundary):
length = len(str(j))
s = 0
for k in range(length):
s += (int(str(j)))**5
if s == j:
result.append(j)
print(sum(result))
有6个 数字
分别是 4150 4151 54748 92727 93084 194979
和是443839
代码
int divcalc(int num)
{
int k = 0;
int sum = 0;
while ( num )
{
k = num%10;
sum = sum + pow(k,5);
num/=10;
}
return sum;
}
int main()
{
for (int n=2;n<=10000000;n++)
{
if(n==divcalc(n))
{
std::cout<<n<<endl;
}
}
return 0;
} i = 1
result = 0
while True:
temp = i
total = 0
while temp:
total += (temp%10)**5
temp = temp//10
if total == i and i != 1:
result += i
print(result)
i += 1
等半天没有继续出结果最后一个是443839 本帖最后由 jerryxjr1220 于 2016-9-22 14:40 编辑
{194979, 4151, 4150, 92727, 54748, 93084} 443839
lst = []
for i in range(2,1000000):
length = len(str(i))
if int(str(i))**5 <= i:
summ = 0
for jin range(length):
summ += int(str(i))**5
if summ == i:
lst.append (i)
lst = list(set(lst))
print (lst, sum(lst)) 一个期间内所有符合该算法的数
import math
def SumSXH(arg):
Num = arg
Count = 0
Sum = 0
eNum = len(str(Num))
listNum = []
for i in str(Num):
listNum.append(i)
while Count < eNum:
Sum += math.pow(int(listNum), eNum)
Count += 1
return int(Sum)
NumStart = int(raw_input("StartNum: "))
NumEnd = int(raw_input("EndNum: "))
Count = NumStart
if NumStart >= NumEnd:
print "The num is error!"
else:
while Count < NumEnd:
if Count == SumSXH(Count):
print Count
Count += 1
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
4210818
9800817
9926315
24678050
24678051
# encoding:utf-8
# 找出所有能写成各位数字五次方之和的数之和
# 比如 ABCD = A**5 + B**5 + C**5 + D**5
from time import time
import itertools
def euler030():
l_nums =
l_result = []
#粗略算一下,只能是3-6位数之间
for i in range(3, 7):
for each in itertools.combinations_with_replacement(l_nums, i):
n_sum = sum()
t1 = list(each)
t1.sort()
t2 =
t2.sort()
if t1 == t2:
l_result.append(n_sum)
print(sum(l_result))
if __name__ == '__main__':
start = time()
euler030()
print('cost %.6f sec' % (time() - start))
443839
cost 0.075007 sec 376103327 发表于 2016-10-26 17:39
一个期间内所有符合该算法的数
import math
def SumSXH(arg):
第一个数算一下就不满足条件{:10_277:} 此代码使用matlab编程
Problem30所用时间为50.6545秒
Problem30的答案为443839
%% Problem30
% 题目30:找出所有能够写成各位数字5次方之和的数之和
function Output=Problem30(Input)
tic
if nargin==0
Input=5;
end
%已经确定最多为6位数
Limit=sum(.^Input);%上限
Sum=0;
disp(Limit)
for ii=2:Limit
Temp=str2num(num2str(ii)')';
if sum(Temp.^5)==ii
disp(ii)
Sum=Sum+ii;
end
end
Output=Sum;
toc
disp('此代码使用matlab编程')
disp(['Problem30所用时间为',num2str(toc),'秒'])
disp(['Problem30的答案为',num2str(Output)]) jh=[]
for i in range(20,295245):
x=list(str(i))
he=0
for j in x:
he+=int(j)**5
if he==i:
jh.append(i)
print(sum(jh),jh)
== RESTART: C:\Users\ASUS\AppData\Local\Programs\Python\Python35-32\test.py ==
443839
>>> list1=[]
for i in range(10,5*9**5+2**5+1):
st=str(i)
sum0=0
for j in st:
sum0+=int(j)**5
if sum0==i:
list1.append(i)
print(sum(list1),list1)
443839
>>> public class DigitFifthPowers{
public static void main(String[] args){
int n = 2;
int sum = 0;
int t = n;
int[] result = new int;
int count = 0;
for(n = 2;n < 1000000;n ++){
t = n;
count = 0;
for(int i = 0;i < result.length;i ++){
result = 0;
}
while(t != 0){
result = t % 10;
t /= 10;
}
if(powerSum(n,result,count)){
System.out.print(n+"\t");
sum += n;
}
}
System.out.println("\n它们的和为"+sum);
}
public static boolean powerSum(int n,int[] result,int count){
int sum = 0;
for(int i = 0;i < count;i ++){
sum += Math.pow(result,5);
}
if(sum == n){
return true;
}
else{
return false;
}
}
}
4150 4151 54748 92727 93084 194979
它们的和为443839 计算和是:443839
用时:2.8860185秒import time
def dig_sum(num):
sum = 0
tmp = num
while tmp > 0:
sum += (tmp % 10)**5
tmp = tmp//10
if sum == num:
return True
else:
return False
print("计算和是:{}\n用时:{}秒".format(sum(), time.process_time())) 本帖最后由 永恒的蓝色梦想 于 2021-2-8 17:06 编辑
C++ 67ms#include<iostream>
using namespace std;
int main() {
int sum = 0, i, j, k, p[] = { 0, 1, 32, 243, 1024, 3125, 7776, 16807, 32768, 59049 };
for (i = 10; i < 1000000; ++i) {
j = 0;
k = i;
while (k) {
j += p;
k /= 10;
}
sum += i == j;
}
cout << sum << endl;
return 0;
}
start = time.time()
a = 100
addup = 0
while a < 1000000:
a = str(a)
sum = 0
for i in range(len(a)):
sum += int(a)**5
a = int(a)
if sum == a:
print(a)
addup += a
a += 1
print(addup)
end = time.time()
print("共用时%f秒" %(end - start))
4150
4151
54748
92727
93084
194979
443839
共用时3.221054秒
#include <stdio.h>
#include <math.h>
int sum_num(int);
int sum_num(int num)
{
int i, sum = 0;
i = num;
while (i)
{
sum += pow((i % 10), 5);
i /= 10;
}
return sum;
}
main()
{
int i, j, sum = 0;
for (i = 2; i < 1000000; i++)
{
j = sum_num(i);
if (j == i)
{
sum += j;
printf("%d ", j);
}
}
printf("\n%d\n", sum);
}
不知道还能不能优化, 望指教 #找出所有能够写成各位数字5次方之和的数之和
from time import*
start = time()
a = 4150
res = []
result = []
while a < 200000:
for i in str(a):
res.append(int(i) ** 5)
if sum(res) == a:
result.append(a)
res = []
else:
res = []
a += 1
end = time()
print(result)
print(sum(result))
print("用时:%.2fs" % (end-start))
本帖最后由 guosl 于 2022-1-3 10:00 编辑
由于这个题目中没有给出数据范围,而又要求求出所有满足条件的数,为此我们首先要求出这题的数据范围,这样才能保证所有的数都被求出。假设这个数n是一个x位的10进制数,那么:
10^(x-1)<=n<=x*9^5,我们来看看满足这个不等式的x最大是多少?作函数 f(x)=x*9^5-10^(x-1),对这个函数求导得:f'(x)=9^5-(x-1)*10^(x-2),我们可以计算出当x>=7时这个函数小于0,也就是说f(x)当x>=7时是一个严格的递减函数,或者换句话将就是:n将无法表示为其各位数字的5次方之和。故这样数的上限是:10^7。
/*
答案:443839
耗时:0.107251秒(单核)
0.0160824秒(六核)
*/
#include <iostream>
#include <omp.h>
using namespace std;
const int N = 10000000;
int getDigitsSum(int n)
{
int nSum = 0;
while (n > 0)
{
int k = n % 10;
n /= 10;
nSum += k * k * k * k * k;
}
return nSum;
}
int main(void)
{
int nSum = 0;
#pragma omp parallel for reduction(+:nSum)
for (int i = 2; i <= N; ++i)
{
if (i == getDigitsSum(i))
nSum += i;
}
cout << nSum << endl;
return 0;
} table = []
for i in range(10):
for j in range(10):
for k in range(10):
for m in range(10):
for n in range(10):
for a in range(10):
if(100000*a+10000*n+1000*m+100*k+10*i+j==i**5+j**5+k**5+m**5+n**5+a**5):
table.append(100000*a+10000*n+1000*m+100*k+10*i+j)
table.remove(0)
table.remove(1)
print(table,"\n",sum(table))
443839
exited with code=0 in 1.53 seconds 本帖最后由 Asss-whom 于 2022-8-8 12:47 编辑
use rayon::prelude::*;
use std::time::Instant;
fn main() {
let now = Instant::now();
let num: i32 = (2..354294) // (9 ^ 5) * 6 < 999999
.into_par_iter()
.filter(|x| check(*x))
.sum();
println!("cost {}ms.", now.elapsed().as_millis());
println!("{:?}", num)
}
fn check(num: i32) -> bool {
let mut value = 0;
for i in num.to_string().bytes() {
value += ((i - 48) as i32).pow(5)
}
value == num
}
cost 10ms.
443839
页:
[1]
2