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++
#include<iostream>
using namespace std;
int countMultiples( int n)
{
int res = 0;
for ( int i=1; i<=n; i++)
if (i%3==0 || i%7 == 0)
res++;
return res;
}
int main()
{
cout << "Count = " << countMultiples(25);
}
|
Java
import java.io.*;
class GFG
{
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;
}
public static void main (String[] args)
{
System.out.print( "Count = " );
System.out.println(countMultiples( 25 ));
}
}
|
Python3
def countMultiples(n):
res = 0 ;
for i in range ( 1 , n + 1 ):
if (i % 3 = = 0 or i % 7 = = 0 ):
res + = 1 ;
return res;
print ( "Count =" , countMultiples( 25 ));
|
C#
using System;
class GFG
{
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;
}
static public void Main ()
{
Console.Write( "Count = " );
Console.WriteLine(countMultiples(25));
}
}
|
PHP
<?php
function countMultiples( $n )
{
$res = 0;
for ( $i = 1; $i <= $n ; $i ++)
if ( $i % 3 == 0 || $i % 7 == 0)
$res ++;
return $res ;
}
echo "Count = " ,countMultiples(25);
?>
|
Javascript
<script>
function countMultiples(n)
{
let res = 0;
for (let i = 1; i <= n; i++)
if (i % 3 == 0 || i % 7 == 0)
res++;
return res;
}
document.write( "Count = " +countMultiples(25));
</script>
|
Time Complexity : O(n)
Auxiliary Space: O(1)
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++
#include<iostream>
using namespace std;
int countMultiples( int n)
{
return n/3 + n/7 -n/21;
}
int main()
{
cout << "Count = " << countMultiples(25);
}
|
Java
import java.io.*;
class GFG
{
static int countMultiples( int n)
{
return n / 3 + n / 7 - n / 21 ;
}
public static void main (String args [] )
{
System.out.println( "Count = " +
countMultiples( 25 ));
}
}
|
Python 3
def countMultiples(n):
return n / 3 + n / 7 - n / 21 ;
n = (( int )(countMultiples( 25 )));
print ( "Count =" , n);
|
C#
using System;
class GFG
{
static int countMultiples( int n)
{
return n / 3 + n / 7 - n / 21;
}
static public void Main ()
{
Console.WriteLine( "Count = " +
countMultiples(25));
}
}
|
PHP
<?php
function countMultiples( $n )
{
return floor ( $n / 3 + $n / 7 - $n / 21);
}
echo "Count = " , countMultiples(25);
?>
|
Javascript
<script>
function countMultiples(n)
{
return Math.floor(n / 3 + n / 7 - n / 21);
}
document.write( "Count = " +countMultiples(25));
</script>
|
Time Complexity : O(1)
Auxiliary Space: 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 review-team@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.
Please Login to comment...