How to Find Sum of Odd Factors from a Given Number in Python

In this tutorial, we will program 'How to Find the Sum of Odd Factors of a Given Number in Python.' We will learn how to find and calculate the sum of all odd factors based on the given number. The objective is to automatically calculate the sum of the odd factors of the given number. I will provide a sample program to demonstrate the actual coding process in this tutorial.

This topic is very easy to understand. Just follow the instructions I provide, and you will be able to do it yourself with ease. The program I will show you covers the basics of programming for calculating the sum of all odd factors. I will do my best to provide you with a simple method for displaying the sum of these odd factors. So, let's start with the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Creating Main Function

This is the main function of the application. The following code will display a simple GUI in terminal console that will display program. To do this, simply copy and paste these blocks of code into the IDLE text editor.
  1. import math
  2.  
  3. def sumofoddFactors( n ):
  4.        
  5.  
  6.         res = 1
  7.  
  8.         while n % 2 == 0:
  9.                 n = n // 2
  10.        
  11.         for i in range(3, int(math.sqrt(n) + 1)):
  12.                
  13.  
  14.                 count = 0
  15.                 curr_sum = 1
  16.                 curr_term = 1
  17.                 while n % i == 0:
  18.                         count+=1
  19.                        
  20.                         n = n // i
  21.                         curr_term *= i
  22.                         curr_sum += curr_term
  23.                
  24.                 res *= curr_sum
  25.  
  26.         if n >= 2:
  27.                 res *= (1 + n)
  28.        
  29.         return res
  30.  
  31.  
  32. ret = False
  33.  
  34.  
  35. while True:
  36.         print("\n================= Find Sum of Odd Factors from a Given Number =================\n\n")
  37.  
  38.  
  39.         n = int(input("Enter a number: "))
  40.  
  41.  
  42.         print("The sum of odd number is: ",sumofoddFactors(n))
  43.  
  44.         opt = input("\nDo you want to try again?(yes/no): ")
  45.  
  46.         if opt.lower() == 'yes':
  47.                 ret=False
  48.         elif opt.lower() == 'no':
  49.                 ret=True
  50.                 print("Exiting program....")
  51.  
  52.         else:
  53.                 print("Please enter yes/no:")
  54.                 break
  55.  
  56.         if ret == False:
  57.                 continue

This Python script calculates the sum of odd factors of a given number. It first removes even factors, then computes the sum of factors for each odd divisor up to the square root of the number. The program repeatedly prompts the user to input a number, displays the sum of its odd factors, and asks if they want to try again. The loop continues until the user chooses to exit.

Output:

There you have it we successfully created How to Find Sum of Odd Factors from a Given Number in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment