-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem56.py
68 lines (46 loc) · 2.07 KB
/
Problem56.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Code by @AmirMotefaker
# projecteuler.net
# https://projecteuler.net/problem=56
# Powerful digit sum
# Problem 56
# A googol (10**100) is a massive number: one followed by one-hundred zeros;
# 100**100 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?
# Solution 1
# Step 1: find the sum of digits of the given number. The function is as follows:
# -Let the number be n.
# -Start a while loop. Create a variable to store the sum of numbers.
# -Find the remainder of the number when divided by 10.
# This remainder will be the last digit. Add this number to the sum.
# -Divide the number with 10 and go again for step 2, until the number becomes 0.
# Step 2: Create two for loops both in the range of 0 to 100.
# One for a and one for b. Now find if any of the previous values of sum of digits
# is lesser than the present one, then assign the present value to the largest.
# In a similar way continue till the end of the loop.
# import time
# start_time = time.time() #Time at the start of program execution
# def sum_of_digits(n): #sum of digits of given number
# sod = 0
# while n != 0:
# sod += n % 10
# n //= 10
# return sod
# largest = 0
# for a in range(0, 100):
# for b in range(0, 100):
# sod = sum_of_digits(a**b)
# if sod > largest:
# largest = sod
# print (largest)
# end_time = time.time() #Time at the end of execution
# print ("Time of program execution:", (end_time - start_time)) #Time of program execution
# Solution 2 - fastest algorithm
import time
start_time = time.time() #Time at the start of program execution
print ("Maximum digital sum =",)
print (max(sum(map(int, str(a**b))) for a in range(60, 100) for b in range(80, 100)))
end_time = time.time() #Time at the end of execution
print ("Time of program execution:", (end_time - start_time)) #Time of program execution
#### Answer: 972