- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program for Find sum of odd factors of a number
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
Advertisements