Open In App

Check whether a large number is divisible by 53 or not

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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++




// 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;
}


Java




// Java program to check whether
// a number is divisible by 53 or not
import java.util.*;
 
class GFG{
 
// Function to check if the
// number is divisible by 53 or not
static boolean isDivisible(char []s)
{
    while (s.length > 4)
    {
        int l = s.length - 1;
        int x = (s[l] - '0') * 37;
 
        s = reverse(s);
        s = Arrays.copyOfRange(s, 1, s.length);
 
        int i = 0, carry = 0;
        while (x > 0)
        {
            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 > 0 && i < l)
        {
            int d = (s[i] - '0') - carry;
            if (d < 0)
            {
                d += 10;
                carry = 1;
            }
            else
                carry = 0;
 
            s[i] = (char)(d + '0');
            i++;
        }
        s = reverse(s);
    }
 
    int num = 0;
    for(int i = 0; i < s.length; i++)
    {
       num = num * 10 + (s[i] - '0');
    }
 
    if (num % 53 == 0)
        return true;
    else
        return false;
}
 
static char[] reverse(char []a)
{
    int l, r = a.length - 1;
     
    for(l = 0; l < r; l++, r--)
    {
       char temp = a[l];
            a[l] = a[r];
            a[r] = temp;
    }
    return a;
}
 
// Driver Code
public static void main(String[] args)
{
    String N = "18432462191076";
 
    if (isDivisible(N.toCharArray()))
        System.out.print("Yes" + "\n");
    else
        System.out.print("No" + "\n");
}
}
 
// This code is contributed by Rohit_ranjan


Python3




# Python3 program to check whether a 
# number is divisible by 53 or not
 
# Function to check if the
# number is divisible by 53 or not
def isDivisible(s):
     
    flag = 0
     
    while (len(s) > 4):
        l = len(s) - 1
        x = (ord(s[l]) - ord('0')) * 37
 
        s = s[::-1]
        s = s.replace('0', '', 1)
 
        i = 0
        carry = 0
         
        while (x):
            d = ((ord(s[i]) - ord('0')) -
                 (x % 10) - carry)
            if (d < 0):
                d += 10
                carry = 1
            else:
                carry = 0
                 
            s = s.replace(s[i], chr(d + ord('0')), 1)
            x //= 10
            i += 1
 
        while (carry and i < l):
            d = (ord(s[i]) - ord('0')) - carry
             
            if (d < 0):
                d += 10
                carry = 1
            else:
                carry = 0
             
            s = s.replace(s[i], chr(d + ord('0')), 1)
            i += 1
        s = s[::-1]
 
    num = 0
    for i in range(len(s)):
        num = num * 10 + (ord(s[i]) - ord('0'))
 
    if (num % 53 == 0):
        return True
    else:
        return False
 
# Driver Code
if __name__ == '__main__':
     
    N = "1843246219106"
 
    if (isDivisible(N)):
        print("No")
    else:
        print("Yes")
 
# This code is contributed by Surendra_Gangwar


C#




// C# program to check whether 
// a number is divisible by 53 or not
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
     
// Function to check if the
// number is divisible by 53 or not
static bool isDivisible(char []s)
{
    while (s.Length > 4)
    {
        int l = s.Length - 1;
        int x = (s[l] - '0') * 37;
 
        s = reverse(s);
         
        char []tmp = new char[s.Length - 1];
         
        Array.Copy(s, 1, tmp, 0, s.Length - 1);
        s = tmp;
         
        int i = 0, carry = 0;
        while (x > 0)
        {
            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 > 0 && i < l)
        {
            int d = (s[i] - '0') - carry;
            if (d < 0)
            {
                d += 10;
                carry = 1;
            }
            else
                carry = 0;
 
            s[i] = (char)(d + '0');
            i++;
        }
        s = reverse(s);
    }
 
    int num = 0;
    for(int i = 0; i < s.Length; i++)
    {
        num = num * 10 + (s[i] - '0');
    }
 
    if (num % 53 == 0)
        return true;
    else
        return false;
}
 
static char[] reverse(char []a)
{
    int l, r = a.Length - 1;
     
    for(l = 0; l < r; l++, r--)
    {
        char temp = a[l];
             a[l] = a[r];
             a[r] = temp;
    }
    return a;
}
 
// Driver Code
public static void Main(string[] args)
{
    string N = "18432462191076";
 
    if (isDivisible(N.ToCharArray()))
        Console.Write("Yes" + "\n");
    else
        Console.Write("No" + "\n");
}
}
 
// This code is contributed by rutvik_56


Javascript




// JavaScript program to check
// whether a number
// is divisible by 53 or not
 
 
// Function to check if the
// number is divisible by 53 or not
function isDivisible(s)
{
    s = Array.from(s);
    let flag = 0;
    while (s.length > 4) {
 
        let l = s.length - 1;
        let x = parseInt(s[l]) * 37;
 
         
        s.reverse();
        s.shift();
 
        let i = 0, carry = 0;
        while (x > 0) {
            let d = (parseInt(s[i]))
                    - (x % 10)
                    - carry;
            if (d < 0) {
                d += 10;
                carry = 1;
            }
            else
                carry = 0;
             
            s[i] = d.toString();
            x = Math.floor(x / 10);
            i++;
        }
        while ((carry > 0) && i < l) {
            let d = parseInt(s[i]) - carry;
            if (d < 0) {
                d += 10;
                carry = 1;
            }
            else
                carry = 0;
            s[i] = d.toString();
 
            i++;
        }
         
        s.reverse();
    }
 
    let num = parseInt((s).join(""));
 
    if (num % 53 == 0)
        return true;
    else
        return false;
}
 
 
// Driver Code
let N = "18432462191076";
 
if (isDivisible(N))
    console.log("Yes");
else
    console.log("No");
 
 
// This code is contributed by phasing17


Output:

Yes

Time Complexity: O(n), where n is the size of the given string N
Auxiliary Space: O(1), as no extra space is required

Approach: Divisibility Test using Sum of Digits Method

To determine whether a large number is divisible by 53 or not, we can use the following approach:

  1. Initialize a variable ‘sum’ to 0.
  2. Iterate over each digit of the given number, starting from the leftmost digit.
  3. For each digit, multiply it with an appropriate power of 10 based on its position in the number (i.e., the leftmost digit has a power of 10 equal to the number of digits minus 1, the next digit has a power of 10 equal to the number of digits minus 2, and so on).
  4. Add the result to the ‘sum’ variable.
  5. After iterating over all digits, check if the ‘sum’ variable is divisible by 53. If it is, the number is divisible by 53, and we can return ‘Yes’. Otherwise, the number is not divisible by 53, and we can return ‘No’.

C++




#include <cmath>
#include <iostream>
#include <string>
 
using namespace std;
 
// A function that checks if a number is divisible by 53
string is_divisible_by_53(string n)
{
    int sum = 0;
    int power_of_10 = n.length() - 1;
    for (char digit : n) {
        sum += (digit - '0')
               * pow(10, power_of_10); // Calculate the sum
                                       // of digits
        power_of_10 -= 1;
    }
    if (sum % 53
        == 0) { // Check if the sum is divisible by 53
        return "Yes";
    }
    else {
        return "No";
    }
}
 
int main()
{
    // Test the is_divisible_by_53 function with example
    // inputs
    cout << is_divisible_by_53("5299947")
         << endl; // Output: Yes
    cout << is_divisible_by_53("54") << endl; // Output: No
    return 0;
}
// This code is contributed by sarojmcy2e


Java




// Java program for the above approach
 
import java.io.*;
import java.lang.*;
import java.util.*;
 
class Main {
 
    // Function that checks if a number
    // is divisible by 53
    public static String isDivisibleBy53(String n)
    {
        int sum = 0;
        int powerOf10 = n.length() - 1;
        for (int i = 0; i < n.length(); i++) {
            char digit = n.charAt(i);
 
            // Calculate the sum of digits
            sum += (digit - '0') * Math.pow(10, powerOf10);
            powerOf10--;
        }
        if (sum % 53 == 0) {
            // Check if the sum is divisible by 53
            return "Yes";
        }
        else {
            return "No";
        }
    }
 
    public static void main(String[] args)
        throws java.lang.Exception
    {
        String N = "5299947";
        System.out.println(isDivisibleBy53(N));
        N = "54";
        System.out.println(isDivisibleBy53(N));
    }
}


Python3




def is_divisible_by_53(n):
    sum = 0
    power_of_10 = len(n) - 1
    for digit in n:
        sum += int(digit) * (10 ** power_of_10)
        power_of_10 -= 1
    if sum % 53 == 0:
        return 'Yes'
    else:
        return 'No'
       
print(is_divisible_by_53('5299947')) # Output: Yes
print(is_divisible_by_53('54')) # Output: No


C#




using System;
 
namespace DivisibleBy53
{
    class Program
    {
        // A function that checks if a number is divisible by 53
        static string is_divisible_by_53(string n)
        {
            int sum = 0;
            int power_of_10 = n.Length - 1;
            foreach (char digit in n)
            {
                sum += (digit - '0')
                       * (int)Math.Pow(10, power_of_10); // Calculate the sum
                                                        // of digits
                power_of_10 -= 1;
            }
            if (sum % 53 == 0) // Check if the sum is divisible by 53
            {
                return "Yes";
            }
            else
            {
                return "No";
            }
        }
 
        static void Main(string[] args)
        {
            // Test the is_divisible_by_53 function with example
            // inputs
            Console.WriteLine(is_divisible_by_53("5299947")); // Output: Yes
            Console.WriteLine(is_divisible_by_53("54")); // Output: No
        }
    }
}


Javascript




// Function to check if a number is divisible by 53
function is_divisible_by_53(n) {
  let sum = 0;
  let power_of_10 = n.length - 1;
  // Iterate over each digit of the number
  for (let digit of n) {
    // Calculate the sum of the digits
    sum += parseInt(digit) * (10 ** power_of_10);
    // Decrement the power of 10 for each digit
    power_of_10 -= 1;
  }
  // Check if the sum is divisible by 53
  if (sum % 53 === 0) {
    return 'Yes';
  } else {
    return 'No';
  }
}
 
// Driver code
console.log(is_divisible_by_53('5299947')); // Output: Yes
console.log(is_divisible_by_53('54')); // Output: No


Output

Yes
No

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 12 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads