- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python Program for Find sum of even factors of a number
In this article, we will learn about the solution to the problem statement given below −
Given a number input n, the task is to Find the sum of even factors of a number.
Here we first need to eliminate all the odd factors.
If the number input is odd it has no even factors therefor return zero directly , otherwise, we will follow the approach in the code below
Below is the implementation −
import math # Returns sum of all even factors of n. def sumofFactors(n) : # If n is odd if (n % 2 != 0) : return 0 #all prime factors res = 1 for i in range(2, (int)(math.sqrt(n)) + 1) : 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 # if n is prime number if (n >= 2) : res = res * (1 + n) return res # main n = 20 print(sumofFactors(n))
All the variables are declared in the global frame as shown in the figure given below −
In this article, we learned about the approach to Find the sum of even factors of a number
- Related Articles
- Find sum of even factors of a number in Python Program
- C++ Program to find sum of even factors of a number?
- Java Program to Find sum of even factors of a number
- Python Program for Find sum of odd factors of a number
- Python Program for Find minimum sum of factors of number
- To find sum of even factors of a number in C++ Program?
- Find sum of even factors of a number using C++.
- C Program for Find sum of odd factors of a number?
- C++ program for Find sum of odd factors of a number
- Java Program to find minimum sum of factors of a number
- C Program to Find the minimum sum of factors of a number?
- Python Program for Product of unique prime factors of a number
- Find sum of odd factors of a number using C++.
- Find minimum sum of factors of number using C++.
- Python Program for Efficient program to print all prime factors of a given number