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
Numbers obtained during checking divisibility by 7 using JavaScript
In mathematics, we can check if a number is divisible by 7 using a special algorithm. For any number of the form 10a + b (where a is all digits except the last, and b is the last digit), we calculate a - 2b. If this result is divisible by 7, then the original number is also divisible by 7.
We repeat this process until we get a number with at most 2 digits, since it's easy to check divisibility by 7 for such small numbers. This article demonstrates how to implement this algorithm in JavaScript and count the steps required.
Problem Statement
Write a JavaScript function that takes a number and returns:
- The final two-digit (or smaller) number obtained after applying the divisibility rule
- The number of steps required to reach this result
Algorithm Explanation
For a number like 1603:
- Split into 160 (a) and 3 (b)
- Calculate: 160 - 2×3 = 154
- Split 154 into 15 (a) and 4 (b)
- Calculate: 15 - 2×4 = 7
- Since 7 has only 1 digit, we stop
Result: 2 steps to reach the final number 7.
Implementation
const num = 1603;
const findSteps = (num) => {
let times = 0;
let result = 0;
let number = String(num);
while(number.length > 2) {
times++;
let lastDigit = Number(number.slice(-1));
const remaining = Number(number.slice(0, number.length - 1));
result = remaining - 2 * lastDigit;
number = String(result);
}
return [result, times];
};
console.log(findSteps(num));
[7, 2]
Step-by-Step Trace
Let's trace through the execution with input 1603:
const traceSteps = (num) => {
let times = 0;
let number = String(num);
console.log(`Starting with: ${number}`);
while(number.length > 2) {
times++;
let lastDigit = Number(number.slice(-1));
const remaining = Number(number.slice(0, number.length - 1));
let result = remaining - 2 * lastDigit;
console.log(`Step ${times}: ${remaining} - 2×${lastDigit} = ${result}`);
number = String(result);
}
console.log(`Final result: ${number} (reached in ${times} steps)`);
return [Number(number), times];
};
traceSteps(1603);
Starting with: 1603 Step 1: 160 - 2×3 = 154 Step 2: 15 - 2×4 = 7 Final result: 7 (reached in 2 steps)
Testing with Multiple Examples
const testCases = [1603, 371, 1001, 98];
testCases.forEach(num => {
const result = findSteps(num);
console.log(`${num} ? Final: ${result[0]}, Steps: ${result[1]}`);
});
1603 ? Final: 7, Steps: 2 371 ? Final: 35, Steps: 1 1001 ? Final: 98, Steps: 1 98 ? Final: 98, Steps: 0
Key Points
- The algorithm works by repeatedly applying the rule: a - 2b where b is the last digit
- We stop when the number has 2 digits or fewer
- The function returns both the final number and the step count
- Numbers with 2 digits or fewer require 0 steps
Conclusion
This divisibility-by-7 algorithm provides an efficient way to reduce large numbers to smaller ones while preserving their divisibility properties. The JavaScript implementation demonstrates how mathematical algorithms can be elegantly coded using string manipulation and arithmetic operations.
