数の奇数因子の合計を見つけるためのC++プログラム
正の整数で与えられ、タスクは、数値の奇数因子を生成し、与えられた奇数因子の合計を見つけることです。
例
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に設定し続けます
- 合計を合計+温度に設定
- 最終的な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
-
数の偶数因子の合計を見つけるためのPythonプログラム
この記事では、以下に示す問題ステートメントの解決策について学習します- 問題の説明 数値入力nが与えられた場合、タスクは数値の偶数因子の合計を見つけることです。 ここでは、最初にすべての奇妙な要因を排除する必要があります。 入力された数値が奇数の場合、偶数の因数はなく、直接ゼロを返します。それ以外の場合は、以下のコードのアプローチに従います。 以下は実装です- 例 import math # Returns sum of all even factors of n. def sumofFactors(n) : # If n is odd &nbs
-
数の因子の最小合計を見つけるためのPythonプログラム
この記事では、以下に示す問題ステートメントの解決策について学習します- 問題の説明 入力された数値を指定して、指定された数値の因子の最小合計を求めます。 ここでは、すべての因子とそれに対応する合計を計算し、それらの中から最小値を見つけます。 したがって、数の積の最小合計を見つけるために、積の素因数の合計を見つけます。 これが問題の反復実装です- 例 #iterative approach def findMinSum(num): sum_ = 0 # Find factors of number and add to the sum