To check divisibility of any large number by 999

3.6

You are given an n-digit large number, you have to check whether it is divisible by 999 without dividing or finding modulo of number by 999.

Input : 235764 
Output : Yes

Input : 23576 
Output : No

Since input number may be very large, we cannot use n % 999 to check if a number is divisible by 999 or not, especially in languages like C/C++. The idea is based on following fact.

The solutions is based on below fact.

A number is divisible by 999 if sum of its 3-digit-groups (if required groups are formed by appending a 0s at the beginning) is divisible by 999.

Illustration:

Input : 235764 
Output : Yes
Explanation : Step I - read input : 235, 764
              Step II - 235 + 764 = 999
              As result is 999 then we can 
              conclude that it is divisible by 999.

Input : 1244633121
Output : Yes
Explanation : Step I - read input : 1, 244, 633, 121
              Step II - 001 + 244 + 633 + 121 = 999
              As result is 999 then we can conclude 
              that it is divisible by 999.

Input : 999999999
Output : Yes
Explanation : Step I - read input : 999, 999, 999
              Step II - 999 + 999 + 999 = 2997
              Step III - 997 + 002 = 999
              As result is 999 then we can conclude  
              that it is divisible by 999.

How does this work?

Let us consider 235764, we can write it as
235764 = 2*105 + 3*104 + 5*103 + 
         7*102 + 6*10 + 4

The idea is based on below observation:
Remainder of 103 divided by 999 is 1
For i > 3, 10i % 999 = 10i-3 % 999 

Let us see how we use above fact.
Remainder of 2*105 + 3*104 + 5*103 + 
7*102 + 6*10 + 4
Remainder with 999 can be written as : 
2*100 + 3*10 + 5*1 + 7*100 + 6*10 + 4 
The above expression is basically sum of
groups of size 3.

Since the sum is divisible by 999, answer is yes.

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 and if that result is 999 we can say that number is divisible by 999.

As in the case of “divisibility by 9” we check that sum of all digit is divisible by 9 or not, the same thing follows within the case of divisibility by 999. We sum up all 3-digits group from right to left and check whether the final result is 999 or not.

C++

// CPP for divisibilty of number by 999
#include<bits/stdc++.h>
using namespace std;

// function to check divisibility
bool isDivisible999(string num)
{
    int n = num.length();
    if (n == 0 && num[0] == '0')
       return true;

    // Append required 0s at the beginning.
    if (n % 3 == 1)
       num = "00" + num;
    if (n % 3 == 2)
       num = "0" + num;

    // add digits in group of three in gSum
    int gSum = 0;
    for (int i = 0; i<n; i++)
    {
        // group saves 3-digit group
        int group = 0;
        group += (num[i++] - '0') * 100;
        group += (num[i++] - '0') * 10;
        group += num[i] - '0';
        gSum += group;
    }

    // calculate result till 3 digit sum
    if (gSum > 1000)
    {
        num = to_string(gSum);
        n = num.length();
        gSum = isDivisible999(num);
    }
    return (gSum == 999);
}

// driver program
int main()
{
    string num = "1998";
    int n = num.length();
    if (isDivisible999(num))
        cout << "Divisible";
    else
        cout << "Not divisible";
    return 0;
}

Java

//Java for divisiblity of number by 999

class Test
{	
	// Method to check divisibility
	static boolean isDivisible999(String num)
	{
	    int n = num.length();
	    if (n == 0 && num.charAt(0) == '0')
	       return true;
	 
	    // Append required 0s at the beginning.
	    if (n % 3 == 1)
	       num = "00" + num;
	    if (n % 3 == 2)
	       num = "0" + num;
	 
	    // add digits in group of three in gSum
	    int gSum = 0;
	    for (int i = 0; i<n; i++)
	    {
	        // group saves 3-digit group
	        int group = 0;
	        group += (num.charAt(i++) - '0') * 100;
	        group += (num.charAt(i++) - '0') * 10;
	        group += num.charAt(i) - '0';
	        gSum += group;
	    }
	 
	    // calculate result till 3 digit sum
	    if (gSum > 1000)
	    {
	        num = Integer.toString(gSum);
	        n = num.length();
	        gSum = isDivisible999(num) ? 1 : 0;
	    }
	    return (gSum == 999);
	}
	
	// Driver method
	public static void main(String args[]) 
	{
		String num = "1998";
	 
	    System.out.println(isDivisible999(num) ? "Divisible" : "Not divisible");
	}
}


Output:

Divisible

More Divisibility Algorithms.

This article is contributed by Shivam Pradhan (anuj_charm). 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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3.6 Average Difficulty : 3.6/5.0
Based on 5 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.