Related Articles
Divisible by 37 for large numbers
• Difficulty Level : Hard
• Last Updated : 16 Apr, 2021

Given a large number n, we need to check whether it is divisible by 37. Print true if it is divisible by 37 otherwise False.
Examples:

Input  : 74
Output : True

Input : 73
Output : False

Input : 8955795758 (10 digit number)
Output : True

A r digit number m whose digital form is (ar-1 ar-2….a2 a1 a0) is divisible by 37 if and only if the sum of series of numbers (a2 a1 a0) + (a5 a4 a3) + (a8 a7 a6) + … is divisible by 37. The triplets of digits within parenthesis represent 3-digit number in digital form.

The given number n can be written as a sum of powers of 1000 as follows.
n = (a2 a1 a0) + (a5 a4 a3)*1000 + (a8 a7 a6)*(1000*1000) +….
As 1000 = (1)(mod 37), 1000 as per congruence relation.
For a positive integer n, two numbers a and b are said to be congruent modulo n, if their difference
(a – b) is an integer multiple of n (that is, if there is an integer k such that a – b = kn). This congruence relation is typically considered when a and b are integers, and is denoted Hence we can write:
n = { (a2a1a0) + (a5a4a3)* (1) + (a8a7a6)* (1)*(1)+…..}(mod 37),
Thus n is divisible by 37 if and if only if the series is divisible by 37.

Examples:

Input : 8955795758 (10 digit number)
Output : True
Explanation:
We express the number in terms of
triplets of digits as follows.
(008)(955)(795)(758)
Now, 758 + 795 + 955 + 8 = 2516
For 2516, the triplets will be:
(002)(516)
Now 516 + 2 = 518 which is divisible
by 37. Hence the number is divisible
by 37.

Input : 189710809179199 (15 digit number)
Output : False

A simple and efficient method is to take input in form of string (make its length in form of 3*m by adding 0 to left of number if required) and then you have to add the digits in blocks of three from right to left until it become a 3 digit number to form an series . Calculate the sum of the series. If the sum of series has more than 3 digits in it, again recursively call this function.
Finally check whether the resultant sum is divisible by 37 or not.
Here is the program implementation to check divisibility by 37.

## C++

 // CPP program for checking divisibility by 37// function divisible37 which returns True if// number is divisible by 37 otherwise False#include using namespace std; int divisibleby37(string n){    int l = n.length();    if (n == "0")        return 0;     // Append required 0's at the beginning    if (l % 3 == 1){        n = "00"+ n;        l += 2;    }    else if (l % 3 == 2){        n = "0"+ n;        l += 1;    }         int gSum = 0;         while (l != 0){     // group saves 3-digit group    string group = n.substr(l - 3, l);        l = l - 3;    int gvalue = (group - '0') * 100 +                 (group - '0') * 10 +                 (group - '0') * 1;                      // add the series    gSum = gSum + gvalue;    }         // if sum of series gSum has minimum 4    // digits in it, then again recursive    // call divisibleby37 function    if (gSum >= 1000)        return (divisibleby37(to_string(gSum)));    else        return (gSum % 37 == 0); } // drive program to test the above functionint main(){     string s="8955795758";         if (divisibleby37(s))    cout<<"True";    else    cout<<"False";    return 0;}// This code is contributed by Prerna Saini

## Java

 // Java program for checking// divisibility by 37 class GFG{// function divisible37 which// returns True if number is// divisible by 37 otherwise Falsestatic int divisibleby37(String n1){    int l = n1.length();    if (n1 == "0")        return 0;     // Append required 0's    // at the beginning    if (l % 3 == 1)    {        n1 = "00"+ n1;        l += 2;    }    else if (l % 3 == 2)    {        n1 = "0"+ n1;        l += 1;    }    char[]  n= n1.toCharArray();    int gSum = 0;    while (l != 0)    {     // group saves 3-digit group    int gvalue;    if(l == 2)        gvalue = ((int)n[(l - 2)] - 48) * 100 +                ((int)n[(l - 1)] - 48) * 10;    else if(l == 1)        gvalue = ((int)n[(l - 1)] - 48) * 100;    else        gvalue = ((int)n[(l - 3)] - 48) * 100 +                ((int)n[(l - 2)] - 48) * 10 +                ((int)n[(l - 1)] - 48) * 1;    l = l - 3;         // add the series    gSum = gSum + gvalue;    }         // if sum of series gSum has minimum 4    // digits in it, then again recursive    // call divisibleby37 function    if (gSum >= 1000)        return (divisibleby37(String.valueOf(gSum)));    else        return (gSum % 37 == 0) ? 1 : 0; } // Driver Codepublic static void main(String[] args){    String s="8955795758";         if (divisibleby37(s) == 1)    System.out.println("True");    else    System.out.println("False");}} // This code is contributed by mits

## Python3

 # Python code for checking divisibility by 37# function divisible37 which returns True if# number is divisible by 37 otherwise Falsedef divisibleby37(n):    l = len(n)    if (n == 0):        return True       # Append required 0's at the beginning    if (l%3 == 1):        n = "00"+ n        l += 2    elif (l%3 == 2):        n = "0"+ n        l += 1     gSum = 0    while (l != 0):         # group saves 3-digit group        group = int(n[l-3:l])        l = l-3         # add the series        gSum = gSum + group     # if sum of series gSum has minimum 4    # digits in it, then again recursive    # call divisibleby37 function    if (gSum >= 1000):        return(divisibleby37(str(gSum)))    else:        return (gSum%37==0) # Driver method to test the above functionprint(divisibleby37("8955795758"))

## C#

 // C# program for checking// divisibility by 37using System; class GFG{// function divisible37 which// returns True if number is// divisible by 37 otherwise Falsestatic int divisibleby37(string n){    int l = n.Length;    if (n == "0")        return 0;     // Append required 0's    // at the beginning    if (l % 3 == 1)    {        n = "00"+ n;        l += 2;    }    else if (l % 3 == 2)    {        n = "0"+ n;        l += 1;    }         int gSum = 0;    while (l != 0)    {     // group saves 3-digit group    int gvalue;    if(l == 2)        gvalue = ((int)n[(l - 2)] - 48) * 100 +                 ((int)n[(l - 1)] - 48) * 10;    else if(l == 1)        gvalue = ((int)n[(l - 1)] - 48) * 100;    else        gvalue = ((int)n[(l - 3)] - 48) * 100 +                 ((int)n[(l - 2)] - 48) * 10 +                 ((int)n[(l - 1)] - 48) * 1;    l = l - 3;         // add the series    gSum = gSum + gvalue;    }         // if sum of series gSum has minimum 4    // digits in it, then again recursive    // call divisibleby37 function    if (gSum >= 1000)        return (divisibleby37(gSum.ToString()));    else        return (gSum % 37 == 0) ? 1 : 0; } // Driver Codepublic static void Main(){    string s="8955795758";         if (divisibleby37(s) == 1)    Console.WriteLine("True");    else    Console.WriteLine("False");}} // This code is contributed by mits

## PHP

 = 1000)        return (divisibleby37((string)($gSum))); else return ($gSum % 37 == 0); } // Driver code$s = "8955795758"; if (divisibleby37($s))echo "True";elseecho "False"; // This code is contributed// by mits?>

## Javascript

 

Output:

True

This article is contributed by Sruti Rai. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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.

My Personal Notes arrow_drop_up