- Related Questions & Answers
- C++ Program to find sum of even factors of a number?
- To find sum of even factors of a number in C++ Program?
- Python Program for Find sum of even factors of a number
- Find sum of even factors of a number in Python Program
- Java Program to Find sum of even factors of a number
- Find sum of odd factors of a number using C++.
- Find minimum sum of factors of 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
- C Program to Find the minimum sum of factors of a number?
- Python Program for Find sum of odd factors of a number
- Java Program to find minimum sum of factors of a number
- Find number of subarrays with even sum in C++
- Python Program for Find minimum sum of factors of number
- How to Find Factors of Number using Python?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this section, we will see how we can get the sum of all even prime factors of a number in an efficient way. There is a number say n = 480, we have to get all factors of this. The prime factors of 480 are 2, 2, 2, 2, 2, 3, 5. The sum of all even factors is 2+2+2+2+2 = 10. To solve this problem, we have to follow this rule −

- When the number is divisible by 2, add them into the sum, and divide the number by 2 repeatedly.
- Now the number must be odd. So we will not find any factors which are even. Then simply ignore those factors.

Let us see the algorithm to get a better idea.

printPrimeFactors(n): begin sum := 0 while n is divisible by 2, do sum := sum + 2 n := n / 2 done end

#include<iostream> using namespace std; int sumEvenFactors(int n){ int i, sum = 0; while(n % 2 == 0){ sum += 2; n = n/2; //reduce n by dividing this by 2 } return sum; } main() { int n; cout << "Enter a number: "; cin >> n; cout << "Sum of all even prime factors: "<< sumEvenFactors(n); }

Enter a number: 480 Sum of all even prime factors: 10

Advertisements