Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Betrothed numbers in C Program?
Betrothed numbers are a special pair of numbers where the sum of proper divisors of one number equals one more than the other number. These mathematical curiosities demonstrate interesting relationships between divisibility and number theory.
Syntax
int sumOfProperDivisors(int n); void findBetrothedPairs(int limit);
Understanding Betrothed Numbers
For example, the pair (48, 75) are betrothed numbers −
- Proper divisors of 48: {1, 2, 3, 4, 6, 8, 12, 16, 24}, sum = 76
- Proper divisors of 75: {1, 3, 5, 15, 25}, sum = 49
- Since 76 = 75 + 1 and 49 = 48 + 1, they form a betrothed pair
Algorithm
The algorithm finds all betrothed pairs up to a given limit −
for each number from 1 to limit:
calculate sum of proper divisors
if sum > number:
check if (sum-1, number) forms betrothed pair
if yes, output the pair
Example: Finding Betrothed Pairs
#include <stdio.h>
int sumOfProperDivisors(int n) {
int sum = 1; /* 1 is always a proper divisor */
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
sum += i;
if (i * i != n) { /* avoid counting square root twice */
sum += n / i;
}
}
}
return sum;
}
void findBetrothedPairs(int limit) {
for (int num = 1; num < limit; num++) {
int sum1 = sumOfProperDivisors(num);
if (sum1 > num) {
int num2 = sum1 - 1;
int sum2 = sumOfProperDivisors(num2);
if (sum2 == num + 1) {
printf("(%d, %d)<br>", num, num2);
}
}
}
}
int main() {
int limit = 300;
printf("Betrothed pairs up to %d:<br>", limit);
findBetrothedPairs(limit);
return 0;
}
Betrothed pairs up to 300: (48, 75) (140, 195)
How It Works
The program works by −
- For each number, calculate the sum of its proper divisors (excluding the number itself)
- If the sum is greater than the original number, check if they form a betrothed pair
- Verify that the sum of proper divisors of (sum - 1) equals (original number + 1)
Key Points
- Proper divisors exclude the number itself but include 1
- The algorithm optimizes divisor finding by checking only up to ?n
- Time complexity: O(n?n) where n is the limit
Conclusion
Betrothed numbers showcase fascinating mathematical relationships. The C implementation efficiently finds these pairs by calculating proper divisor sums and verifying the betrothed condition for each candidate pair.
Advertisements
