Divisible by 37 for large numbers
Last Updated :
22 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int divisibleby37(string n){
int l = n.length();
if (n == "0" )
return 0;
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){
string group = n.substr(l - 3, l);
l = l - 3;
int gvalue = (group[0] - '0' ) * 100 +
(group[1] - '0' ) * 10 +
(group[2] - '0' ) * 1;
gSum = gSum + gvalue;
}
if (gSum >= 1000)
return (divisibleby37(to_string(gSum)));
else
return (gSum % 37 == 0);
}
int main(){
string s= "8955795758" ;
if (divisibleby37(s))
cout<< "True" ;
else
cout<< "False" ;
return 0;
}
|
Java
class GFG
{
static int divisibleby37(String n1)
{
int l = n1.length();
if (n1 == "0" )
return 0 ;
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 )
{
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 ;
gSum = gSum + gvalue;
}
if (gSum >= 1000 )
return (divisibleby37(String.valueOf(gSum)));
else
return (gSum % 37 == 0 ) ? 1 : 0 ;
}
public static void main(String[] args)
{
String s= "8955795758" ;
if (divisibleby37(s) == 1 )
System.out.println( "True" );
else
System.out.println( "False" );
}
}
|
Python3
def divisibleby37(n):
l = len (n)
if (n = = 0 ):
return True
if (l % 3 = = 1 ):
n = "00" + n
l + = 2
elif (l % 3 = = 2 ):
n = "0" + n
l + = 1
gSum = 0
while (l ! = 0 ):
group = int (n[l - 3 :l])
l = l - 3
gSum = gSum + group
if (gSum > = 1000 ):
return (divisibleby37( str (gSum)))
else :
return (gSum % 37 = = 0 )
print (divisibleby37( "8955795758" ))
|
C#
using System;
class GFG
{
static int divisibleby37( string n)
{
int l = n.Length;
if (n == "0" )
return 0;
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)
{
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;
gSum = gSum + gvalue;
}
if (gSum >= 1000)
return (divisibleby37(gSum.ToString()));
else
return (gSum % 37 == 0) ? 1 : 0;
}
public static void Main()
{
string s= "8955795758" ;
if (divisibleby37(s) == 1)
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
}
|
PHP
<?php
function divisibleby37( $n )
{
$l = strlen ( $n );
if ( $n == '0' )
return 0;
if ( $l % 3 == 1)
{
$n = "00" . $n ;
$l += 2;
}
else if ( $l % 3 == 2)
{
$n = "0" . $n ;
$l += 1;
}
$gSum = 0;
while ( $l != 0)
{
$group = substr ( $n , $l - 3, $l );
$l = $l - 3;
$gvalue = (ord( $group [0]) - 48) * 100 +
(ord( $group [1]) - 48) * 10 +
(ord( $group [2]) - 48) * 1;
$gSum = $gSum + $gvalue ;
}
if ( $gSum >= 1000)
return (divisibleby37((string)( $gSum )));
else
return ( $gSum % 37 == 0);
}
$s = "8955795758" ;
if (divisibleby37( $s ))
echo "True" ;
else
echo "False" ;
?>
|
Javascript
<script>
function divisibleby37(n)
{
let l = n.length;
if (n == '0' )
return 0;
if (l % 3 == 1)
{
n = "00" + n;
l += 2;
}
else if (l % 3 == 2)
{
n = "0" + n;
l += 1;
}
let gSum = 0;
while (l != 0)
{
let group = n.substr(l - 3, l);
l = l - 3;
gvalue = (group.charCodeAt(0) - 48) * 100 +
(group.charCodeAt(1) - 48) * 10 +
(group.charCodeAt(2) - 48) * 1;
gSum = gSum + gvalue;
}
if (gSum >= 1000)
return (divisibleby37(`${gSum}`));
else
return (gSum % 37 == 0);
}
let s = "8955795758" ;
if (divisibleby37(s))
document.write( "True" );
else
document.write( "False" );
</script>
|
Time Complexity: O(n), where n is the length of the string.
Auxiliary Space: O(1)
Method: Checking given number is divisible by 37 or not by using modulo division operator “%”.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 8955;
if (num % 37 == 0) {
cout << " divisible" ;
}
else {
cout << " not divisible" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void main(String[] args)
{
int num = 8955 ;
if (num % 37 == 0 ) {
System.out.println( " divisible" );
}
else {
System.out.println( " not divisible" );
}
}
}
|
Python3
n = 8955795758
if int (n) % 37 = = 0 :
print ( "true" )
else :
print ( "false" )
|
C#
using System;
public class GFG {
static public void Main()
{
long num = 8955795758;
if (num % 37 == 0) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
|
Javascript
<script>
var n = 8955795758
if (n % 37 == 0)
document.write( "true" )
else
document.write( "false" )
</script>
|
PHP
<?php
$num = 8955795758;
if ( $num % 37 == 0) {
echo "true" ;
}
else {
echo "false" ;
}
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...