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 Abundant Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, understanding and identifying special types of numbers is a fascinating exploration. One such category is abundant numbers.
An abundant number, also known as an excessive number, is a positive integer that is smaller than the sum of its proper divisors (excluding itself).
In this tutorial, we will dive into a JavaScript program designed to check whether a given number is an abundant number or not.
The program will employ logic to calculate the sum of the proper divisors of a number and determine if it exceeds the number itself.
π Example
Let's take a look at the JavaScript code that achieves this functionality.
// Function to check if a number is abundant
function isAbundant(num) {
let sum = 1; // Start with 1 as every number is divisible by 1
// Iterate from 2 to the square root of the number
for (let i = 2; i * i <= num; ++i) {
if (num % i === 0) {
sum += i;
if (i !== num / i) {
sum += num / i;
}
}
}
// If the sum of divisors exceeds the number, it is abundant
return sum > num;
}
// Driver program
// Replace this value with your desired number
const number = 12;
// Check if the number is abundant
if (isAbundant(number)) {
console.log(`${number} is an abundant number.`);
} else {
console.log(`${number} is not an abundant number.`);
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the code.
12 is an abundant number.
Run the script to check if the number is an abundant number.
π§ How the Program Works
- The program defines a function isAbundant that takes a number as input and returns true if the number is abundant and false otherwise.
- The program defines a function isAbundant that takes a number as input and returns true if the number is abundant and false otherwise.
- For each divisor found, it adds it to the sum, including the corresponding divisor on the other side of the square root.
- For each divisor found, it adds it to the sum, including the corresponding divisor on the other side of the square root.
π Between the Given Range
Let's dive into the JavaScript code that identifies abundant numbers within the given range.
// Function to check if a number is abundant
function isAbundantNumber(number) {
let sum = 1; // Start with 1 as it is always a divisor
// Iterate from 2 to the square root of the number
for (let i = 2; i * i <= number; i++) {
if (number % i === 0) {
sum += i;
// If the divisors are not the same, add the other divisor
if (i * i !== number) {
sum += number / i;
}
}
}
// Check if the sum of divisors is greater than the number
return sum > number;
}
// Function to find abundant numbers in a given range
function findAbundantNumbersInRange(start, end) {
console.log(`Abundant numbers between ${start} and ${end}: `);
for (let i = start; i <= end; i++) {
if (isAbundantNumber(i)) {
console.log(i);
}
}
}
// Define the range
const startRange = 1;
const endRange = 50;
// Call the function to find abundant numbers in the range
findAbundantNumbersInRange(startRange, endRange);
π» Testing the Program
The program is set to check for abundant numbers in the range from 1 to 50. You can adjust the range by modifying the values of startRange and endRange in the code.
Abundant numbers between 1 and 50: 12 18 20 24 30 36 40 42 48
Run the script to see the abundant numbers within the specified range.
π§ How the Program Works
- The program defines a function isAbundantNumber that checks if a given number is abundant.
- Inside the function, it iterates through potential divisors up to the square root of the number, summing up proper divisors.
- Another function, findAbundantNumbersInRange, iterates through the specified range and calls the isAbundantNumber function to identify and print abundant numbers.
π§ Understanding the Concept of Abundant Number
Before diving into the code, let's take a moment to understand abundant numbers. An abundant number is a positive integer for which the sum of its proper divisors (excluding itself) is greater than the number itself.
For example, the number 12 is abundant because its divisors (excluding 12) are 1, 2, 3, 4, 6, and the sum of these divisors is 16, which is greater than 12.
π’ Optimizing the Program
While the provided program is effective, there are opportunities for optimization, such as caching previously calculated divisor sums to avoid redundant computations.
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 Abundant Number), please comment here. I will help you immediately.