Program to find sum of odd factors of the given number using Python

Introduction

The task is to find and print the sum of odd factors of the given number.

Program to find sum of odd factors of the given number using Python

Program

import math

def findsum( ip_num ):

    op_sum = 1

    while ip_num % 2 == 0:

        ip_num = ip_num // 2

    for num in range(3, int(math.sqrt(ip_num) + 1)):

        i = 0

        temp = 1

        temp1= 1

        while ip_num % num == 0:

            i += 1

            ip_num = ip_num // num

            temp1 *= num

            temp += temp1

        op_sum *= temp

#If n is prime

    if ip_num >= 2:

        op_sum *= (1 + ip_num)

    return op_sum

ip_num = int(input("Enter the number: "))

print("The sum of odd factors of given number is: ",findsum(ip_num))

Output

Program to find sum of odd factors of the given number using Python Output

Explanation

A number can be written in the form

(n1p1 ) * (n2p2 )…… (nnpn ) or (1+ n1+n12+n13+…..n1p1) * (1+n2+n22+n23+…..n1p2) *……. *(1+ nn+nn2+n13+…..nnpn).

 

To get the sum of odd numbers we need to ignore the even values (divide the number by 2 and check if num%2 == 0)  and add the odd numbers.

 

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.