Check whether a very large number of the given form is a multiple of 3.
Last Updated :
21 Jun, 2022
Consider a very long K-digit number N with digits d0, d1, ..., dK-1 (in decimal notation; d0 is the most significant and dK-1 the least significant digit). This number is so large that it can't be given or written down explicitly; instead, only its starting digits are given and a way to construct the remainder of the number.
Specifically, you are given d0 and d1; for each i ≥ 2, di is the sum of all preceding (more significant) digits, modulo 10, more formally -
Determine if N is a multiple of 3.
Constraints:
2 ≤K ≤1012
1 ≤d0 ≤9
0 ≤d1 ≤9
Examples:
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.
Input : K = 5, d0 = 3, d1 = 4
Output : NO
Explanation: The whole number N is 34748, which is not divisible by 3,
so the answer is NO.
Method 1 (Brute Force)
We can apply the brute force method to calculate the whole number N by using the condition given for constructing the number iteratively (sum of preceding numbers modulo 10) and check whether the number is divisible by 3 or not. But since the number of digits (K) can be as large as 1012, we can't store it as an integer since it will be very larger than the maximum range of 'long long int'. Hence below is an efficient method to determine if N is a multiple of 3.
Method 2 (Efficient)
The key idea behind the solution is the fact that the digits start to repeat after some time in a cycle of length 4. Firstly, we will find the sum of all the digits and then determine if it is divisible by 3 or NOT.
We know d0 and d1.
d2 = ( d0 + d1 ) % 10
d3 = ( d2 + d1 + d0 ) % 10 = (( d0 + d1) % 10 + d0 + d1) % 10 = 2 * ( d0 + d1 ) % 10
Similarly,
d4 = ( d3 + d2 + d1 + d0 ) % 10 = 4 * ( d0 + d1 ) % 10
d5 = ( d4 + d3 + d2 + d1 + d0 ) % 10 = 8 * ( d0 + d1 ) % 10
d6 = ( d5 + ... + d1 + d0 ) % 10 = 16 * (d0 + d3) % 10 = 6 * ( d0 + d1 ) % 10
d7 = ( d6 + ... + d1 + d0 ) % 10 = 12 * ( d0 + d1 ) % 10 = 2 * ( d0 + d1 ) % 10
If we keep on finding on di, we will see that the resultant is just looping around the same values (2, 4, 8, 6).
Here the cycle length is 4 and d2 is not present in the cycle. Hence after d2 the cycle starts forming in length of 4 starting from any value in (2, 4, 8, 6) but in the same order giving a sum of S = 2 + 4 + 8 + 6 = 20 for consecutive four digits. Thus, the total sum of digits for the whole number is = d0 + d1 + d2 + S*(k - 3)/4 + x, where first three terms will be covered by d0, d1, d2
and after that groups of 4 will be covered by S. But since (k - 3) may be not a multiple of 4, some remaining digits will be left which is covered by x which can be calculated by running a loop as those number of terms will be less than 4.
For e.g. When K = 13,
sum of digits = d0 + d1 + d2 + S * (13 - 3) / 4 + x = d0 + d1 + d2 + S * 2 + x,
where first S will have d3, d4, d5, d6 and second S will have d7, d8, d9, d10 and
x = d11 + d12
- d11 = 2 * ( d0 + d1 ) % 10
- d12 = 4 * ( d0 + d1 ) % 10
Below is the implementation of above idea :
C++
// CPP Program to determine if
// number N of given form is
// divisible by 3 or not
#include <bits/stdc++.h>
using namespace std;
// function returns true if number N is
// divisible by 3 otherwise false,
// dig0 - most significant digit
// dig1 - 2nd most significant digit
// K - number of digits
bool multipleOfThree(int K, int dig0, int dig1)
{
// sum of digits
long long int sum = 0;
// store the sum of first two digits
// modulo 10 in a temporary variable
int temp = (dig0 + dig1) % 10;
sum = dig0 + dig1;
// if the number N is a two digit number
if (K == 2) {
if (sum % 3 == 0)
return true;
else
return false;
}
// add temp to sum to get the sum
// of first three digits which are
// not a part of cycle
sum += temp;
// get the number of groups in cycle
long long int numberofGroups = (K - 3) / 4;
// get the remaining number of digits
int remNumberofDigits = (K - 3) % 4;
// if temp = 5 or temp = 0 then sum of each group will
// be 0
if (temp == 5 || temp == 0)
sum += (numberofGroups * 0);
else
// add sum of 20 for each group (2, 4, 8, 6)
sum += (numberofGroups * 20);
// find the remaining sum of remaining digits
for (int i = 0; i < remNumberofDigits; i++) {
temp = (2 * temp) % 10;
sum += temp;
}
// check if it is divisible by 3 or not
if (sum % 3 == 0)
return true;
else
return false;
}
// Driver Code
int main()
{
int K = 5, dig0 = 3, dig1 = 4;
if (multipleOfThree(K, dig0, dig1))
cout << "YES" << endl;
else
cout << "NO" << endl;
K = 10;
dig0 = 3;
dig1 = 2;
if (multipleOfThree(K, dig0, dig1))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
Java
// Java Program to determine if
// number N of given form is
// divisible by 3 or not
import java.io.*;
public class GFG {
// function returns true if number N is
// divisible by 3 otherwise false,
// dig0 - most significant digit
// dig1 - 2nd most significant digit
// K - number of digits
static boolean multipleOfThree(int K, int dig0,
int dig1)
{
// sum of digits
long sum = 0;
// store the sum of first two digits
// modulo 10 in a temporary variable
int temp = (dig0 + dig1) % 10;
sum = dig0 + dig1;
// if the number N is a two digit number
if (K == 2) {
if (sum % 3 == 0)
return true;
else
return false;
}
// add temp to sum to get the sum
// of first three digits which are
// not a part of cycle
sum += temp;
// get the number of groups in cycle
long numberofGroups = (K - 3) / 4;
// get the remaining number of digits
int remNumberofDigits = (K - 3) % 4;
// add sum of 20 for each group (2, 4, 8, 6)
sum += (numberofGroups * 20);
// find the remaining sum of
// remaining digits
for (int i = 0; i < remNumberofDigits; i++) {
temp = (2 * temp) % 10;
sum += temp;
}
// check if it is divisible by 3 or not
if (sum % 3 == 0)
return true;
else
return false;
}
// Driver Code
static public void main(String[] args)
{
int K = 5, dig0 = 3, dig1 = 4;
if (multipleOfThree(K, dig0, dig1))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by vt_m.
Python 3
# Python 3 Program to determine if
# number N of given form is
# divisible by 3 or not
# function returns true if number N
# is divisible by 3 otherwise false,
# dig0 - most significant digit
# dig1 - 2nd most significant digit
# K - number of digits
def multipleOfThree(K, dig0, dig1):
# sum of digits
sum = 0
# store the sum of first two digits
# modulo 10 in a temporary variable
temp = (dig0 + dig1) % 10
sum = dig0 + dig1
# if the number N is a
# two digit number
if (K == 2):
if (sum % 3 == 0):
return True
else:
return False
# add temp to sum to get the sum
# of first three digits which are
# not a part of cycle
sum += temp
# get the number of groups in cycle
numberofGroups = (K - 3) // 4
# get the remaining number of digits
remNumberofDigits = (K - 3) % 4
# add sum of 20 for each
# group (2, 4, 8, 6)
sum += (numberofGroups * 20)
# find the remaining sum of
# remaining digits
for i in range(remNumberofDigits):
temp = (2 * temp) % 10
sum += temp
# check if it is divisible
# by 3 or not
if (sum % 3 == 0):
return True
else:
return False
# Driver Code
if __name__ == "__main__":
K = 5
dig0 = 3
dig1 = 4
if (multipleOfThree(K, dig0, dig1)):
print("Yes")
else:
print("No")
# This code is contributed by ChitraNayal
C#
// C# Program to determine if
// number N of given form is
// divisible by 3 or not
using System;
class GFG {
// function returns true if number N is
// divisible by 3 otherwise false,
// dig0 - most significant digit
// dig1 - 2nd most significant digit
// K - number of digits
static bool multipleOfThree(int K, int dig0, int dig1)
{
// sum of digits
long sum = 0;
// store the sum of first two digits
// modulo 10 in a temporary variable
int temp = (dig0 + dig1) % 10;
sum = dig0 + dig1;
// if the number N is
// a two digit number
if (K == 2) {
if (sum % 3 == 0)
return true;
else
return false;
}
// add temp to sum to get the sum
// of first three digits which are
// not a part of cycle
sum += temp;
// get the number of groups in cycle
long numberofGroups = (K - 3) / 4;
// get the remaining number of digits
int remNumberofDigits = (K - 3) % 4;
// add sum of 20 for each group (2, 4, 8, 6)
sum += (numberofGroups * 20);
// find the remaining sum of
// remaining digits
for (int i = 0; i < remNumberofDigits; i++) {
temp = (2 * temp) % 10;
sum += temp;
}
// check if it is divisible by 3 or not
if (sum % 3 == 0)
return true;
else
return false;
}
// Driver Code
static public void Main(String[] args)
{
int K = 5, dig0 = 3, dig1 = 4;
if (multipleOfThree(K, dig0, dig1))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP Program to determine if number N
// of given form is divisible by 3 or not
// function returns true if number N
// is divisible by 3 otherwise false,
// dig0 - most significant digit
// dig1 - 2nd most significant digit
// K - number of digits
function multipleOfThree($K, $dig0, $dig1)
{
// sum of digits
$sum = 0;
// store the sum of first two digits
// modulo 10 in a temporary variable
$temp = ($dig0 + $dig1) % 10;
$sum = $dig0 + $dig1;
// if the number N is a
// two digit number
if ($K == 2)
if ($sum % 3 == 0)
return true;
else
return false;
// add temp to sum to get the sum
// of first three digits which are
// not a part of cycle
$sum += $temp;
// get the number of groups in cycle
$numberofGroups = (int)(($K - 3) / 4);
// get the remaining number of digits
$remNumberofDigits = ($K - 3) % 4;
// add sum of 20 for each
// group (2, 4, 8, 6)
$sum += ($numberofGroups * 20);
// find the remaining sum of
// remaining digits
for ($i = 0; $i < $remNumberofDigits; $i++)
{
$temp = (2 * $temp) % 10;
$sum += $temp;
}
// check if it is divisible
// by 3 or not
if ($sum % 3 == 0)
return true;
else
return false;
}
// Driver Code
$K = 5;
$dig0 = 3;
$dig1 = 4;
if (multipleOfThree($K, $dig0, $dig1))
print("Yes");
else
print("No");
// This code is contributed by mits
?>
JavaScript
<script>
// JavaScript Program to determine if
// number N of given form is
// divisible by 3 or not
// function returns true if number N is
// divisible by 3 otherwise false,
// dig0 - most significant digit
// dig1 - 2nd most significant digit
// K - number of digits
function multipleOfThree(K, dig0, dig1)
{
// sum of digits
let sum = 0;
// store the sum of first two digits
// modulo 10 in a temporary variable
let temp = (dig0 + dig1) % 10;
sum = dig0 + dig1;
// if the number N is a two digit number
if (K == 2) {
if (sum % 3 == 0)
return true;
else
return false;
}
// add temp to sum to get the sum
// of first three digits which are
// not a part of cycle
sum += temp;
// get the number of groups in cycle
let numberofGroups = parseInt((K - 3) / 4);
// get the remaining number of digits
let remNumberofDigits = (K - 3) % 4;
// if temp = 5 or temp = 0 then sum of each group will
// be 0
if (temp == 5 || temp == 0)
sum += (numberofGroups * 0);
else
// add sum of 20 for each group (2, 4, 8, 6)
sum += (numberofGroups * 20);
// find the remaining sum of remaining digits
for (let i = 0; i < remNumberofDigits; i++) {
temp = (2 * temp) % 10;
sum += temp;
}
// check if it is divisible by 3 or not
if (sum % 3 == 0)
return true;
else
return false;
}
// Driver Code
let K = 5, dig0 = 3, dig1 = 4;
if (multipleOfThree(K, dig0, dig1))
document.write("YES<br>");
else
document.write("NO<br>");
K = 10;
dig0 = 3;
dig1 = 2;
if (multipleOfThree(K, dig0, dig1))
document.write("YES<br>");
else
document.write("NO<br>");
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Write an Efficient Method to Check if a Number is Multiple of 3
Given an integer n, the task is to determine whether n is a multiple of 3 or not.Examples:Input: n = 9Output: YesExplanation: 9 is divisible by 3, so it is a multiple of 3.Input: n = 14Output: NoExplanation: 14 is not divisible by 3, so it is not a multiple of 3.Input: n = 21Output: YesExplanation:
12 min read
Check whether a large number is divisible by 53 or not
Given a large number in the form of a string N, the task is to check whether the number is divisible by 53 or not. Examples: Input: N = 5299947 Output: Yes Input: N = 54 Output: No Approach: Extract the last digit of the given string N and remove it.Multiply that digit by 37.Subtract the product cal
10 min read
An efficient way to check whether n-th Fibonacci number is multiple of 10
We are given a variable n, we need to find whether Fibonacci number will be a multiple of 10 or not. Examples: Input : 15Output : Yes Input : 17Output : No A Simple Method is to find the nth Fibonacci number and check if it is divisible by 10 or not. C++ // A simple C++ program to check if // n-th F
7 min read
To check whether a large number is divisible by 7
You are given an n-digit large number, you have to check whether it is divisible by 7. A (r+1)-digit integer n whose digital form is (ar ar-1 ar-2....a2 a1 a0) is divisible by 7 if and only if the alternate series of numbers (a2 a1 a0) - (a5 a4 a3) + (a8 a7 a6) - ... is divisible by 7. The triplets
15+ min read
Check whether the given number is Euclid Number or not
Given a positive integer n, the task is to check if it is Euclid Number or not. Print âYESâ if the given number is Euclid Number otherwise print âNO'. Euclid number : In Mathematics, Euclid numbers are integers of the form - E_{n} = p_{n}\# + 1 where p_{n}\# is product of first n prime numbers.The f
15 min read
Check whether a given number is Polydivisible or Not
Given an integer n, find whether n is a Polydivisible or not. In mathematics, a number is called Polydivisible if it follows some unique properties. The number should not have any leading zeroes. The number formed by first i digits of the input number should be divisible by i, where i > 1 ~and ~i
5 min read
Check whether the given number is Wagstaff prime or not
Given a positive integer n, the task is to check if it is a Wagstaff prime or not. Print 'YES' if the given number is Wagstaff prime otherwise print 'NO'.Wagstaff prime: In mathematics, Wagstaff prime is a prime number 'n' of the form n = \frac{2^{q} + 1}{3} where 'q' is an odd prime.First, few Wags
7 min read
Check if the large number formed is divisible by 41 or not
Given the first two digits of a large number digit1 and digit2. Also given a number c and the length of the actual large number. The next n-2 digits of the large number are calculated using the formula digit[i] = ( digit[i - 1]*c + digit[i - 2] ) % 10. The task is to check whether the number formed
13 min read
Find whether a given integer is a power of 3 or not
Given a positive integer, write a function to find if it is a power of three or not. Examples: Input : 3Output :YesInput :6Output :NoGeneral Solution to check if any number N is a power of P:Refer to the code below for implementation. C++#include <iostream> using namespace std; #include<mat
15+ min read
Number of factors of very large number N modulo M where M is any prime number
Given a large number N, the task is to find the total number of factors of the number N modulo M where M is any prime number. Examples: Input: N = 9699690, M = 17 Output: 1 Explanation: Total Number of factors of 9699690 is 256 and (256 % 17) = 1Input: N = 193748576239475639, M = 9 Output: 8 Explana
8 min read