C++ 中的 aliquot 和?


這裡,我們將瞭解 aliquot 和是什麼?n 的 aliquot 和是指 n 的所有真因數之和,但 n 自身除外。例如,如果該數為 20,則真因數為 (1, 2, 4, 5, 10)。因此,aliquot 和為 22。

一個有趣的事實是,如果一個數的 aliquot 和等於該數本身,則這個數就是完全數。例如,6。其因數為 (1, 2, 3)。aliquot 和為 1+2+3=6。

讓我們看看如何使用以下演算法獲得 aliquot 和。

演算法

getAliquotSum(n)

begin
   sum := 0
   for i in range 1 to n, do
      if n is divisible by i, then
         sum := sum + i
      end if
   done
   return sum.
end

示例

 Live Demo

#include <iostream>
using namespace std;
int getAliquotSum(int n) {
   int sum = 0;
   for(int i = 1; i<n; i++) {
      if(n %i ==0) {
         sum += i;
      }
   }
   return sum;
}
int main() {
   int n;
   cout << "Enter a number to get Aliquot sum: ";
   cin >> n;
   cout << "The Aliquot sum of " << n << " is " << getAliquotSum(n);
}

輸出

Enter a number to get Aliquot sum: 20
The Aliquot sum of 20 is 22

更新於: 30-Jul-2019

199 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告