Check whether a large number is divisible by 53 or not

Given a large number in the form of a string N, the task is to check whether the number is divisible by 53 or not.

Examples:

Input: N = 5299947
Output: Yes



Input: N = 54
Output: No

Approach:

  • Extract the last digit of the given string N and remove it.
  • Multiply that digit by 37.
  • Subtract the product calculated in the above step from the remaining number.
  • Continue until we reduce the given string to a 3 or four digit number.
  • Convert the remaining string to its corresponding integer form and check if it is divisible by 53 or not.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check
// whether a number
// is divisible by 53 or not
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if the
// number is divisible by 53 or not
bool isDivisible(string s)
{
    int flag = 0;
    while (s.size() > 4) {
  
        int l = s.size() - 1;
        int x = (s[l] - '0') * 37;
  
        reverse(s.begin(), s.end());
        s.erase(0, 1);
  
        int i = 0, carry = 0;
        while (x) {
            int d = (s[i] - '0')
                    - (x % 10)
                    - carry;
            if (d < 0) {
                d += 10;
                carry = 1;
            }
            else
                carry = 0;
  
            s[i] = (char)(d + '0');
            x /= 10;
            i++;
        }
  
        while (carry && i < l) {
            int d = (s[i] - '0') - carry;
            if (d < 0) {
                d += 10;
                carry = 1;
            }
            else
                carry = 0;
  
            s[i] = (char)(d + '0');
  
            i++;
        }
  
        reverse(s.begin(), s.end());
    }
  
    int num = 0;
    for (int i = 0; i < s.size(); i++) {
        num = num * 10 + (s[i] - '0');
    }
  
    if (num % 53 == 0)
        return true;
    else
        return false;
}
  
// Driver Code
int main()
{
    string N = "18432462191076";
  
    if (isDivisible(N))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
  
    return 0;
}

chevron_right


Output:

Yes

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.