Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

project euler

How to calculate the powerful digital sum

Kainat Asif

Problem statement

  • A googol (1010010^{100}) is a massive number, i.e., one followed by one hundred zeros.
  • 100100100^{100} (1020010^{200}) is almost unimaginably large, i.e., 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 ‘ab,’ where a, b < 100, what is the maximum digital sum?

Expected output

Maximum digital sum = 972

Solution

Let’s look at the code snippet below. It gives a solution for how to calculate the powerful digital sum of a, b < 100.

a = 100
b = 100
maxSum = 0
for i in range(a):
    for j in range(b):
        temp = i**j #i**j = i^j
        temp = map(int, str(temp)) #mapping int to string as it is less costly to parse strings than dealing with big integers
        sumList = list(temp) #making a list of numbers

        tempSum = sum(sumList); #calculating sum of digits
        if(tempSum > maxSum):
            maxSum = tempSum #finding max sum
print "Maximum digital sum = ", maxSum

Explanation

The nested loop calculates the exponent iji^{j}, where 0<i<100 and 0<j<100. The sum of the digits of the exponent is calculated in line 10 and the maximum digit sum is stored in maxSum in line 12. This is the powerful digital sum aba^{b} for a<100 and b<100.

RELATED TAGS

project euler

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring