![]() |
VOOZH | about |
Given a number n, the task is to find the sum of all its odd factors. Odd factors are the divisors of n that are not divisible by 2. For example:
Input: n = 30
Output: 24
Odd divisors -> 1, 3, 5, 15 -> Sum = 24Input: n = 18
Output: 13
Odd divisors -> 1, 3, 9 -> Sum = 13
Lets explore different methods to find sum of odd factors of a number in python.
In this method, we use the prime factorization of n and ignore even factors. We repeatedly divide n by 2 to remove all even factors, then calculate the sum of odd divisors using powers of remaining primes.
24
Explanation:
Here, we reduce the number of iterations by checking divisors only up to √n. For each divisor i, both i and n/i are considered if they are odd.
24
Explanation:
In this method, we loop through all numbers up to n and add only the odd divisors. It’s easy to understand but slower for large numbers.
24
Explanation:
Here, we use list comprehension to quickly collect all odd divisors and sum them directly. It’s concise and Pythonic but not as efficient as formula-based or paired divisor methods.
24
Explanation:
Please refer complete article on Find sum of odd factors of a number for more details!