Home > Problem, Projects/Experiments, Python > Python Project: Sum of power of digits

Python Project: Sum of power of digits




Python: Sum of power of digits


Problem No.16

In Problem No.16, projectEuler ask to find the Sum of power of digits in the number, for example if we have 2^15 (2 to power of 15) the answer is 23768 then we need to calculate the sum of this number (2+3+7+6+8 ) that’s equal to 26.

The Task: So our task in this project is to find the sum of the digits of (2^1000). To write this as a program and to make it more general we will ask the user to input the number and the power he want, then I start thinking to restrict user from input large numbers that could cause CPU problems, but then i decide to keep it open as is.



The Code:


num=2
p=15

num=int(input(“Enter a number “))
p=int(input(“Enter a power “))
a=num**p
def sum_of_digits(num,p):

a=num**p

l=[int(i) for i in str(a)]

print(l)

for x in range (len(l)):

t=t+l[x]

print (“sum =”,t)







Follow me on Twitter..



Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s