-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem30.py
More file actions
53 lines (45 loc) · 1.17 KB
/
problem30.py
File metadata and controls
53 lines (45 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# project euler problem 30
# http://projecteuler.net/problem=30
# Since 9**5*7 = 413343 is a 6-digit number
# so there is no 7-digit number can fullfil the request
# hence, we can determine the upper-bond.
# Finished in 7.0s on my computer.
total_sum = 0
for x in xrange(2, 1000000):
if x == sum(map(lambda x:x**5, map(int, str(x)))):
total_sum += x
print total_sum
# We could also write it in one line :)
print reduce(lambda x, y: x+y, (x for x in xrange(2, 1000000) if x == sum(map(lambda x:x**5, map(int, str(x))))))
# The equivalent C code only need 0.257s...
# Huge performance difference
'''
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int get_sum(const char *s)
{
unsigned int n = 0;
unsigned int sum = 0;
const char *p = s;
while (*p != '\0') {
n = *p - '0';
sum += (n*n*n*n*n);
p++;
}
return sum;
}
int main(int argc, char *argv[])
{
unsigned int i = 0;
char buffer[7] = {0};
unsigned int ret = 0;
for (i = 2; i < 1000000; i++) {
sprintf(buffer, "%u", i);
if (i == get_sum(buffer))
ret += i;
}
printf("ret = %u\n", ret);
return 0;
}
'''