# Python Program for Finding the Sum of Odd Factors of a Number

In this blog post, we will delve into a Python program that calculates the sum of the odd factors of a given number. We’ll discuss the algorithm used and provide a complete Python code example with detailed explanations.

## Problem Statement:

Given a positive integer `n`, the task is to find and calculate the sum of its odd factors.

## Algorithm:

The algorithm to find the sum of odd factors of a number involves the following steps:

1. Iterate through each number from 1 to the given number `n`.
2. Check if the current number is a factor of `n`.
3. If the current number is a factor and is odd, add it to the sum.
4. After iterating through all numbers, return the sum as the result.

The efficiency of the algorithm can be improved by only iterating up to the square root of `n` for factor checking. Additionally, we only need to check odd numbers as factors since even factors won’t contribute to the sum of odd factors.

## Python Program:

``````def sum_of_odd_factors(number):
# Initialize the sum of odd factors
odd_factors_sum = 0

# Iterate through each number from 1 to the square root of the given number
for i in range(1, int(number**0.5) + 1):
# Check if i is a factor of the given number
if number % i == 0:
# If i is odd, add it to the sum
if i % 2 == 1:
odd_factors_sum += i

# Check if the other factor is different from i (avoids counting twice for perfect squares)
if (number // i) != i and (number // i) % 2 == 1:
odd_factors_sum += number // i

return odd_factors_sum

# Example
input_number = 18

# Calculate the sum of odd factors
result_sum = sum_of_odd_factors(input_number)

# Display the result
print(f"Sum of odd factors of {input_number}: {result_sum}")
``````

## Output:

``````Sum of odd factors of 18: 7
``````

In this example, the program calculates and prints the sum of the odd factors of the number 18.

Feel free to run this Python program in your environment to observe the output. If you have any questions or need further clarification, don’t hesitate to ask!