Given a number N, the task is to check whether the number is divisible by 47 or not.
Input: N = 1645
47 * 35 = 1645
Input: N = 4606
47 * 98 = 4606
Approach: The divisibility test of 47 is:
- Extract the last digit.
- Subtract 14 * last digit from the remaining number obtained after removing the last digit.
- Repeat the above steps until a two-digit number, or zero, is obtained.
- If the two-digit number is divisible by 47, or it is 0, then the original number is also divisible by 47.
If N = 59173 Step 1: N = 59173 Last digit = 3 Remaining number = 5917 Subtracting 14 times last digit Resultant number = 5917 - 14*3 = 5875 Step 2: N = 5875 Last digit = 5 Remaining number = 587 Subtracting 14 times last digit Resultant number = 587 - 14*5 = 517 Step 3: N = 517 Last digit = 7 Remaining number = 51 Subtracting 14 times last digit Resultant number = 51 - 14*7 = -47 Step 4: N = -47 Since N is a two-digit number, and -47 is divisible by 47 Therefore N = 59173 is also divisible by 47
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.