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.
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.
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 email@example.com. 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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a large number is divisibility by 15
- Divisibility by 12 for a large number
- Number of factors of very large number N modulo M where M is any prime number
- Check if any permutation of a large number is divisible by 8
- Check if any large number is divisible by 19 or not
- Check if any large number is divisible by 17 or not
- Check divisibility by 7
- Check divisibility in a binary stream
- Check divisibility of binary string by 2^k
- Check the divisibility of Hexadecimal numbers
- Multiply large integers under large modulo
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Check if any permutation of N equals any power of K
- Check if a large number is divisible by a number which is a power of 2
- Sub-string Divisibility by 3 Queries
- Sub-string Divisibility by 11 Queries
- Maximum path sum for each position with jumps under divisibility condition
- Divisibility by 64 with removal of bits allowed
- Repeated Unit Divisibility
- Divisibility by 3 where each digit is the sum of all prefix digits modulo 10