Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, … n}
Last Updated :
05 Oct, 2023
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
Brute Force:
A brute force approach to solve this problem would be to iterate through all the numbers from 1 to n and check if each number is a multiple of 3 or 5. If it is, increment a counter.
Implementation of the above approach:
C++
#include <iostream>
using namespace std;
unsigned countOfMultiples(unsigned n)
{
unsigned count = 0;
for (unsigned i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 5 == 0) {
count++;
}
}
return count;
}
int main()
{
cout << countOfMultiples(6) << endl;
cout << countOfMultiples(16) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int countOfMultiples( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= n; i++) {
if (i % 3 == 0 || i % 5 == 0 ) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
System.out.println(countOfMultiples( 6 ));
System.out.println(countOfMultiples( 16 ));
}
}
|
Python3
def countOfMultiples(n):
count = 0
for i in range ( 1 , n + 1 ):
if i % 3 = = 0 or i % 5 = = 0 :
count + = 1
return count
if __name__ = = "__main__" :
print (countOfMultiples( 6 ))
print (countOfMultiples( 16 ))
|
C#
using System;
public class MainClass
{
public static int CountOfMultiples( int n)
{
int count = 0;
for ( int i = 1; i <= n; i++)
{
if (i % 3 == 0 || i % 5 == 0)
{
count++;
}
}
return count;
}
public static void Main( string [] args)
{
Console.WriteLine(CountOfMultiples(6));
Console.WriteLine(CountOfMultiples(16));
}
}
|
Javascript
function countOfMultiples(n) {
let count = 0;
for (let i = 1; i <= n; i++) {
if (i % 3 == 0 || i % 5 == 0) {
count++;
}
}
return count;
}
console.log(countOfMultiples(6));
console.log(countOfMultiples(16));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
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++
#include <iostream>
using namespace std;
unsigned countOfMultiples(unsigned n)
{
return (n/3 + n/5 - n/15);
}
int main()
{
cout << countOfMultiples(6) << endl;
cout << countOfMultiples(16) << endl;
return 0;
}
|
Java
import java .io.*;
class GFG {
static long countOfMultiples( long n)
{
return (n/ 3 + n/ 5 - n/ 15 );
}
static public void main (String[] args)
{
System.out.println(countOfMultiples( 6 ));
System.out.println(countOfMultiples( 16 ));
}
}
|
Python3
def countOfMultiples(n):
return ( int (n / 3 ) + int (n / 5 ) - int (n / 15 ));
print (countOfMultiples( 6 ))
print (countOfMultiples( 16 ))
|
C#
using System;
public class GFG {
static uint countOfMultiples( uint n)
{
return (n/3 + n/5 - n/15);
}
static public void Main ()
{
Console.WriteLine(countOfMultiples(6));
Console.WriteLine(countOfMultiples(16)) ;
}
}
|
Javascript
<script>
function countOfMultiples(n)
{
return (parseInt(n/3, 10) + parseInt(n/5, 10) - parseInt(n/15, 10));
}
document.write(countOfMultiples(6) + "</br>" );
document.write(countOfMultiples(16) + "</br>" ) ;
</script>
|
PHP
<?php
function countOfMultiples( $n )
{
return floor ( floor ( $n / 3) +
floor ( $n / 5) -
floor ( $n / 15));
}
echo countOfMultiples(6), "\n" ;
echo countOfMultiples(16);
?>
|
Output:
3
7
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...