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 < 3) {
return false;
}
// Get first and last digits
const firstDigit = numStr[0];
const lastDigit = numStr[numStr.length - 1];
const divisor = parseInt(firstDigit + lastDigit);
// Check if divisible
return num % divisor === 0;
};
// Test examples
console.log(isGapful(1053)); // true
console.log(isGapful(135)); // true
console.log(isGapful(144)); // true
console.log(isGapful(123)); // false (not divisible by 13)
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 < 0) {
return -1;
}
// Numbers below 100 can't be gapful (need 3+ digits)
if (num <= 100) {
return 100; // First 3-digit gapful number
}
// Check if current number is already gapful
if (isGapful(num)) {
return num;
}
// Search in both directions
let prev = num - 1;
let next = num + 1;
while (prev >= 100) {
if (isGapful(prev)) {
// Check if next direction also has gapful number
while (next <= num + (num - prev)) {
if (isGapful(next)) {
// Return the closer one
return (num - prev <= next - num) ? prev : next;
}
next++;
}
return prev;
}
if (isGapful(next)) {
return next;
}
prev--;
next++;
}
// If no previous gapful found, continue searching forward
while (true) {
if (isGapful(next)) {
return next;
}
next++;
}
};
// Test the function
console.log(nearestGapful(134)); // 135
console.log(nearestGapful(1000)); // 1001
console.log(nearestGapful(50)); // 100
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 < 3) return false;
const firstDigit = numStr[0];
const lastDigit = numStr[numStr.length - 1];
const divisor = parseInt(firstDigit + lastDigit);
return num % divisor === 0;
};
const nearestGapful = (num) => {
if (typeof num !== 'number' || num < 0) return -1;
if (num <= 100) return 100;
if (isGapful(num)) return num;
let prev = num - 1;
let next = num + 1;
while (prev >= 100) {
if (isGapful(prev)) {
while (next <= num + (num - prev)) {
if (isGapful(next)) {
return (num - prev <= next - num) ? prev : next;
}
next++;
}
return prev;
}
if (isGapful(next)) return next;
prev--;
next++;
}
while (true) {
if (isGapful(next)) return next;
next++;
}
};
// Test with various inputs
const testNumbers = [134, 200, 99, 1053];
testNumbers.forEach(num => {
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.
