JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Check Amicable Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, exploring number properties is a fascinating journey. One interesting concept is that of amicable numbers.
Amicable numbers are two numbers that share a unique relationship: the sum of the proper divisors of each number is equal to the other number.
In simpler terms, the sum of the divisors of one number is the other number itself.
In this tutorial, we'll explore a JavaScript program designed to check whether two given numbers form an amicable pair.
The program will identify and compare the sum of proper divisors to determine if the numbers form an amicable pair.
π Example
In this tutorial, we'll explore a JavaScript program designed to check whether two given numbers form an amicable pair.
// Function to calculate the sum of proper divisors of a number
function sumOfDivisors(num) {
let sum = 1; // Start with 1 as every number is divisible by 1
for (let i = 2; i * i <= num; ++i) {
if (num % i === 0) {
sum += i;
if (i !== num / i) {
sum += num / i;
}
}
}
return sum;
}
// Function to check if two numbers are amicable
function areAmicable(num1, num2) {
return sumOfDivisors(num1) === num2 && sumOfDivisors(num2) === num1;
}
// Replace these values with your desired numbers
const number1 = 220;
const number2 = 284;
// Check if the numbers are amicable
if (areAmicable(number1, number2)) {
console.log(`${number1} and ${number2} are amicable numbers.`);
} else {
console.log(`${number1} and ${number2} are not amicable numbers.`);
}
π» Testing the Program
To test the program with different numbers, replace the values of number1 and number2 in the code.
220 and 284 are amicable numbers.
Run the script to check if the numbers form an amicable pair.
π§ How the Program Works
- The program defines a function sumOfDivisors to calculate the sum of proper divisors for a given number.
- Another function, areAmicable, checks whether two numbers are amicable by comparing the sums of their proper divisors.
- The program then tests the amicability of two numbers and logs the result.
π§ Understanding the Concept of Amicable Numbers
Amicable numbers exhibit a unique relationship where the sum of the proper divisors of each number is equal to the other number.
For example, the pair (220, 284) is amicable because the sum of divisors of 220 is 284, and the sum of divisors of 284 is 220.
π’ Optimizing the Program
While the provided program is effective, you may explore optimizations such as caching the results of the sum of divisors to enhance performance for repeated calculations.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Check Amicable Number), please comment here. I will help you immediately.