Write a python program to find sum of odd factors of a number with the output.
How to find sum of even factors of a number in Python?
Copy the below python program and execute it to see the program output. I have executed it shared the program output after the program.
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 | import math # Returns sum of all even factors of n. def sumofFactors(n) : # If n is odd if (n % 2 != 0) : return 0 #all prime factors res = 1 for i in range(2, (int)(math.sqrt(n)) + 1) : count = 0 curr_sum = 1 curr_term = 1 while (n % i == 0) : count= count + 1 n = n // i # here we remove the 2^0 that is 1. All other factors if (i == 2 and count == 1) : curr_sum = 0 curr_term = curr_term * i curr_sum = curr_sum + curr_term res = res * curr_sum # if n is prime number if (n >= 2) : res = res * (1 + n) return res # main n = 20 print(sumofFactors(n)) |
Program Output
1 | 36 |
That’s all, we hope this program helped you!
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.