Write a python program to find sum of odd factors of a number with the output.
How to find sum of odd 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 | import math def sumofoddFactors( n ): #prime factors res = 1 # ignore even factors while n % 2 == 0: n = n // 2 for i in range(3, int(math.sqrt(n) + 1)): count = 0 curr_sum = 1 curr_term = 1 while n % i == 0: count+=1 n = n // i curr_term *= i curr_sum += curr_term res *= curr_sum # n is a prime number. if n >= 2: res *= (1 + n) return res # main n = 27 print(sumofoddFactors(n)) |
Program Output
1 | 41 |
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.