Related Articles
Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
• Difficulty Level : Medium
• Last Updated : 16 Apr, 2021

Given K, number of digits, and d0 and d1 as the two digits to form the k-sized integer. Task is to check whether the k-sized number formed using d0 and d1 is divisible by 3 or not.
For each i, di is the sum of all preceding (more significant) digits, modulo 10 — more formally, the following formula must hold :

Examples :

Input : K = 5 d0 = 3 d1 = 4
Output : NO
Explanation :
The whole number N is 34748 (Starting from
third digit, every digit is some of preceding
digits mod 10). Since 34748 is not divisible

Input : K = 13 d0 = 8 d1 = 1
Output : YES
Explanation :
The whole number N is 8198624862486,
which is divisible by 3, so the answer
is YES.

K may be very long so generating the entire number, calculating the sum of digits and then checking for multiple of 3 is cumbersome. The key idea behind the solution is that the digits start repeating after some time in a cycle of length 4 and then the sum of digits can be determined in O(1) step.
We know d0 and d1,
So, d2 = (d0 + d1) mod 10
d3 = (d2 + d1 + d0) mod 10 = ((d0 + d1) mod 10 + (d0 + d1)) mod 10 = 2 * (d0 + d1) mod 10
d4 = (d3 + d2 + d1 + d0) mod 10 = 4 * (d0 + d1) mod 10
d5 = (d4 + d3 + d2 + d1 + d0) mod 10 = 8 * (d0 + d1) mod 10
d6 = (d5 +…+ d1 + d0) mod 10 = 16 * (d0 + d1) mod 10 = 6 * (d0 + d1) mod 10
d7 = (d6 +…+ d1 + d0) mod 10 = 32 * (d0 + d1) mod 10 = 2 * (d0 + d1) mod 10
If we keep on getting di, we will see that the resultant is just looping through the same values. We can see that (d0 + d1) contributes 1 time(s) to d2, 2 times to d3, 4 times to d4, 8 times to d5, …, 2^(k – 2) times to dk.
But, since the powers of 2 under modulo 10 cycle from 1, 2, 4, 8, 6, 2, 4. Here, the cycle length is of 4, where d2 is not present in the cycle. Let, S = (2 * (d0 + d1)) mod 10 + (4 * (d0 + d1)) mod 10 + (8 * (d0 + d1)) mod 10 + (6 * (d0 + d1)) mod 10, this is the cycle which repeats.
So, the sum of digits = (d0 + d1 + d2) + S * ( (k – 3) / 4 ) + x. Here, the first 3 terms will be covered by d0, d1, d2 and after that the groups of 4 will be covered by S, but this formula will still has not summed some terms at the end. That is the residue that is noted by x.
Below is the implementation of above approach :

## C++

 // CPP code to check divisibility by 3#include using namespace std; // Function to check the divisibilitystring check(long int k, int d0, int d1){     // Cycle    long int s = (2 * (d0 + d1)) % 10 +                 (4 * (d0 + d1)) % 10 +                 (8 * (d0 + d1)) % 10 +                 (6 * (d0 + d1)) % 10;         // no of residual terms    int a = (k - 3) % 4;         // sum of residual terms    int x;         switch(a)    {             // if no of residue term = 0        case 0:             x = 0;        break;             // if no of residue term = 1        case 1:             x = (2 * (d0 + d1)) % 10;        break;         // if no of residue term = 2        case 2:             x = (2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10;        break;             // if no of residue term = 3        case 3:             x = (2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10 +            (8 * (d0 + d1)) % 10;                 break;    }         // sum of all digits    long int sum = d0 + d1 + ((k - 3) / 4) * s + x;         // divisibility check    if(sum % 3 == 0)        return "YES";    return "NO";} // Driver codeint main(){     long int k, d0, d1;         k = 13;    d0 = 8;    d1 = 1;         cout << check(k, d0, d1) << endl;             k = 5;    d0 = 3;    d1 = 4;             cout << check(k, d0, d1) << endl;     return 0;}

## Java

 // Java code to check divisibility by 3 import java.util.*;import java.io.*; class GFG {    // Function to check the divisibilitystatic String check( int k, int d0, int d1){     // Cycle     int s = (2 * (d0 + d1)) % 10 +                (4 * (d0 + d1)) % 10 +                (8 * (d0 + d1)) % 10 +                (6 * (d0 + d1)) % 10;         // no of residual terms    int a = (k - 3) % 4;         // sum of residual terms    int x=0;         switch(a)    {             // if no of residue term = 0        case 0:             x = 0;        break;             // if no of residue term = 1        case 1:             x = (2 * (d0 + d1)) % 10;        break;         // if no of residue term = 2        case 2:             x = (2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10;        break;             // if no of residue term = 3        case 3:             x = (2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10 +            (8 * (d0 + d1)) % 10;                 break;    }         // sum of all digits     int sum = d0 + d1 + (((k - 3) / 4) * s + x );         // divisibility check    if(sum % 3 == 0)        return "YES";    return "NO";}     //Code driven         public static void main (String[] args) {              int k, d0, d1;         k = 13;    d0 = 8;    d1 = 1;         System.out.println (check(k, d0, d1));             k = 5;    d0 = 3;    d1 = 4;                 System.out.println (check(k, d0, d1));             }}

## Python3

 # Python3 code to check divisibility by 3  # Function to check the divisibilitydef check(k, d0, d1):     # Cycle    s = ((2 * (d0 + d1)) % 10 +        (4 * (d0 + d1)) % 10 +        (8 * (d0 + d1)) % 10 +        (6 * (d0 + d1)) % 10)     # no of residual terms    a = (k - 3) % 4     # if no of residue term = 0    if(a == 0):        x = 0     # if no of residue term = 1    elif(a == 1):        x = (2 * (d0 + d1)) % 10     # if no of residue term = 2    elif(a == 2):        x = ((2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10)     # if no of residue term = 3    elif(a == 3):        x = ((2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10 +            (8 * (d0 + d1)) % 10)     # sum of all digits    sum = d0 + d1 + ((k - 3) // 4) * s + x     # divisibility check    if(sum % 3 == 0):        return "YES"    else:        return "NO"  # Driver codeif __name__=='__main__':    k = 13    d0 = 8    d1 = 1     print(check(k, d0, d1))     k = 5    d0 = 3    d1 = 4     print(check(k, d0, d1)) # This code is contributed by# Sanjit_Prasad

## C#

 // C# code to check divisibility by 3using System; class GFG{// Function to check the divisibilitystatic String check(int k, int d0, int d1){     // Cycle    int s = (2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10 +            (8 * (d0 + d1)) % 10 +            (6 * (d0 + d1)) % 10;         // no of residual terms    int a = (k - 3) % 4;         // sum of residual terms    int x = 0;         switch(a)    {             // if no of residue term = 0        case 0:             x = 0;        break;             // if no of residue term = 1        case 1:             x = (2 * (d0 + d1)) % 10;        break;         // if no of residue term = 2        case 2:             x = (2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10;        break;             // if no of residue term = 3        case 3:             x = (2 * (d0 + d1)) % 10 +            (4 * (d0 + d1)) % 10 +            (8 * (d0 + d1)) % 10;                 break;    }         // sum of all digits    int sum = d0 + d1 + (((k - 3) / 4) * s + x );         // divisibility check    if(sum % 3 == 0)        return "YES";    return "NO";} // Driver Codestatic public void Main (){         int k, d0, d1;    k = 13;    d0 = 8;    d1 = 1;         Console.WriteLine (check(k, d0, d1));             k = 5;    d0 = 3;    d1 = 4;             Console.WriteLine(check(k, d0, d1));}} // This code is contributed by Sach_Code

## PHP

 

## Javascript

 
Output:
YES
NO

Time Complexity : O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up