-
Notifications
You must be signed in to change notification settings - Fork 0
/
question56.py
executable file
·53 lines (38 loc) · 1.03 KB
/
question56.py
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
#!/usr/bin/env python3
# Powerful digit sum
#Problem 56
#A googol (10^100) 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, a^b, where a, b < 100, what is the maximum digital sum?
import time
start_time = time.time()
def power(a,b):
if b==0: return 1
elif b==1: return a
else:
temp = a
for i in range(b-1):
temp *= a
return temp
def sum_digits(n):
r = 0
while n:
r, n = r + n % 10, n // 10
return r
limit = 100
count=0
resa = 0
resb = 0
for a in reversed(range(1,101)):
for b in reversed(range(1,101)):
temp = sum_digits(power(a,b))
if temp>count:
count = temp
resa = a
resb = b
print(" ",a,"^",b)
print(count,resa,resb)
#972 = 99 ^95
#--- 0.25336360931396484 seconds ---
print ('--- %s seconds ---' % (time.time()-start_time))
# 4075 YAY
# --- 0.035889387130737305 seconds ---