In this article, we will learn about the solution to the problem statement given below −
Problem statement
Given a number input n, the task is to Find the sum of odd factors of a number.
Here we first need to eliminate all the even factors.
To remove all even factors, we repeatedly divide n till it is divisible by 2. After this step, we only get the odd factors of the number.
Below is the implementation −
Example
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))
Output
41
All the variables are declared in the global frame as shown in the figure given below −
Conclusion
In this article, we learnt about the approach to Find sum of odd factors of a number