Multiples of 3 or 7

Given a positive integer n, find count of all multiples of 3 or 7 less than or equal to n.

Examples :

Input : n = 10
Output : Count = 4
The multiples are 3, 6, 7 and 9

Input : n = 25
Output : Count = 10
The multiples are 3, 6, 7, 9, 12, 14, 15, 18, 21 and 24

A Simple Solution is to iterate over all numbers from 1 to n and increment count whenever a number is a multiple of 3 or 7 or both.



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple C++ program to find count of all
// numbers that multiples
#include<iostream>
using namespace std;
  
// Returns count of all numbers smaller than
// or equal to n and multples of 3 or 7 or both
int countMultiples(int n)
{
    int res = 0;
    for (int i=1; i<=n; i++)
       if (i%3==0 || i%7 == 0)
           res++;
  
    return res;
}
  
// Driver code
int main()
{
   cout << "Count = " << countMultiples(25);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple Java program to 
// find count of all numbers 
// that multiples
import java.io.*;
  
class GFG 
{
      
// Returns count of all numbers 
// smaller than or equal to n 
// and multples of 3 or 7 or both
static int countMultiples(int n)
{
    int res = 0;
    for (int i = 1; i <= n; i++)
    if (i % 3 == 0 || i % 7 == 0)
        res++;
  
    return res;
}
  
// Driver Code
public static void main (String[] args)
{
    System.out.print("Count = ");
    System.out.println(countMultiples(25));
}
}
  
// This code is contributed by m_kit

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# A Simple Python3 program to 
# find count of all numbers 
# that multiples
  
# Returns count of all numbers 
# smaller than or equal to n 
# and multples of 3 or 7 or both
def countMultiples(n):
    res = 0;
    for i in range(1, n + 1):
        if (i % 3 == 0 or i % 7 == 0):
            res += 1;
   
    return res;
  
# Driver code
print("Count =", countMultiples(25));
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A Simple C# program to 
// find count of all numbers 
// that are multiples of 3 or 7
using System;
  
class GFG
{
      
// Returns count of all  
// numbers smaller than 
// or equal to n  and 
// are multples of 3 or 
// 7 or both
static int countMultiples(int n)
{
    int res = 0;
    for (int i = 1; i <= n; i++)
    if (i % 3 == 0 || i % 7 == 0)
        res++;
  
    return res;
}
  
// Driver Code
static public void Main ()
{
    Console.Write("Count = ");
    Console.WriteLine(countMultiples(25));
}
}
  
// This code is contributed by ajit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A Simple PHP program to find count 
// of all numbers that multiples
  
// Returns count of all numbers 
// smaller than or equal to n 
// and multples of 3 or 7 or both
function countMultiples($n)
{
    $res = 0;
    for ($i = 1; $i <= $n; $i++)
    if ($i % 3 == 0 || $i % 7 == 0)
        $res++;
  
    return $res;
}
  
// Driver code
echo "Count = " ,countMultiples(25);
  
// This code is contributed by aj_36
?>
  

chevron_right



Output :

Count = 10

Time Complexity : O(n)

An efficient solution can solve the above problem in O(1) time. The idea is to count multiples of 3 and add multiples of 7, then subtract multiples of 21 because these are counted twice.

count = n/3 + n/7 - n/21

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// A better C++ program to find count of all
// numbers that multiples
#include<iostream>
using namespace std;
  
// Returns count of all numbers smaller than
// or equal to n and multples of 3 or 7 or both
int countMultiples(int n)
{
   return n/3 + n/7 -n/21;
}
  
// Driver code
int main()
{
   cout << "Count = " << countMultiples(25);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// A better Java program to 
// find count of all numbers
// that multiples
import java.io.*;
  
class GFG 
{
      
// Returns count of all numbers
// smaller than or equal to n
// and multples of 3 or 7 or both
static int countMultiples(int n)
{
    return n / 3 + n / 7 - n / 21;
}
  
// Driver code
public static void main (String args [] ) 
{
    System.out.println("Count = "
                        countMultiples(25));
}
}
  
// This code is contributed by aj_36 

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find count of 
# all numbers that multiples
  
# Returns count of all numbers 
# smaller than or equal to n and 
# multples of 3 or 7 or both
def countMultiples(n):
    return n / 3 + n / 7 - n / 21;
  
# Driver code
n = ((int)(countMultiples(25)));
print("Count =", n);
  
# This code is contributed 
# by Shivi_Aggarwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// A better Java program to 
// find count of all numbers
// that multiples
using System;
  
class GFG
{
      
// Returns count of all numbers
// smaller than or equal to n
// and multples of 3 or 7 or both
static int countMultiples(int n)
{
    return n / 3 + n / 7 - n / 21;
}
  
// Driver Code
static public void Main ()
{
    Console.WriteLine("Count = "
                       countMultiples(25));
}
}
  
// This code is contributed by m_kit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// A better PHP program to find count 
// of all numbers that multiples
  
// Returns count of all numbers 
// smaller than or equal to n 
// and multples of 3 or 7 or both
function countMultiples($n)
{
return floor($n / 3 + $n / 7 - $n / 21);
}
  
// Driver code
echo "Count = " , countMultiples(25);
  
// This code is contributed by ajit
?>

chevron_right



Output :

Count = 10

Time Complexity : O(1)

Exercise:
Now try the problem of finding sum of all numbers less than or equal to n and multiples of 3 or 7 or both in O(1) time.

This article is contributed by Saurabh Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.