숫자의 짝수 인수의 합을 구하려면 Java 코드는 다음과 같습니다. -
예시
import java.util.*;
import java.lang.*;
public class Demo{
public static int factor_sum(int num){
if (num % 2 != 0)
return 0;
int result = 1;
for (int i = 2; i <= Math.sqrt(num); i++){
int count = 0, current_sum = 1;
int current_term = 1;
while (num % i == 0){
count++;
num = num / i;
if (i == 2 && count == 1)
current_sum = 0;
current_term *= i;
current_sum += current_term;
}
result *= current_sum;
}
if (num >= 2)
result *= (1 + num);
return result;
}
public static void main(String argc[]){
int num = 36;
System.out.println("The sum of even factors of the number is ");
System.out.println(factor_sum(num));
}
} 출력
The sum of even factors of the number is 78
Demo라는 클래스에는 'factor_sum'이라는 함수가 포함되어 있습니다. 이 함수는 숫자의 요소를 찾고 짝수 요소를 더하고 이 값을 출력으로 반환합니다. 주 함수에서는 짝수 인수를 찾아야 하는 숫자가 정의되고 이 숫자에서 함수가 호출됩니다. 해당 메시지가 콘솔에 표시됩니다.