양의 정수가 주어지고 작업은 숫자의 홀수 요소를 생성하고 주어진 홀수 요소의 합을 찾는 것입니다.
예시
Input-: number = 20 Output-: sum of odd factors is: 6 Input-: number = 18 Output-: sum of odd factors is: 13

따라서 결과 =1 + 5 =6
아래 프로그램에서 사용된 접근 방식은 다음과 같습니다. -
- 해당 숫자의 홀수 인수의 합을 계산하는 숫자를 입력하세요.
- 0과 2는 모두 짝수이므로 무시하고 숫자 1은 홀수이므로 저장
- 3에서 숫자의 제곱근까지 루프 시작
- 숫자 % i가 0을 반환할 때까지 순회하고 숫자를 i 값으로 계속 나눕니다.
- 루프에서 임시 변수의 값을 temp =temp * i로 계속 설정
- 총계를 total + temp로 설정
- 최종 res 변수의 값을 반환하고 결과를 출력합니다.
알고리즘
START Step 1-> Declare function to calculate sum of odd factors int sum(int num) declare int res = 1 Loop While(num % 2 == 0) set num = num / 2 End Loop For int i = 3 and i <= sqrt(num) and i++ declare int count = 0 and total = 1 declare int temp = 1 Loop while (num % i == 0) count++ set num = num / i set temp *= i set total += temp End set res = res*total End IF (num >= 2) set res *= (1 + num) End return res Step 2-> In main() Declare int num = 20 call sum(num) STOP
예시
#include <bits/stdc++.h>
using namespace std;
//calculate sum of odd factors
int sum(int num) {
int res = 1;
while (num % 2 == 0)
num = num / 2;
for (int i = 3; i <= sqrt(num); i++) {
int count = 0, total = 1 ;
int temp = 1;
while (num % i == 0) {
count++;
num = num / i;
temp *= i;
total += temp;
}
res = res*total;
}
if (num >= 2)
res *= (1 + num);
return res;
}
int main() {
int num = 20;
cout<<"sum of odd factors is : ";
cout <<sum(num);
return 0;
} 출력
sum of odd factors is : 6