Java Program to Find sum of even factors of a number Last Updated : 03 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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. java // Formula based Java program to // find sum of all divisors of n. import java.util.*; import java.lang.*; public class GfG { // Returns sum of all factors of n. public static int sumofFactors(int n) { // If n is odd, then there // are no even factors. if (n % 2 != 0) return 0; // Traversing through all prime // factors. int res = 1; for (int i = 2; i <= Math.sqrt(n); i++) { int count = 0, curr_sum = 1; int curr_term = 1; // While i divides n, print i and // divide n while (n % i == 0) { count++; n = n / i; // here we remove the 2^0 that // is 1. All other factors if (i == 2 && count == 1) curr_sum = 0; curr_term *= i; curr_sum += curr_term; } res *= curr_sum; } // This condition is to handle the // case when n is a prime number. if (n >= 2) res *= (1 + n); return res; } // Driver function public static void main(String argc[]) { int n = 18; System.out.println(sumofFactors(n)); } } /* This code is contributed by Sagar Shukla */ Output:26 Please refer complete article on Find sum of even factors of a number for more details! Comment More infoAdvertise with us Next Article Java Program to Find sum of even factors of a number kartik Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Java Program to Find minimum sum of factors of number Given a number, find minimum sum of its factors. Examples: Input : 12 Output : 7 Explanation: Following are different ways to factorize 12 and sum of factors in different ways. 12 = 12 * 1 = 12 + 1 = 13 12 = 2 * 6 = 2 + 6 = 8 12 = 3 * 4 = 3 + 4 = 7 12 = 2 * 2 * 3 = 2 + 2 + 3 = 7 Therefore minimum su 2 min read Java program for Find sum of odd factors of a number Write a java program for a given number n, the task is to find the odd factor sum. Examples: Input: n = 30Output: 24Explanation: Odd dividers sum 1 + 3 + 5 + 15 = 24 Input: 18Output: 13Explanation: Odd dividers sum 1 + 3 + 9 = 13 Java program for Find sum of odd factors of a number using Prime facto 3 min read Java 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 : 3 min read Java Program to Find the Sum of First N Odd & Even Numbers When any number which ends with 0,2,4,6,8 is divided by 2 that is an even number. And when any number ends with 1,3,5,7,9 is not divided by two is an odd number. Example: Input : 8 Output: Sum of First 8 Even numbers = 72 Sum of First 8 Odd numbers = 64Approach #1: Iterative Create two variables eve 3 min read Java Program to Find Sum of Fibonacci Series Numbers of First N Even Indexes For a given positive integer N, the purpose is to find the value of F2 + F4 + F6 +â¦â¦â¦+ F2n till N number. Where Fi indicates the i'th Fibonacci number. The Fibonacci Series is the numbers in the below-given integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, â¦â¦Examples: Input: n = 4 Output: 3 4 min read Java Program to Check if a Given Number is Perfect Number A number is said to be a perfect number if the sum of its proper divisors ( i.e. all positive divisors excluding the number itself )is equal to that number itself. Aliquot sum is the sum of divisors of a number, excluding the number itself. Hence, a number is a perfect number only if it is equal to 5 min read Java Program to Check if a Given Integer is Odd or Even A number that is divisible by 2 and generates a remainder of 0 is called an even number. All the numbers ending with 0, 2, 4, 6, and 8 are even numbers. On the other hand, number that is not divisible by 2 and generates a remainder of 1 is called an odd number. All the numbers ending with 1, 3, 5,7, 7 min read Perfect Number Program in Java Using While Loop The number which is equal to the sum of its divisors is called a perfect number. Read the entered long number, assigned to the long variable n. While loop iterates until the condition (i<=n/2) is false. If the remainder of n/i=0 then add i value to the sum and increase the i value. After all the 2 min read Sum of Array Divisible by Size with Even and Odd Numbers at Odd and Even Index in Java Given an array arr[] of size N, Convert the given array as even numbers in the array occurs at the odd index, odd numbers in the array occur at the even index, and whether the sum of the numbers in an array is divisible by the size of an array. If the array follows all the properties then an array i 3 min read Implement Filter Function using Reduce in Java 8 Streams Many times, we need to perform operations where a stream reduces to a single resultant value, for example, maximum, minimum, sum, product, etc. Reducing is the repeated process of combining all elements. reduce operation applies a binary operator to each element in the stream where the first argumen 2 min read Like