Sum of divisors in python
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. Show My code:
Example if user inputed 20:
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:
Also you can use
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
Output
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.
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
OUTPUT
answered Oct 1, 2018 at 14:14
Albin PaulAlbin Paul 3,2332 gold badges13 silver badges28 bronze badges
Output:
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. https://en.wikipedia.org/wiki/Amicable_numbers answered May 20 at 21:27
1 Not the answer you're looking for? Browse other questions tagged python or ask your own question.How do you find the sum of the divisors?∑ d ∣ n d = ∏ i = 1 k p i m i + 1 - 1 p i - 1 . If we want only proper divisors, we should not include n in the sum, so we obtain the formula for proper divisors by subtracting n from our formula. (24−12−1)(33−13−1)(53−15−1)=15⋅26⋅1242⋅4=6045.
...
Proof.. How do you find the divisors of a number in Python while loop?Explanation :. Initialize one variable i as 1 at the start of this method.. Using one while loop, iterate till i is less than n+1.. Check if the current value is a divisor of n or not. If yes, print out the value.. Increment the value of i.. What is the sum of divisors of 12?What is the Sum of all the Factors of 12? All the factors of 12 are 1, 2, 3, 4, 6, 12. Therefore, the sum of all these factors is 1 + 2 + 3 + 4 + 6 + 12 = 28.
|