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

Introduction

The task is to find and print sum of even factors of the give number.

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

Program

import math
def findsum(ip_num) :
#No even factor for odd input
    if (ip_num % 2 != 0) :
        return 0
    op_sum = 1
    for num in range(2, int(math.sqrt(ip_num)) + 1) :
        i = 0
        temp = 1
        temp1 = 1
        while (ip_num % num == 0) :
            i = i + 1
            ip_num = ip_num // num
            if (num == 2 and i == 1) :
                temp = 0
            temp1 = temp1 * num
            temp = temp + temp1       
        op_sum = op_sum * temp
#If n is prime
    if (ip_num >= 2) :
        op_sum = op_sum * (1 + ip_num)
    return op_sum
ip_num = int(input("Enter the number: "))
print("The sum of even factors of given number is: ",findsum(ip_num))

Output

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

Explanation

If the given number is odd there is no even factor of it. For example, let us take 27, the factors of 27 are 1, 3, 9, 27. There is no even factor of 27 so we return 0.

We can write the number as

(n1p1 ) * (n2p2 )…… (nnpn ) or (1+ n1+n12+n13+…..n1p1) * (1+ n2+n22+n23+…..n1p2) *……. *(1+ nn+nn2+n13+…..nnpn). To get the sum of even number we need to ignore n0 value (i.e. 1).

 

Leave a comment

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