The Wayback Machine - https://web.archive.org/web/20241123012056/https://www.geeksforgeeks.org/python-program-for-find-sum-of-even-factors-of-a-number/
Open In App

Python Program for Find sum of even factors of a number

Last Updated : 08 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a number n, the task is to find the even factor sum of a number. Examples:

Input : 30
Output : 48
Even dividers sum 2 + 6 + 10 + 30 = 48

Input : 18
Output : 26
Even dividers sum 2 + 6 + 18 = 26

Let p1, p2, … pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (p1a1)*(p2a2)* … (pkak).

Sum of divisors = (1 + p1 + p12 ... p1a1) * 
                  (1 + p2 + p22 ... p2a2) *
                  ...........................
                  (1 + pk + pk2 ... pkak) 

If number is odd, then there are no even factors, so we simply return 0. If number is even, we use above formula. We only need to ignore 20. All other terms multiply to produce even factor sum. For example, consider n = 18. It can be written as 2132 and sum of all factors is (20 + 21)*(30 + 31 + 32). if we remove 20 then we get the Sum of even factors (2)*(1+3+32) = 26. To remove odd number in even factor, we ignore then 20 which is 1. After this step, we only get even factors. Note that 2 is the only even prime. 

python3




# Formula based Python3
# program to find sum
# of alldivisors of n.
import math
 
# Returns sum of all
# factors of n.
def sumofFactors(n) :
     
    # If n is odd, then
    # there are no even
    # factors.
    if (n % 2 != 0) :
        return 0
  
    # Traversing through
    # all prime factors.
    res = 1
    for i in range(2, (int)(math.sqrt(n)) + 1) :
         
        # While i divides n
        # print i and divide n
        count = 0
        curr_sum = 1
        curr_term = 1
        while (n % i == 0) :
            count= count + 1
  
            n = n // i
  
            # here we remove the
            # 2^0 that is 1. All
            # other factors
            if (i == 2 and count == 1) :
                curr_sum = 0
  
            curr_term = curr_term * i
            curr_sum = curr_sum + curr_term
         
        res = res * curr_sum
         
  
    # This condition is to
    # handle the case when
    # n is a prime number.
    if (n >= 2) :
        res = res * (1 + n)
  
    return res
 
 
# Driver code
n = 18
print(sumofFactors(n))
 
 
# This code is contributed by Nikita Tiwari.


Output

26

Method: Finding even factors sum of a given number using only for loop and if statements .

1. Iterate from the start range 1 to the given number to find the factors of a number using modulo division.

2. Finding even factors from the obtained factors by performing modulo division of a factor with 2. if the result of modulo division is equal to 0 then it should be considered as an even factor.

3. Adding all even factors and storing the result in s.

4. Print the sum of the even factors. 

Python3




# Python code
# To find the sum of even factors of a number
 
 
def evenfactorssum(n):
    s = 0
    for i in range(1, n+1):
        # finding factors of a given number
        if n % i == 0:
            # finding even factors of a given number
            if i % 2 == 0:
                # adding even factors of a given number
                s = s+i  # 2+6+10+30
                # printing the sum of even factors of a given number
    print(s)
 
 
# driver code
# input
n = 18
# the above input can also be given as
# n=int(input()) -> taking input from the user
evenfactorssum(n)
 
# this code is contributed by gangarajula laxmi


Output

26

Method: Using the list comprehension 

Python3




n=18
x=[i for i in range(1,n+1) if n%i==0 and i%2==0]
print(sum(x))


Output

26

Time Complexity: O(n), where n is length of x list.
Auxiliary Space: O(n), where n is number of elements in list x.

Method: Using lambda function 

Python3




n = 18
l = [i for i in range(1, n+1) if n % i == 0]
s = list(filter(lambda x: (x % 2 == 0), l))
print(sum(s))


Output

26

Method: Using enumerate function

Python3




n=18
x=[str(i) for i in range(1,n+1)]
s=[int(i) for i in x if n%int(i)==0 and int(i)%2==0]
print(sum(s))


Output

26

Please refer complete article on Find sum of even factors of a number for more details!



Similar Reads

Find sum of even factors of a number
Given a number n, the task is to find the even factor sum of a number.Examples: Input : 30 Output : 48 Even dividers sum 2 + 6 + 10 + 30 = 48 Input : 18 Output : 26 Even dividers sum 2 + 6 + 18 = 26 Recommended PracticeFind sum of even factors of a numberTry It! Prerequisite : Sum of factorsAs discussed in above mentioned previous post, sum of fact
8 min read
Maximum number of prime factors a number can have with exactly x factors
Given an integer X, denoting the number of factors of a positive integer N can have. The task is to find the maximum number of distinct prime factors the number N can have. Examples: Input: X = 9 Output: 2 Explanation: Some of the possible numbers having 9 factors are: 256: 1, 2, 4, 8, 16, 32, 64, 128, 256 Number of prime factors = 1 36: 1, 2, 3, 4
6 min read
Check if Sum of total number of factors from 1 to N is even or odd
Given an integer N. The task is to find check if the Sum of total number of factors from 1 to N is even or odd i.e., sum % 2 = 0 or 1. Examples: Input: N = 3Output: OddExplanation: 1 has 1 factor, 2 has 2 factors (1, 2) and 3 has 2 factors (1, 3), Hence, total number of factors is 5 which is odd. Input: N = 4Output: EvenExplanation: 1 has 1 factor,
15+ min read
Python Program for Find minimum sum of factors of number
Given a number, find minimum sum of its factors.Examples: Input : 12Output : 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3 = 2 + 2 + 3 = 7Therefore minimum sum is 7Input : 105Output : 15 C/C++ Code # Python program to
1 min read
Python Program for Find sum of odd factors of a number
Given a number n, the task is to find the odd factor sum. Examples: Input : n = 30 Output : 24 Odd dividers sum 1 + 3 + 5 + 15 = 24 Input : 18 Output : 13 Odd dividers sum 1 + 3 + 9 = 13 Let p1, p2, ... pk be prime factors of n. Let a1, a2, .. ak be highest powers of p1, p2, .. pk respectively that divide n, i.e., we can write n as n = (p1a1)*(p2a2
2 min read
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X. Examples: Input: X = 60, a[] = {2, 5, 10, 7, 17} Output: 2 5 10 Set of prime factors of 60: {2, 3, 5} Set of prime factors of 2: {2} Set of prime factors of 5: {5} Set of prime fa
13 min read
Sum of all even factors of numbers in the range [l, r]
Given a range [l, r], the task is to find the sum of all the even factors of the numbers from the given range.Examples: Input: l = 6, r = 8 Output: 22 factors(6) = 1, 2, 3, 6, evenfactors(6) = 2, 6 sumEvenFactors(6) = 2 + 6 = 8 factors(7) = 1, 7, No even factors factors(8) = 1, 2, 4, 8, evenfactors(8) = 2, 4, 8 sumEvenFactors(8) = 2 + 4 + 8 = 14 Th
15+ min read
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers
Given a number N (N > 6), the task is to print the prime factorization of a number Z, where Z is the product of all numbers ≤ N that are even and can be expressed as the product of two distinct prime numbers. Example: Input: N = 6Output: 2→1 3→1Explanation: 6 is the only number ≤ N, which is even and a product of two distinct prime numbers (2 an
7 min read
Python Program for Efficient program to print all prime factors of a given number
Given a number n, write an efficient function to print all prime factors of n. For example, if the input number is 12, then output should be "2 2 3". And if the input number is 315, then output should be "3 3 5 7". Following are the steps to find all prime factors. 1) While n is divisible by 2, print 2 and divide n by 2. 2) After step 1, n must be
6 min read
Number of elements with even factors in the given range
Given a range [n, m], the task is to find the number of elements that have even number of factors in the given range (n and m inclusive).Examples : Input: n = 5, m = 20 Output: 14 The numbers with even factors are 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20. Input: n = 5, m = 100 Output: 88 A Simple Solution is to loop through all numbers st
3 min read
Count ways to express even number ‘n’ as sum of even integers
Given an positive even integer 'n'. Count total number of ways to express ‘n’ as sum of even positive integers. Output the answer in modulo 109 + 7Examples: Input: 6 Output: 4 Explanation There are only four ways to write 6 as sum of even integers: 1. 2 + 2 + 2 2. 2 + 4 3. 4 + 2 4. 6 Input: 8 Output: 8 Approach is to find pattern or recursive funct
7 min read
Count of integers in a range which have even number of odd digits and odd number of even digits
Given a range [L, R], the task is to count the numbers which have even number of odd digits and odd number of even digits. For example, 8 has 1 even digit and 0 odd digit - Satisfies the condition since 1 is odd and 0 is even.545 has 1 even digit and 2 odd digits - Satisfies the condition since 1 is odd and 2 is even.4834 has 3 even digits and 1 od
11 min read
Find sum of odd factors of a number
Given a number n, the task is to find the odd factor sum.Examples : Input : n = 30Output : 24Odd dividers sum 1 + 3 + 5 + 15 = 24 Input : 18Output : 13Odd dividers sum 1 + 3 + 9 = 13 Approach :- Naive approach in o(n) time complexity The first approach involves iterating over all factors of the given number and checking if each factor is odd. If a
10 min read
Find minimum sum of factors of number
Given a number N, the task is to find minimum sum of its factors. Examples: Input: 12Output: 7Explanation: Following are different ways to factorize 12 andsum of factors in different ways.12 = 12 * 1 = 12 + 1 = 1312 = 2 * 6 = 2 + 6 = 812 = 3 * 4 = 3 + 4 = 712 = 2 * 2 * 3 = 2 + 2 + 3 = 7Therefore minimum sum is 7 Input: 105Output: 15 Recommended Pra
4 min read
Queries on sum of odd number digit sums of all the factors of a number
Given Q queries. Each query contain a positive integer n. The task is to output the sum of sum of odd number digit contained in all the divisors of n.Examples : Input : Q = 2, n1 = 10, n2 = 36 Output : 7 18 For Query1, Divisors of 10 are 1, 2, 5, 10. Sum of odd digits in 1 is 1, in 2 is 0, in 5 is 5, in 10 is 1. So, sum became 7.For Query 2, Diviso
9 min read
Program to find all Factors of a Number using recursion
Given a number N, the task is to print all the factors of N using recursion.Examples: Input: N = 16 Output: 1 2 4 8 16 Explanation: 1, 2, 4, 8, 16 are the factors of 16. A factor is a number which divides the number completely. Input: N = 8 Output: 1 2 4 8 Approach: The idea is to create a function that takes 2 arguments. The function is recursivel
3 min read
Number of factors of very large number N modulo M where M is any prime number
Given a large number N, the task is to find the total number of factors of the number N modulo M where M is any prime number. Examples: Input: N = 9699690, M = 17 Output: 1 Explanation: Total Number of factors of 9699690 is 256 and (256 % 17) = 1Input: N = 193748576239475639, M = 9 Output: 8 Explanation: Total Number of factors of 9699690 is 256 an
8 min read
Count pairs from an array with even product of count of distinct prime factors
Given two arrays A[] and B[] consisting of N and M integers respectively, the task is to count pairs (A[i], B[j]) such that the product of their count of distinct prime factors is even. Examples: Input: A[] = {1, 2, 3}, B[] = {4, 5, 6}, N = 3, M = 3Output: 2Explanation: Replacing all array elements with the count of their distinct prime factors mod
11 min read
Absolute difference between the count of odd and even factors of N
Given a positive integer N, the task is to find the absolute difference of the count of odd and even factors of N. Examples: Input: N = 12Output: 2Explanation: The even factors of 12 are {2, 4, 6, 12}. Therefore, the count is 4.The odd factors of 12 are {1, 3}. Therefore, the count is 2.Hence, the difference between their counts is (4 - 2) = 2. Inp
13 min read
Sum of even numbers at even position
Given an array of size n. The problem is to find the sum of numbers that are even and are at even index. Examples: Input : arr[] = {5, 6, 12, 1, 18, 8} Output : 30 Explanation: Here, n = 6 Now here are index and numbers as: index->arr[index] 0->5, 1->6, 2->12, 3->1, 4->18, 5->8 so, number which are even and are at even indices
11 min read
Absolute difference between sum of even elements at even indices & odd elements at odd indices in given Array
Given an array arr[] containing N elements, the task is to find the absolute difference between the sum of even elements at even indices & the count of odd elements at odd indices. Consider 1-based indexing Examples: Input: arr[] = {3, 4, 1, 5}Output: 0Explanation: Sum of even elements at even indices: 4 {4}Sum of odd elements at odd indices: 4
5 min read
Maximum sum of even indexed elements obtained by right shift on an even sized subarray
Given an array arr[], we need to find the maximum sum of the even indexed elements that can be obtained by performing right shift operation on any sub-array of even length by 1. Examples: Input: arr[] = {5, 1, 3, 4, 5, 6} Output: 15 Explanation: We can perform a right shift on index 2 to 5 then resulting array is: arr[] = {5, 1, 6, 3, 4, 5} Sum of
14 min read
Python program to find the sum of all even and odd digits of an integer list
The following article shows how given an integer list, we can produce the sum of all its odd and even digits. Input : test_list = [345, 893, 1948, 34, 2346] Output : Odd digit sum : 36 Even digit sum : 40 Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.Input : test_list = [345, 893] Output : Odd digit sum : 20 Even digit sum : 12 Ex
5 min read
Python Program to find Sum of Negative, Positive Even and Positive Odd numbers in a List
Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List. Examples: Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative numbers is -6 Sum of even positive numbers is 50
7 min read
Python Program for Number of elements with odd factors in given range
Given a range [n,m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output : 150 A Simple Solution is to loop through all numbe
2 min read
Python Program for Product of unique prime factors of a number
Given a number n, we need to find the product of all of its unique prime factors. Prime factors: It is basically a factor of the number that is a prime number itself. Examples: Input: num = 10 Output: Product is 10 Explanation: Here, the input number is 10 having only 2 prime factors and they are 5 and 2. And hence their product is 10. Input : num
3 min read
Find the nearest odd and even perfect squares of odd and even array elements respectively
Given an array arr[ ] of size N, the task for each array element is to print the nearest perfect square having same parity. Examples: Input: arr[ ] = {6, 3, 2, 15}Output: 4 1 4 9Explanation:The nearest even perfect square of arr[0] (= 6) is 4.The nearest odd perfect square of arr[1] (= 3) is 1.The nearest even perfect square of arr[2] (= 2) is 4The
5 min read
Average of even numbers till a given even number
Given an even number n, find the average of even numbers from 1 to n.Examples : Input : 10 Output : 6 Explanation: (2 + 4 + 6 + 8 + 10 )/5 = 30/5 = 6 Input : 100 Output : 51 Method 1 We can calculate average by adding each even numbers till n and then dividing sum by count. C/C++ Code // Program to find average of even numbers // till a given even
7 min read
Check if a number has an odd count of odd divisors and even count of even divisors
Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors. Examples: Input: N = 36Output: YesExplanation:Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even] Input: N = 28Output: No Naive Approach: The idea i
9 min read
Find four factors of N with maximum product and sum equal to N | Set-2
Given an integer [Tex]N [/Tex]. The task is to find all factors of N and print the product of four factors of N such that: Sum of the four factors is equal to N.Product of the four factors is maximum. If it is not possible to find 4 such factors then print "Not possible".Note: All the four factors can be equal to each other to maximize the product.
11 min read
Article Tags :
three90RightbarBannerImg