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
Checking for the Gapful numbers in JavaScript
A gapful number is a special type of number that meets specific criteria. Understanding gapful numbers can be useful in mathematical programming and number theory applications.
What is a Gapful Number?
A number is considered gapful when:
It has at least three digits, and
It is exactly divisible by the number formed by combining its first and last digits
Examples
1053 is a gapful number because it has 4 digits and is divisible by 13 (first digit 1 + last digit 3). 135 is a gapful number because it has 3 digits and is divisible by 15 (first digit 1 + last digit 5). 144 is a gapful number because it has 3 digits and is divisible by 14 (first digit 1 + last digit 4).
Checking if a Number is Gapful
Let's create a function to check if a given number is gapful:
const isGapful = (num) => {
const numStr = num.toString();
// Must have at least 3 digits
if (numStr.length
true
true
true
false
Finding the Nearest Gapful Number
Now let's implement a function that finds the nearest gapful number to a given input:
const nearestGapful = (num) => {
// Validate input
if (typeof num !== 'number' || num = 100) {
if (isGapful(prev)) {
// Check if next direction also has gapful number
while (next
135
1001
100
Complete Example
Here's a comprehensive example that demonstrates both functions working together:
const isGapful = (num) => {
const numStr = num.toString();
if (numStr.length {
if (typeof num !== 'number' || num = 100) {
if (isGapful(prev)) {
while (next {
const nearest = nearestGapful(num);
console.log(`Nearest gapful to ${num}: ${nearest}`);
});
Nearest gapful to 134: 135
Nearest gapful to 200: 200
Nearest gapful to 99: 100
Nearest gapful to 1053: 1053
Conclusion
Gapful numbers are an interesting mathematical concept where a number must be divisible by the combination of its first and last digits. The algorithm efficiently finds the nearest gapful number by searching in both directions from the input value.
