Write an Efficient Method to Check if a Number is Multiple of 3
Last Updated :
29 Mar, 2025
Given an integer n, the task is to determine whether n is a multiple of 3 or not.
Examples:
Input: n = 9
Output: Yes
Explanation: 9 is divisible by 3, so it is a multiple of 3.
Input: n = 14
Output: No
Explanation: 14 is not divisible by 3, so it is not a multiple of 3.
Input: n = 21
Output: Yes
Explanation: 21 is divisible by 3, so it is a multiple of 3.
[Approach 1] Using Repeated Sum of Digits - O(log(n)) Time and O(1) Space
The idea is based on the divisibility rule of 3, which states that a number is a multiple of 3 if the sum of its digits is also a multiple of 3. Instead of directly checking divisibility, we repeatedly sum the digits until we get a single-digit number. If the final result is 3, 6, or 9, the number is divisible by 3; otherwise, it is not.
C++
// C++ program to check if a number is a
// multiple of 3 using Repeated Sum of Digits
#include <bits/stdc++.h>
using namespace std;
// Function to compute sum of digits
int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
// Add last digit to sum
sum += n % 10;
// Remove last digit
n /= 10;
}
return sum;
}
// Function to check if n is a multiple of 3
bool isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
while (n > 9) {
// Replace n with sum of its digits
n = sumOfDigits(n);
}
// Check if final result is 3, 6, or 9
return (n == 3 || n == 6 || n == 9);
}
int main() {
int n = 9;
if (isMultipleOf3(n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program to check if a number is a
// multiple of 3 using Repeated Sum of Digits
class GfG {
// Function to compute sum of digits
static int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
// Add last digit to sum
sum += n % 10;
// Remove last digit
n /= 10;
}
return sum;
}
// Function to check if n is a multiple of 3
static boolean isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
while (n > 9) {
// Replace n with sum of its digits
n = sumOfDigits(n);
}
// Check if final result is 3, 6, or 9
return (n == 3 || n == 6 || n == 9);
}
public static void main(String[] args) {
int n = 9;
if (isMultipleOf3(n)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python program to check if a number is a
# multiple of 3 using Repeated Sum of Digits
# Function to compute sum of digits
def sumOfDigits(n):
sum = 0
while n > 0:
# Add last digit to sum
sum += n % 10
# Remove last digit
n //= 10
return sum
# Function to check if n is a multiple of 3
def isMultipleOf3(n):
# Make n positive
if n < 0:
n = -n
while n > 9:
# Replace n with sum of its digits
n = sumOfDigits(n)
# Check if final result is 3, 6, or 9
return (n == 3 or n == 6 or n == 9)
if __name__ == "__main__":
n = 9
if isMultipleOf3(n):
print("Yes")
else:
print("No")
C#
// C# program to check if a number is a
// multiple of 3 using Repeated Sum of Digits
using System;
class GfG {
// Function to compute sum of digits
static int sumOfDigits(int n) {
int sum = 0;
while (n > 0) {
// Add last digit to sum
sum += n % 10;
// Remove last digit
n /= 10;
}
return sum;
}
// Function to check if n is a multiple of 3
static bool isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
while (n > 9) {
// Replace n with sum of its digits
n = sumOfDigits(n);
}
// Check if final result is 3, 6, or 9
return (n == 3 || n == 6 || n == 9);
}
static void Main() {
int n = 9;
if (isMultipleOf3(n)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to check if a number is a
// multiple of 3 using Repeated Sum of Digits
// Function to compute sum of digits
function sumOfDigits(n) {
let sum = 0;
while (n > 0) {
// Add last digit to sum
sum += n % 10;
// Remove last digit
n = Math.floor(n / 10);
}
return sum;
}
// Function to check if n is a multiple of 3
function isMultipleOf3(n) {
// Make n positive
if (n < 0) {
n = -n;
}
while (n > 9) {
// Replace n with sum of its digits
n = sumOfDigits(n);
}
// Check if final result is 3, 6, or 9
return (n == 3 || n == 6 || n == 9);
}
let n = 9;
if (isMultipleOf3(n)) {
console.log("Yes");
}
else {
console.log("No");
}
[Approach 2] Using Binary Representation - O(log(n)) Time and O(log(n)) Space
The idea is based on the binary representation of a number. The observation is that even powers of 2 contribute (3k+1) and odd powers contribute (3k-1) modulo 3. By counting set bits at odd and even positions and checking their difference, we determine if the number is divisible by 3. The approach uses recursion, reducing n by half in each step.
Mathematical Proof:
Consider a number n represented in binary form as abcde. Its decimal equivalent is:
n = (2^4 * a) + (2^3 * b) + (2^2 * c) + (2^1 * d) + (2^0 * e)
Observing powers of 2 modulo 3, we derive:
- Even powers of 2 can be expressed as 3k + 1 (e.g., 2^0 = 1, 2^2 = 3k + 1).
- Odd powers of 2 can be expressed as 3k - 1 (e.g., 2^1 = 3k - 1, 2^3 = 3k - 1).
Rewriting the decimal representation in terms of multiples of 3, we get:
n = (3k+1) * a + (3k-1) * b + (3k+1) * c + (3k-1) * d + (3k+1) * e
Rearranging the terms:
n = (3k)(a+b+c+d+e) + (a + c + e) - (b + d)
Since the first term is always a multiple of 3, for n to be a multiple of 3, the expression (a + c + e) - (b + d) must also be a multiple of 3. Thus, a number is divisible by 3 if and only if the difference between the count of set bits at odd positions and even positions is a multiple of 3.
C++
// C++ program to check if a number is a
// multiple of 3 using Binary
// Representation
#include <bits/stdc++.h>
using namespace std;
// Function to check if n is a multiple of 3
bool isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
// Base cases
if (n == 0) {
return true;
}
if (n == 1) {
return false;
}
int odd_count = 0, even_count = 0;
while (n) {
// If rightmost bit is set, increment odd_count
if (n & 1) {
odd_count++;
}
// Right shift n
n >>= 1;
// If rightmost bit is set, increment even_count
if (n & 1) {
even_count++;
}
// Right shift n again
n >>= 1;
}
// Recursive call with the difference
return isMultipleOf3(abs(odd_count - even_count));
}
int main() {
int n = 9;
if (isMultipleOf3(n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program to check if a number is a
// multiple of 3 using Binary
// Representation
class GfG {
// Function to check if n is a multiple of 3
static boolean isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
// Base cases
if (n == 0) {
return true;
}
if (n == 1) {
return false;
}
int odd_count = 0, even_count = 0;
while (n > 0) {
// If rightmost bit is set, increment odd_count
if ((n & 1) == 1) {
odd_count++;
}
// Right shift n
n >>= 1;
// If rightmost bit is set, increment even_count
if ((n & 1) == 1) {
even_count++;
}
// Right shift n again
n >>= 1;
}
// Recursive call with the difference
return isMultipleOf3(Math.abs(odd_count - even_count));
}
public static void main(String[] args) {
int n = 9;
if (isMultipleOf3(n)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python program to check if a number is a
# multiple of 3 using Binary
# Representation
# Function to check if n is a multiple of 3
def isMultipleOf3(n):
# Make n positive
if n < 0:
n = -n
# Base cases
if n == 0:
return True
if n == 1:
return False
odd_count = 0
even_count = 0
while n:
# If rightmost bit is set, increment odd_count
if n & 1:
odd_count += 1
# Right shift n
n >>= 1
# If rightmost bit is set, increment even_count
if n & 1:
even_count += 1
# Right shift n again
n >>= 1
# Recursive call with the difference
return isMultipleOf3(abs(odd_count - even_count))
if __name__ == "__main__":
n = 9
if isMultipleOf3(n):
print("Yes")
else:
print("No")
C#
// C# program to check if a number is a
// multiple of 3 using Binary
// Representation
using System;
class GfG {
// Function to check if n is a multiple of 3
static bool isMultipleOf3(int n) {
// Make n positive
if (n < 0) {
n = -n;
}
// Base cases
if (n == 0) {
return true;
}
if (n == 1) {
return false;
}
int odd_count = 0, even_count = 0;
while (n > 0) {
// If rightmost bit is set, increment odd_count
if ((n & 1) == 1) {
odd_count++;
}
// Right shift n
n >>= 1;
// If rightmost bit is set, increment even_count
if ((n & 1) == 1) {
even_count++;
}
// Right shift n again
n >>= 1;
}
// Recursive call with the difference
return isMultipleOf3(Math.Abs(odd_count - even_count));
}
public static void Main() {
int n = 9;
if (isMultipleOf3(n)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to check if a number is a
// multiple of 3 using Binary
// Representation
// Function to check if n is a multiple of 3
function isMultipleOf3(n) {
// Make n positive
if (n < 0) {
n = -n;
}
// Base cases
if (n === 0) {
return true;
}
if (n === 1) {
return false;
}
let odd_count = 0, even_count = 0;
while (n > 0) {
// If rightmost bit is set, increment odd_count
if (n & 1) {
odd_count++;
}
// Right shift n
n >>= 1;
// If rightmost bit is set, increment even_count
if (n & 1) {
even_count++;
}
// Right shift n again
n >>= 1;
}
// Recursive call with the difference
return isMultipleOf3(Math.abs(odd_count - even_count));
}
let n = 9;
if (isMultipleOf3(n)) {
console.log("Yes");
}
else {
console.log("No");
}
[Approach 3] Using Modulo Division - O(1) Time and O(1) Space
The idea is to use the modulo operation to check divisibility by 3 efficiently. A number n is a multiple of 3 if n % 3 == 0, meaning it leaves no remainder when divided by 3.
C++
// C++ program to check if a number is a
// multiple of 3 using Modulo Division
#include <bits/stdc++.h>
using namespace std;
// Function to check if n is a multiple of 3
bool isMultipleOf3(int n) {
return (n % 3 == 0);
}
int main() {
int n = 9;
if (isMultipleOf3(n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program to check if a number is a
// multiple of 3 using Modulo Division
class GfG {
// Function to check if n is a multiple of 3
static boolean isMultipleOf3(int n) {
return (n % 3 == 0);
}
public static void main(String[] args) {
int n = 9;
if (isMultipleOf3(n)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
Python
# Python program to check if a number is a
# multiple of 3 using Modulo Division
# Function to check if n is a multiple of 3
def isMultipleOf3(n):
return (n % 3 == 0)
if __name__ == "__main__":
n = 9
if isMultipleOf3(n):
print("Yes")
else:
print("No")
C#
// C# program to check if a number is a
// multiple of 3 using Modulo Division
using System;
class GfG {
// Function to check if n is a multiple of 3
static bool isMultipleOf3(int n) {
return (n % 3 == 0);
}
static void Main() {
int n = 9;
if (isMultipleOf3(n)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to check if a number is a
// multiple of 3 using Modulo Division
// Function to check if n is a multiple of 3
function isMultipleOf3(n) {
return (n % 3 === 0);
}
// Driver Code
let n = 9;
if (isMultipleOf3(n)) {
console.log("Yes");
}
else {
console.log("No");
}