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
Selected Reading
Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
We are required to write a JavaScript function that takes in three numbers A, B and N, and finds the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively.
Understanding the Problem
For an N-digit number, we need to:
- Calculate sum of digits at even positions (0-indexed)
- Calculate sum of digits at odd positions (0-indexed)
- Check if even position sum is divisible by A
- Check if odd position sum is divisible by B
Helper Function: Calculate Position-wise Sums
First, let's create a function to calculate sums based on digit positions:
const indexSum = (num, sumOdd = 0, sumEven = 0, index = 0) => {
if(num){
if(index % 2 === 0){
sumEven += num % 10;
}else{
sumOdd += num % 10;
};
return indexSum(Math.floor(num / 10), sumOdd, sumEven, ++index);
};
return {sumOdd, sumEven};
};
// Test the helper function
console.log("Testing indexSum for number 1234:");
console.log(indexSum(1234));
Testing indexSum for number 1234:
{ sumOdd: 6, sumEven: 4 }
Divisibility Check Function
const divides = (b, a) => a % b === 0;
// Test divisibility
console.log("Testing divisibility:");
console.log("15 divisible by 5:", divides(5, 15));
console.log("17 divisible by 5:", divides(5, 17));
Testing divisibility: 15 divisible by 5: true 17 divisible by 5: false
Main Function: Count Valid Numbers
const countNum = (n, first, second) => {
let start = Math.pow(10, (n-1));
const end = Math.pow(10, n)-1;
const res = [];
while(start <= end){
const { sumEven, sumOdd } = indexSum(start);
const condition = divides(first, sumEven) && divides(second, sumOdd);
if(condition){
res.push(start);
};
start++;
};
return res;
};
// Example: Find 2-digit numbers where even position sum divisible by 5 and odd position sum divisible by 3
console.log("2-digit numbers (A=5, B=3):");
console.log(countNum(2, 5, 3));
2-digit numbers (A=5, B=3): [ 30, 35, 60, 65, 90, 95 ]
Complete Example with Explanation
const indexSum = (num, sumOdd = 0, sumEven = 0, index = 0) => {
if(num){
if(index % 2 === 0){
sumEven += num % 10;
}else{
sumOdd += num % 10;
};
return indexSum(Math.floor(num / 10), sumOdd, sumEven, ++index);
};
return {sumOdd, sumEven};
};
const divides = (b, a) => a % b === 0;
const countNum = (n, first, second) => {
let start = Math.pow(10, (n-1));
const end = Math.pow(10, n)-1;
const res = [];
while(start <= end){
const { sumEven, sumOdd } = indexSum(start);
const condition = divides(first, sumEven) && divides(second, sumOdd);
if(condition){
res.push(start);
};
start++;
};
return res;
};
// Test with different parameters
console.log("N=2, A=5, B=3:", countNum(2, 5, 3).length, "numbers found");
console.log("N=2, A=2, B=3:", countNum(2, 2, 3).length, "numbers found");
N=2, A=5, B=3: 6 numbers found N=2, A=2, B=3: 30 numbers found
How It Works
For number 35:
- Digits: 3 (position 1, odd), 5 (position 0, even)
- Even position sum: 5
- Odd position sum: 3
- 5 divisible by 5: ?, 3 divisible by 3: ?
Conclusion
This solution recursively calculates position-wise digit sums and filters N-digit numbers based on divisibility conditions. The algorithm has O(10^N) time complexity as it checks all N-digit numbers.
Advertisements
