In my program, it is supposed to ask the user for a number, then displays all the divisors possible, but in the end, it has to display the sum of all the divisors. I'm having trouble with the last part and would appreciate some help.
My code:
prompt = int[input["Enter an interger: "]]
print["The divisors of the integer you entered are: "]
for i in range[1, prompt+1]:
if[prompt%i==0]:
print[i]
print[f"]
Example if user inputed 20:
The divisors of the integer you entered are:
1
2
4
5
10
20
The sum of the divisors is: 42
Welbog
58.2k8 gold badges112 silver badges120 bronze badges
asked Oct 1, 2018 at 14:07
3
Just a simple modification of your code can do the trick. like:
prompt = int[input["Enter an interger: "]]
print["The divisors of the integer you entered are: "]
divisor_sum = 0 #updated line
for i in range[1, prompt+1]:
if[prompt%i==0]:
print[i]
divisor_sum+=i #calculate sum of all divisors
print["The sum of divisors " + str[divisor_sum]] #print the sum of divisors
Also you can use list comprehension
to make your program shorter and smarter, like:
prompt = int[input["Enter an interger: "]]
divisors = [i for i in range[1,prompt+1] if prompt%i==0]
divisor_sum = sum[divisors]
print["The divisors of the integer you entered are: "]
for i in divisors:
print[i]
print["The sum of divisors " + str[divisor_sum]]
answered Oct 1, 2018 at 14:10
Taohidul IslamTaohidul Islam
5,1003 gold badges23 silver badges38 bronze badges
You just need a variable to store the sum. I have used
s
. Rest all code is perfectly fine except print[f"]
. It is unused and gives syntax error due to incomplete "
. Moreover, f
is not defined
prompt = int[input["Enter an interger: "]]
s=0
print["The divisors of the integer you entered are: "]
for i in range[1, prompt+1]:
if[prompt%i==0]:
s += i
print[i]
print ["The sum of the divisors is: %d" %s]
Output
Enter an interger: 20
The divisors of the integer you entered are:
1
2
4
5
10
20
The sum of the divisors is: 42
answered Oct 1, 2018 at 14:09
SheldoreSheldore
35.9k6 gold badges45 silver badges60 bronze badges
Another approach is storing the list of valid divisors in a container of some sorts. In this case the appropriate "container" is a list. This has the advantage that you store the divisors for later use.
prompt = int[input["Enter an interger: "]]
divisors = []
print["The divisors of the integer you entered are: "]
for i in range[1, prompt+1]:
if[prompt%i==0]:
print[i]
divisors.append[i]
print["The sum of divisors " + sum[divisors]] #print the sum of divisors
# max[divisors]
# min[divisors]
# etc...
answered Oct 1, 2018 at 14:17
redactedredacted
3,6396 gold badges23 silver badges38 bronze badges
Well, This question reminded me about a question i solved in project Euler ,and i did it in O[sqrt[n]] complexity .
if you consider 9 ,
We dont need to consider till 9 to get all factors . We just need to consider till 3 if we have X as divisor then prompt/X is also a divisor . With this property you could make the algorithm more efficient
import time
from math import sqrt
prompt = int[input["Enter an interger: "]]
start =time.time[]
print["The divisors of the integer you entered are: "]
sumofdivisors=0
for divisor in range[1, int[sqrt[prompt]]+1]:
if[prompt%divisor==0]:
sumofdivisors+=divisor
sumofdivisors+=[prompt/divisor]
if [divisor==prompt/divisor]:
sumofdivisors-=divisor
print[divisor]
else:
print[divisor]
print[prompt/divisor]
print["sum is",sumofdivisors]
end=time.time[]
print["time taken is",end-start]
OUTPUT
Enter an interger: 8
The divisors of the integer you entered are:
1
8.0
2
4.0
sum is 15.0
time took = 0.0002665519714355469
answered Oct 1, 2018 at 14:14
Albin PaulAlbin Paul
3,2332 gold badges13 silver badges28 bronze badges
prompt = int[input["Enter an interger: "]]
print["The divisors of the integer you entered are: "]
total= 0
for i in range[1, prompt+1]:
if[prompt%i==0]:
print[i]
total+= i
print["The sum of the divisors is:{}".format[total]]
Output:
Enter an interger: 20
The divisors of the integer you entered are:
1
2
4
5
10
20
The sum of the divisors is:42
answered Oct 1, 2018 at 14:19
Mathematically speaking, when you are summing the divisors of a number you do not include the number in question. the +1 normally added the range function is not needed, and will produce in incorrect result. The following Wikipedia article on amicable pairs can be used as a reference.
//en.wikipedia.org/wiki/Amicable_numbers
answered May 20 at 21:27
1