Amicable numbers in C
In this article, we will discuss the amicable numbers in C with its example, algorithm, and applications.
What is an amicable number?
A pair of numbers is said to be amicable if each number is the sum of the proper divisors of the other number. Let's imagine we have two of each integer, A and B. The sum of the proper divisors of A (excluding A itself) is equal to B, and the sum of the proper divisors of B (excluding B itself) is equal to A.
In other words:
- Determine the total of A's suitable divisors or those smaller than A.
- The numbers A and B are amicable if this sum equals B and B is not equal to A.
- B's proper divisors must be added to equal A to meet the amicable requirement.
To clarify, let's use the following variables:
- σ(n) represents the sum of the proper divisors of n (excluding n itself).
- A and B are amicable numbers if σ(A) = B and σ(B) = A, where A ≠ B.
Example:
Here's an illustration of an amicable pair of numbers:
- Let A = 220
- The divisors of 220 are (1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110), excluding 220 itself. These divisors add up to 284, thus.
- Let B = 284
- The divisors of 284 are 1, 2, 4, 71, and 142, excluding 284 itself. These divisors add up to 220 in total.
- A = 220 and B = 284 make up an amicable pair since σ(220) = 284 and σ(284) = 220.
Mathematicians have long been fascinated by amicable numbers, and number theory continues to be a fascinating field of research in this area. Only a few pairs of amicable numbers are known and highly irregular.
![Amicable numbers in C](https://static.tutorialandexample.com/c/amicable-numbers-in-c1.png)
Algorithm:
- To start, request that the user enter both numbers.
- For both numbers, add up all the divisors.
- Finally, determine whether or not the sum of the divisors of one number equals the other.
- If so, it is an amicable number; if not, it is not.
Example:
Let us see an example:
#include<stdio.h>
int main()
{
int i,num1,num2;
int num1DivisorSum = 0,num2DivisorSum = 0;
//inputting two numbers
printf("Enter two numbers");
scanf("%d %d",&num1,&num2);
//finding number1 divisors sum
for(int i=1;i<num1;i++){
if(num1 % i == 0){
num1DivisorSum = num1DivisorSum + i;
}
}
//finding number2 divisors sum
for(int i=1;i<num2;i++){
if(num2 % i == 0){
num2DivisorSum = num2DivisorSum + i;
}
}
//checking condition
if((num1 == num2DivisorSum) && (num2 == num1DivisorSum)){
printf("%d and %d are Amicable numbers\n",num1,num2);
}else{
printf("%d and %d are not Amicable numbers\n",num1,num2);
}
return 0;
}
Output:
![Amicable numbers in C](https://static.tutorialandexample.com/c/amicable-numbers-in-c2.png)
Explanation:
- The numbers in the program above that are commented on indicate the following steps:
- Create two additional integer variables, num1 and num2, to hold the first and second numbers and one integer variable, i, to be used in the loops.
- Create the integer variables num1DivisorSum and num2DivisorSum to keep the total of the divisors.
- Invoke both numbers from the user. Keep the values in num1 and num2.
- Use a single for loop to examine each integer between 1 and num1 -1.
- Verify whether or not each integer in the loop can divide num1 before continuing. If so, increase the num1DivisorSum by this amount. When the loop is finished, num1DivisorSum includes the sum of num1's divisors.
- Similarly, calculate num2's total divisor sum and save it in num2DivisorSum.
- Finally, determine whether or not the total of num1's divisors equals the other number. If so, indicate that both numbers are amicable. If not, they are not.
Applications of Amicable numbers in C:
Despite being a fascinating mathematical idea, amicable numbers have few direct applications in computer science or C programming. Understanding and investigating the characteristics of amicable numbers, however, can be helpful in a variety of ways:
Testing mathematical algorithms: You can use tasks to test your coding and mathematical ability by implementing algorithms to identify and confirm agreeable integers. Practicing programming concepts like loops, conditionals, and functions can be beneficial by working with amicable numbers.
Number theory: A discipline of mathematics that examines the characteristics and connections between numbers and studies agreeable numbers. Investigating amicable numbers can strengthen your comprehension of number theory and related issues.
Mathematical puzzles and challenges: Some many different mathematical puzzles and challenges can be solved using amicable numbers. For instance, in programming competitions or algorithmic challenges where you have to identify pairs of numbers that meet specific criteria, you could come across amicable numbers.
Cryptography: Although not a direct application, certain features of amicable numbers may affect many number theory-based encryption techniques.
Educational purposes: Amicable numbers can be used as examples to illustrate programming topics in instructional materials and tutorials. Implementing algorithms to identify agreeable numbers might be beneficial for students studying C programming.
It is crucial to remember that while amicable numbers may not directly apply in most software development contexts, they are nevertheless precious in mathematics. Additional mathematical and scientific breakthroughs frequently start with mathematical ideas like amicable numbers. Therefore, learning and comprehending these ideas can help one understand mathematics and its uses in various industries.