โปรแกรมนี้ใช้หาตัวประกอบคู่ทั้งหมดและคำนวณผลรวมของตัวประกอบคู่เหล่านั้น แล้วแสดงเป็นผลลัพธ์
ตัวอย่าง −
Input : 30 Even dividers : 2+6+10+30 = 48 Output : 48
สำหรับนี้ เราจะหาตัวประกอบทั้งหมด หาตัวคู่ของพวกเขา และหาผลรวม
อีกวิธี เราจะใช้สูตรหาผลรวมของตัวประกอบโดยใช้ตัวประกอบเฉพาะ
Sum of divisors = (1 + d11 + d12 ... d1a1) * (1 + d21 + d22 ... d2a2) * ... * (1 + dk1 + dk2 ... dkak) ที่นี่ di = ตัวประกอบเฉพาะ ; ai = กำลังของ di
เราต้องการเฉพาะตัวประกอบคู่ ดังนั้น ถ้าจำนวนเป็นคี่ จะไม่มีตัวประกอบคู่ เราจะแสดงผล 0 ในกรณีนั้น
ตัวอย่าง
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int n = 12;
int m = n;
if (n % 2 != 0)
cout << "The sum of all even factors of " << n << " is " << 0;
int evfac = 1;
for (int i = 2; i <= sqrt(n); i++) {
int count = 0, curr_sum = 1, curr_term = 1;
while (n % i == 0) {
count++;
n = n / i;
if (i == 2 && count == 1)
curr_sum = 0;
curr_term *= i;
curr_sum += curr_term;
}
evfac *= curr_sum;
}
if (n >= 2)
evfac *= (1 + n);
cout << "The sum of all even factors of " << m <> " is " <> evfac;
return 0;
}
ผลลัพธ์
The sum of all even factors of 12 is 24