Divisibility by 3 where each digit is the sum of all prefix digits modulo 10
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 by 3, answer is NO. 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 <bits/stdc++.h> using namespace std; // Function to check the divisibility string 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 code int 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 divisibility static 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 divisibility def 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 code if __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 3
using System;
class GFG
{
// Function to check the divisibility
static 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 Code
static 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
<?php // PHP code to check // divisibility by 3 // Function to check // the divisibility function 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; // sum of residual // terms $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 $sum = $d0 + $d1 + (int)(($k - 3) / 4) * $s + $x; // divisibility check if($sum % 3 == 0) return "YES"; return "NO"; } // Driver code $k; $d0; $d1; $k = 13; $d0 = 8; $d1 = 1; echo check($k, $d0, $d1), "\n"; $k = 5; $d0 = 3; $d1 = 4; echo check($k, $d0, $d1), "\n"; // This code is contributed by ajit ?> |
YES NO
Time Complexity : O(1)
Recommended Posts:
- Sum of Digits in a^n till a single digit
- Count of n digit numbers whose sum of digits equals to given sum
- Finding sum of digits of a number until sum becomes single digit
- Print all n-digit numbers whose sum of digits equals to given sum
- N digit numbers divisible by 5 formed from the M digits
- Count numbers formed by given two digit with sum having given digits
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Count positive integers with 0 as a digit and maximum 'd' digits
- Find last five digits of a given five digit number raised to power five
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Generate k digit numbers with digits in strictly increasing order
- Maximum of sum and product of digits until number is reduced to a single digit
- Reduce number to a single digit by subtracting adjacent digits repeatedly
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



