用 C 編寫訂婚數?
訂婚數是一對兩個數,其除數的和再加一個值與另一個數相等。
例如,(a, b) 為一對訂婚數,當 s(a) = b + 1 且 s(b) = a + 1 時,其中 s(b) 為 b 的商和:一個等效條件是 σ(a) = σ(b) = a + b + 1,其中 σ 表示除數和函式。
第一幾對訂婚數是:(48, 75)、(140, 195)、(1050, 1925)、(1575, 1648)、(2024, 2295)、(5775, 6128)。
所有已知的訂婚數對都具有相反的奇偶性。任何奇偶性相同的對都必須大於 1010。
演算法
Step 1: Find the sum of all divisors for both numbers. Step 2: Finally check if the sum of the divisors of number added by one is equal to the other number or not. Step 3: If yes, it is a Betrothed number and otherwise not.
Input:a = 48 b = 75 Output: 48 and 75 are Betrothed numbers
說明
48 的除數:1、2、3、4、6、8、12、16、24。其和為 76。
75 的除數:1、3、5、15、25。其和為 49。
使用 for 迴圈,從 1 到 a-1 檢查每個數。
在迴圈中為每個數檢查其是否可以除以數 a。如果是,則將此數新增到 aDivisorSum。在迴圈完成後, aDivisorSum 包含 a 的所有除數的和。
以同樣的方式,求出第二個數的所有除數的和,並將其儲存在 bDivisorSum 中。
現在檢查一個數的除數和是否等於另一個數加上 1。如果是,則列印兩者均為訂婚數。否則,它們不是。
示例
#include <stdio.h>
int main() {
int i;
int a,b;
int aDivisorSum = 0;
int bDivisorSum = 0;
a=48 ;
b=75 ;
for( i = 1; i < a; i++) {
if(a % i == 0) {
aDivisorSum = aDivisorSum + i;
}
}
for( i = 1; i < b; i++) {
if(b % i == 0) {
bDivisorSum = bDivisorSum + i;
}
}
if(( a+1== bDivisorSum) && (b+1 == aDivisorSum)) {
printf("%d and %d are Betrothed numbers
",a,b);
} else {
printf("%d and %d are not Betrothed numbers
",a,b);
}
}輸出
48 and 75 are not Betrothed numbers
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP