Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3

Given two integers L and R. The task is to find the count of all even numbers in the range [L, R] whose sum of digits is divisible by 3.

Examples:

Input: L = 18, R = 36
Output: 4
18, 24, 30, 36 are the only numbers in the range [18, 36] which are even and whose sum of digits is divisible by 3.



Input: L = 7, R = 11
Output: 0
There is no number in the range [7, 11] which is even and whose sum of digits is divisible by 3.

Naive approach: Initialize count = 0 and for every number in the range [L, R], check if the number is divisible by 2 and sum of its digits is divisible by 3. If yes then increment the count. Print the count in the end.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the
// sum of digits of x
int sumOfDigits(int x)
{
    int sum = 0;
    while (x != 0) {
        sum += x % 10;
        x = x / 10;
    }
    return sum;
}
  
// Function to return the count
// of required numbers
int countNumbers(int l, int r)
{
    int count = 0;
    for (int i = l; i <= r; i++) {
  
        // If i is divisible by 2 and
        // sum of digits of i is divisible by 3
        if (i % 2 == 0 && sumOfDigits(i) % 3 == 0)
            count++;
    }
  
    // Return the required count
    return count;
}
  
// Driver code
int main()
{
    int l = 1000, r = 6000;
    cout << countNumbers(l, r);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class GFG
{
  
// Function to return the 
// sum of digits of x 
static int sumOfDigits(int x) 
    int sum = 0
    while (x != 0
    
        sum += x % 10
        x = x / 10
    
    return sum; 
  
// Function to return the count 
// of required numbers 
static int countNumbers(int l, int r) 
    int count = 0
    for (int i = l; i <= r; i++)
    
  
        // If i is divisible by 2 and 
        // sum of digits of i is divisible by 3 
        if (i % 2 == 0 && sumOfDigits(i) % 3 == 0
            count++; 
    
  
    // Return the required count 
    return count; 
  
// Driver code 
public static void main(String args[]) 
    int l = 1000, r = 6000
    System.out.println(countNumbers(l, r)); 
}
  
// This code is contributed by Arnab Kundu
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# python implementation of the approach
  
# Function to return the
# sum of digits of x
def sumOfDigits(x):
    sum = 0
    while x != 0:
        sum += x % 10
        x = x//10
    return sum
  
  
# Function to return the count
# of required numbers
def countNumbers(l, r):
    count = 0
    for i in range(l, r + 1):
  
        # If i is divisible by 2 and
        # sum of digits of i is divisible by 3
        if i % 2 == 0 and sumOfDigits(i) % 3 == 0:
            count += 1
    return count
  
# Driver code
l = 1000; r = 6000
print(countNumbers(l, r))
  
# This code is contributed by Shrikant13
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System;
  
class GFG
{
  
// Function to return the 
// sum of digits of x 
static int sumOfDigits(int x) 
    int sum = 0; 
    while (x != 0) 
    
        sum += x % 10; 
        x = x / 10; 
    
    return sum; 
  
// Function to return the count 
// of required numbers 
static int countNumbers(int l, int r) 
    int count = 0; 
    for (int i = l; i <= r; i++)
    
  
        // If i is divisible by 2 and 
        // sum of digits of i is divisible by 3 
        if (i % 2 == 0 && sumOfDigits(i) % 3 == 0) 
            count++; 
    
  
    // Return the required count 
    return count; 
  
// Driver code 
public static void Main() 
    int l = 1000, r = 6000; 
    Console.WriteLine(countNumbers(l, r)); 
}
  
// This code is contributed by Code_Mech.
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to return the sum of
// digits of x
function sumOfDigits( $x)
{
    $sum = 0;
    while ($x != 0)
    {
        $sum += $x % 10;
        $x = $x / 10;
    }
    return $sum;
}
  
// Function to return the count
// of required numbers
function countNumbers($l, $r)
{
    $count = 0;
    for ($i = $l; $i <= $r; $i++) 
    {
  
        // If i is divisible by 2 and
        // sum of digits of i is divisible by 3
        if ($i % 2 == 0 && 
            sumOfDigits($i) % 3 == 0)
            $count++;
    }
  
    // Return the required count
    return $count;
}
  
// Driver code
$l = 1000;
$r = 6000;
echo countNumbers($l, $r);
  
// This code is contributed by princiraj1992
?>
chevron_right

Output:
834

Time Complexity: O(r – l)

Efficient approach:

  1. We have to check that the number is divisible by 2.
  2. We have to check that the sum of digit is divisible by 3 which means that the number is divisible by 3.

So overall we have to check if a number is divisible by both 2 and 3, and since both 2 and 3 are co prime so we just have to check if a number is divisible by their product i.e. 6.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the count
// of required numbers
int countNumbers(int l, int r)
{
  
    // Count of numbers in range
    // which are divisible by 6
    return ((r / 6) - (l - 1) / 6);
}
  
// Driver code
int main()
{
    int l = 1000, r = 6000;
    cout << countNumbers(l, r);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
  
    // Count of numbers in range
    // which are divisible by 6
    return ((r / 6) - (l - 1) / 6);
}
  
// Driver code
public static void main(String[] args)
{
    int l = 1000, r = 6000;
    System.out.println(countNumbers(l, r));
}
}
  
// This code is contributed by princiraj1992
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the count 
# of required numbers 
def countNumbers(l, r) :
  
    # Count of numbers in range 
    # which are divisible by 6 
    return ((r // 6) - (l - 1) // 6); 
  
# Driver code 
if __name__ == "__main__" :
  
    l = 1000; r = 6000
    print(countNumbers(l, r)); 
  
# This code is contributed by Ryuga
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach 
using System;     
  
class GFG
{
  
// Function to return the count
// of required numbers
static int countNumbers(int l, int r)
{
  
    // Count of numbers in range
    // which are divisible by 6
    return ((r / 6) - (l - 1) / 6);
}
  
// Driver code
public static void Main(String[] args)
{
    int l = 1000, r = 6000;
    Console.WriteLine(countNumbers(l, r));
}
}
  
// This code contributed by Rajput-Ji
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to return the count
// of required numbers
function countNumbers($l, $r)
{
  
    // Count of numbers in range
    // which are divisible by 6
    return ((int)($r / 6) - 
            (int)(($l - 1) / 6));
}
  
// Driver code
$l = 1000; $r = 6000;
echo(countNumbers($l, $r));
  
// This code is contributed 
// by Code_Mech.
?>
chevron_right

Output:
834

Time Complexity: O(1)




Check out this Author's contributed articles.

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.





Article Tags :
Practice Tags :