📜  求一个数的偶数因子之和

📅  最后修改于: 2021-04-27 23:22:19             🧑  作者: Mango

给定数字n,任务是找到数字的偶数因子和。
例子:

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

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

先决条件:因素总和
如以上提到的先前文章所述,数的因子之和为
令p1,p2,…pk为n的素因子。设a1,a2,.. ak分别是除以n的p1,p2,.. pk的最高幂,即,我们可以将n写成n =(p1 a1 )*(p2 a2 )*…(pk ak )

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

如果数字为奇数,则没有偶数因子,因此我们仅返回0。
如果数字是偶数,我们使用上面的公式。我们只需要忽略2 0 。所有其他项相乘以产生偶数因子之和。例如,考虑n =18。它可以写为2 1 3 2,并且所有因子的太阳为(2 0 + 2 1 )*(3 0 + 3 1 + 3 2 )。如果我们删除2 0,那么我们得到
偶数因子之和(2)*(1 + 3 + 3 2 )= 26。
要删除偶数因子中的奇数,我们将忽略2 0 whaich为1。在此步骤之后,我们仅获得偶数因子。请注意,2是唯一的偶数素数。
下面是上述方法的实现。

C++
// Formula based CPP program to find sum of all
// divisors of n.
#include 
using namespace std;
 
// Returns sum of all factors of n.
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 <= sqrt(n); i++) {
 
        // While i divides n, print i and divide n
        int count = 0, curr_sum = 1, curr_term = 1;
        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 code
int main()
{
    int n = 18;
    cout << sumofFactors(n);
    return 0;
}


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 */


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.


C#
// Formula based C# program to
// find sum of all divisors of n.
using System;
 
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 Code
    public static void Main() {
        int n = 18;
        Console.WriteLine(sumofFactors(n));
    }
     
}
 
// This code is contributed by vt_m


PHP
= 2)
        $res *= (1 + $n);
 
    return $res;
}
 
// Driver code
    $n = 18;
    echo sumofFactors($n);
 
// This code is contributed by mits
?>


Javascript


输出:

26