Open In App

Check if any large number is divisible by 19 or not

Improve
Improve
Like Article
Like
Save
Share
Report

Given a number, the task is to quickly check if the number is divisible by 19 or not. 
Examples: 
 

Input : x = 38
Output : Yes

Input : x = 47
Output : No


A solution to the problem is to extract the last digit and add 2 times of last digit to remaining number and repeat this process until a two digit number is obtained. If the obtained two digit number is divisible by 19, then the given number is divisible by 19.
Approach: 
 

  • Extract the last digit of the number/truncated number every time
  • Add 2*(last digit of the previous number) to the truncated number
  • Repeat the above three steps as long as necessary.


Illustration: 
 

101156-->10115+2*6 = 10127-->1012+2*7=1026-->102+2*6=114 and 114=6*19,
So 101156 is divisible by 19.


 

Mathematical Proof : 
Let \overline{a b c}                  be any number such that \overline{a b c}                  =100a+10b+c . 
Now assume that \overline{a b c}                  is divisible by 19. Then 
\overline{a b c}\equiv                  0 (mod 19) 
100a+10b+c\equiv                  0 (mod 19) 
10(10a+b)+c\equiv                  0 (mod 19) 
10\overline{a b}                  +c\equiv                  0 (mod 19)
Now that we have separated the last digit from the number, we have to find a way to use it. 
Make the coefficient of \overline{a b}                  1. 
In other words, we have to find an integer such that n such that 10n\equiv                  1 mod 19. 
It can be observed that the smallest n which satisfies this property is 2 as 20\equiv                  1 mod 19. 
Now we can multiply the original equation 10\overline{a b}                  +c\equiv                  0 (mod 19) 
by 2 and simplify it: 
20\overline{a b}                  +2c\equiv                  0 (mod 19) 
\overline{a b}                  +2c\equiv                  0 (mod 19) 
We have found out that if \overline{a b c}\equiv                  0 (mod 19) then, 
\overline{a b}                  +2c\equiv                  0 (mod 19). 
In other words, to check if a 3-digit number is divisible by 19, 
we can just remove the last digit, multiply it by 2, 
and then add to the rest of the two digits. 
 


 

C++

// CPP Program to validate the above logic
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if the number
// is divisible by 19 or not
bool isDivisible(long long int n)
{
 
    while (n / 100) //
    {
        // Extracting the last digit
        int d = n % 10;
 
        // Truncating the number
        n /= 10;
 
        // Adding twice the last digit
        // to the remaining number
        n += d * 2;
    }
 
    // return true if number is divisible by 19
    return (n % 19 == 0);
}
 
// Driver code
int main()
{
    long long int n = 101156;
    if (isDivisible(n))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    return 0;
}

                    

Java

// Java Program to validate the above logic
import java.io.*;
 
class GFG {
 
// Function to check if the
// number is divisible by 19 or not
static boolean isDivisible(long n)
{
 
    while (n / 100>0)
    {
        // Extracting the last digit
        long d = n % 10;
 
        // Truncating the number
        n /= 10;
 
        // Subtracting the five times the
        // last digit from the remaining number
        n += d * 2;
    }
 
    // Return n is divisible by 19
    return (n % 19 == 0);
}
 
// Driver code
 
    public static void main (String[] args) {
    long n = 101156;
    if (isDivisible(n))
        System.out.println( "Yes");
    else
        System.out.println( "No");
    }
}
// This code is contributed by Raj.

                    

Python3

# Python3 Program to validate the above logic
 
# Function to check if the number
# is divisible by 19 or not
def isDivisible(n):
 
    while n // 100:
        # Extracting the last digit
        d = n % 10
 
        # Truncating the number
        n //= 10
 
        # Adding twice the last digit
        # to the remaining number
        n += d * 2
 
    # return True if number is divisible by 19
    return (n % 19 == 0)
 
# Driver code
if __name__ == "__main__":
    n = 101156
    if isDivisible(n):
        print("Yes")
    else:
        print("No")

                    

C#

// C# Program to validate the
// above logic
using System;
 
class GFG
{
     
// Function to check if the
// number is divisible by 19 or not
static bool isDivisible(long n)
{
 
    while (n / 100 > 0)
    {
        // Extracting the last digit
        long d = n % 10;
 
        // Truncating the number
        n /= 10;
 
        // Subtracting the five times
        // the last digit from the
        // remaining number
        n += d * 2;
    }
 
    // Return n is divisible by 19
    return (n % 19 == 0);
}
 
// Driver code
public static void Main()
{
    long n = 101156;
     
    if (isDivisible(n))
        Console.WriteLine( "Yes");
    else
        Console.WriteLine( "No");
}
}
 
// This code is contributed by ajit

                    

Javascript

// Javascript Program to validate the above logic
 
// Function to check if the
// number is divisible by 19 or not
function isDivisible(n)
{
 
    while (parseInt(n / 100)>0)
    {
        // Extracting the last digit
        var d = n % 10;
 
        // Truncating the number
        n = parseInt(n/ 10);
 
        // Subtracting the five times the
        // last digit from the remaining number
        n += d * 2;
    }
 
    // Return n is divisible by 19
    return (n % 19 == 0);
}
 
// Driver code
var n = 101156;
if (isDivisible(n))
    console.log( "Yes");
else
    console.log( "No");
 
// This code is contributed by 29AjayKumar

                    

PHP

<?php
// PHP Program to validate
// the above logic
 
// Function to check if the number
// is divisible by 19 or not
function isDivisible( $n)
{
     
    while (1)
    {
        // Extracting the last digit
        $d = $n % 10;
 
        // Truncating the number
        $n = $n / 10;
 
        // Adding twice the last digit
        // to the remaining number
        $n = $n + $d * 2;
        if($n < 100)
            break;
    }
     
    // return true if number is
    // divisible by 19
    return ($n % 19 == 0);
}
 
// Driver code
$n = 38;
 
if (isDivisible($n))
    echo "Yes" ;
else
    echo "No" ;
 
// This code is contributed by ash264
?>

                    

Output
Yes

Time Complexity: O(log10n), time required to check if number is divisible by 19
Auxiliary Space: O(1), as no extra space is required

Approach 2: Using Modular Arithmetic
A third way to check if a number is divisible by 19 is to use modular arithmetic. This approach involves calculating the remainder when the large number is divided by 19, and checking if the remainder is zero. If the remainder is zero, then the number is divisible by 19.

For example, let’s say we want to check if the number 7,654,321 is divisible by 19.

We calculate the remainder when 7,654,321 is divided by 19 using modular arithmetic.
To do this, we first find the remainder when the first digit, 7, is divided by 19. Since 7 is less than 19, its remainder is simply 7.
Next, we multiply the remainder by 10 and add the next digit, 6, to get 76. We then find the remainder when 76 is divided by 19, which is 0.
We repeat this process for the remaining digits, and find that the remainder when 7,654,321 is divided by 19 is also 0.
Therefore, we can conclude that 7,654,321 is divisible by 19.
These are three different ways to check if a large number is divisible by 19.

Note that the above program may not make a lot of sense as could simply do n % 19 to check for divisibility. The idea of this program is to validate the concept. Also, this might be an efficient approach if input number is large and given as string.

C++

#include <iostream>
#include <string>
 
using namespace std;
 
bool is_divisible_by_19(int num) {
    int remainder = 0;
    for (char digit : to_string(num)) {
        remainder = (remainder * 10 + (digit - '0')) % 19;
    }
    return remainder == 0;
}
 
int main() {
    int n = 101156;
    if (is_divisible_by_19(n)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
    return 0;
}

                    

Java

import java.util.*;
 
public class Gfg {
     
    public static boolean is_divisible_by_19(int num) {
        int remainder = 0;
        for (char digit : Integer.toString(num).toCharArray()) {
            remainder = (remainder * 10 + (digit - '0')) % 19;
        }
        return remainder == 0;
    }
     
    public static void main(String[] args) {
        int n = 101156;
        if (is_divisible_by_19(n)) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
}

                    

Python

def is_divisible_by_19(num):
    remainder = 0
    for digit in str(num):
        remainder = (remainder * 10 + int(digit)) % 19
    return remainder == 0
 
   
# Driver Code
if __name__ == "__main__" :
 
    n = 101156
     
    if (is_divisible_by_19(n)) :
        print("Yes" )
    else :
        print("No")

                    

C#

using System;
 
public class Gfg
{
    public static bool IsDivisibleBy19(int num)
    {
        int remainder = 0;
        foreach (char digit in num.ToString())
        {
            remainder = (remainder * 10 + (digit - '0')) % 19;
        }
        return remainder == 0;
    }
 
    public static void Main()
    {
        int n = 101156;
        if (IsDivisibleBy19(n))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}

                    

Javascript

function is_divisible_by_19(num) {
  let remainder = 0;
  for (const digit of num.toString()) {
    remainder = (remainder * 10 + (digit.charCodeAt() - '0'.charCodeAt())) % 19;
  }
  return remainder === 0;
}
 
const n = 101156;
if (is_divisible_by_19(n)) {
  console.log('Yes');
} else {
  console.log('No');
}

                    

Output
Yes

Time Complexity:
The time complexity of this approach is O(n), where n is the number of digits in the input number num. This is because we need to iterate over each digit in num and perform a constant amount of arithmetic operations (multiplication, addition, and modulo) for each digit.

Space Complexity:
The space complexity of this approach is O(1), as we only use a constant amount of extra space to store the remainder variable. We do not use any data structures that grow with the size of the input.



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