Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, ... n}

03 Nov, 2021
Given a number n, count all multiples of 3 and/or 5 in set of numbers from 1 to n.Examples:  
Input: n = 6
Output: 3
There are three multiples of 3 and/or 5 in {1, 2, 3, 4, 5, 6}

Input: n = 16
Output: 7
There are two multiples of 7 and/or 5 in {1, 2, .. 16}
The multiples are 3, 5, 6, 9, 10, 12, 15
We strongly recommend to minimize your browser and try this yourself first.The value of n/3 gives us number of multiples of 3, the value of n/5 gives us number of multiples of 5. But the important point is there are may be some common multiples which are multiples of both 3 and 5. We can get such multiples by using n/15. Following is the program to find count of multiples.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find count of multiples of 3 and 5 in {1, 2, 3, ..n}#include <iostream>using namespace std; unsigned countOfMultiples(unsigned n){   // Add multiples of 3 and 5.  Since common multiples are   // counted twice in n/3 + n/15, subtract common multiples   return (n/3 + n/5 - n/15);} // Driver program to test above functionint main(){   cout << countOfMultiples(6) << endl;   cout << countOfMultiples(16) << endl;   return 0;}
// Java program to find count of multiples// of 3 and 5 in {1, 2, 3, ..n}import java .io.*;class GFG {         static long countOfMultiples(long n)    {                 // Add multiples of 3 and 5.        // Since common multiples are        // counted twice in n/3 + n/15,        // subtract common multiples        return (n/3 + n/5 - n/15);    }         // Driver Code    static public void main (String[] args)    {        System.out.println(countOfMultiples(6));        System.out.println(countOfMultiples(16));    }} // This code is contributed by anuj_67.
# python program to find count# of multiples of 3 and 5 in# {1, 2, 3, ..n} def countOfMultiples(n):    # Add multiples of 3 and 5.    # Since common multiples are    # counted twice in n/3 + n/15,    # subtract common multiples    return (int(n/3) + int(n/5) - int(n/15));  # Driver program to test# above functionprint(countOfMultiples(6))print(countOfMultiples(16)) # This code is contributed by Sam007.
// C# program to find count of multiples// of 3 and 5 in {1, 2, 3, ..n}using System; public class GFG {         static uint countOfMultiples(uint n)    {        // Add multiples of 3 and 5.        // Since common multiples are        // counted twice in n/3 + n/15,        // subtract common multiples        return (n/3 + n/5 - n/15);    }         // Driver program to test above    // function    static public void Main ()    {        Console.WriteLine(countOfMultiples(6));                 Console.WriteLine(countOfMultiples(16)) ;    }} // This code is contributed by anuj_67.
<?php// PHP program to find count of// multiples of 3 and 5 in// {1, 2, 3, ..n} function countOfMultiples($n){         // Add multiples of 3 and 5.    // Since common multiples are    // counted twice in n/3 + n/15,    // subtract common multiples    return floor(floor($n / 3) +                 floor($n / 5) -                 floor($n / 15));} // Driver Codeecho countOfMultiples(6),"\n" ;echo countOfMultiples(16); // This code is contributed by nitin mittal?>
<script>    // Javascript program to find count of multiples    // of 3 and 5 in {1, 2, 3, ..n}         function countOfMultiples(n)    {        // Add multiples of 3 and 5.        // Since common multiples are        // counted twice in n/3 + n/15,        // subtract common multiples        return (parseInt(n/3, 10) + parseInt(n/5, 10) - parseInt(n/15, 10));    }         document.write(countOfMultiples(6) + "</br>");                 document.write(countOfMultiples(16) + "</br>") ;     </script>
Output: 
3
7
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Asif. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
Sam007
nitin mittal
vt_m
divyeshrabadiya07
surinderdawra388
subham348
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Check if a number is Palindrome
Count ways to reach the n'th stair
Fizz Buzz Implementation
Median of two sorted arrays of same size
Product of Array except itself
Find Union and Intersection of two unsorted arrays