Table of Contents
Introduction
The task is to find and print the sum of odd factors of the given number.
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
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.
0 Comments