Count natural numbers whose factorials are divisible by x but not y
Given two numbers x and y (x <= y), find out the total number of natural numbers, say i, for which i! is divisible by x but not y.
Examples :
Input : x = 2, y = 5
Output : 3
There are three numbers, 2, 3 and 4
whose factorials are divisible by x
but not y.
Input: x = 15, y = 25
Output: 5
5! = 120 % 15 = 0 && 120 % 25 != 0
6! = 720 % 15 = 0 && 720 % 25 != 0
7! = 5040 % 15 = 0 && 5040 % 25 != 0
8! = 40320 % 15 = 0 && 40320 % 25 != 0
9! = 362880 % 15 = 0 && 362880 % 25 != 0
So total count = 5
Input: x = 10, y = 15
Output: 0
For all numbers greater than or equal to y, their factorials are divisible by y. So all natural numbers to be counted must be less than y.
A simple solution is to iterate from 1 to y-1 and for every number i check if i! is divisible by x and not divisible by y. If we apply this naive approach, we wouldn’t go above 20! or 21! (long long int will have its upper limit)
A better solution is based on below post.
Find the first natural number whose factorial is divisible by x
We find the first natural numbers whose factorials are divisible by x! and y! using above approach. Let the first natural numbers whose factorials are divisible by x and y be xf and yf respectively. Our final answer would be yf – xf. This formula is based on the fact that if i! is divisible by a number x, then (i+1)!, (i+2)!, … are also divisible by x.
Below is the implementation.
C++
#include<bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if ((a % b) == 0)
return b;
return gcd(b, a % b);
}
int firstFactorialDivisibleNumber( int x)
{
int i = 1;
int new_x = x;
for (i=1; i<x; i++)
{
new_x /= gcd(i, new_x);
if (new_x == 1)
break ;
}
return i;
}
int countFactorialXNotY( int x, int y)
{
return (firstFactorialDivisibleNumber(y) -
firstFactorialDivisibleNumber(x));
}
int main( void )
{
int x = 15, y = 25;
cout << countFactorialXNotY(x, y);
return 0;
}
|
Java
class GFG
{
static int gcd( int a, int b)
{
if ((a % b) == 0 )
return b;
return gcd(b, a % b);
}
static int firstFactorialDivisibleNumber( int x)
{
int i = 1 ;
int new_x = x;
for (i = 1 ; i < x; i++)
{
new_x /= gcd(i, new_x);
if (new_x == 1 )
break ;
}
return i;
}
static int countFactorialXNotY( int x, int y)
{
return (firstFactorialDivisibleNumber(y) -
firstFactorialDivisibleNumber(x));
}
public static void main (String[] args)
{
int x = 15 , y = 25 ;
System.out.print(countFactorialXNotY(x, y));
}
}
|
Python3
def gcd(a, b):
if ((a % b) = = 0 ):
return b
return gcd(b, a % b)
def firstFactorialDivisibleNumber(x):
i = 1
new_x = x
for i in range ( 1 , x):
new_x / = gcd(i, new_x)
if (new_x = = 1 ):
break
return i
def countFactorialXNotY(x, y):
return (firstFactorialDivisibleNumber(y) -
firstFactorialDivisibleNumber(x))
x = 15
y = 25
print (countFactorialXNotY(x, y))
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
if ((a % b) == 0)
return b;
return gcd(b, a % b);
}
static int firstFactorialDivisibleNumber( int x)
{
int i = 1;
int new_x = x;
for (i = 1; i < x; i++)
{
new_x /= gcd(i, new_x);
if (new_x == 1)
break ;
}
return i;
}
static int countFactorialXNotY( int x, int y)
{
return (firstFactorialDivisibleNumber(y) -
firstFactorialDivisibleNumber(x));
}
public static void Main ()
{
int x = 15, y = 25;
Console.Write(countFactorialXNotY(x, y));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if (( $a % $b ) == 0)
return $b ;
return gcd( $b , $a % $b );
}
function firstFactorialDivisibleNumber( $x )
{
$i = 1;
$new_x = $x ;
for ( $i = 1; $i < $x ; $i ++)
{
$new_x /= gcd( $i , $new_x );
if ( $new_x == 1)
break ;
}
return $i ;
}
function countFactorialXNotY( $x , $y )
{
return (firstFactorialDivisibleNumber( $y ) -
firstFactorialDivisibleNumber( $x ));
}
$x = 15; $y = 25;
echo (countFactorialXNotY( $x , $y ));
?>
|
Javascript
<script>
function gcd(a, b)
{
if ((a % b) == 0)
return b;
return gcd(b, a % b);
}
function firstFactorialDivisibleNumber(x)
{
let i = 1;
let new_x = x;
for (i = 1; i < x; i++)
{
new_x /= gcd(i, new_x);
if (new_x == 1)
break ;
}
return i;
}
function countFactorialXNotY(x, y)
{
return (firstFactorialDivisibleNumber(y) -
firstFactorialDivisibleNumber(x));
}
let x = 15, y = 25;
document.write(countFactorialXNotY(x, y));
</script>
|
Output :
5
Last Updated :
26 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...