给定两个数字x和y(x <= y),找出自然数的总数,例如i,为此i!可被x整除,但不能被y整除。
例子 :
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
对于所有大于或等于y的数字,其阶乘可被y整除。因此,要计算的所有自然数必须小于y。
一个简单的解决方案是从1到y-1进行迭代,对于每个数字,我都要检查i!可被x整除,而不可被y整除。如果我们采用这种幼稚的方法,我们将不会超过20岁!或21! (long long int将有其上限)
一个更好的解决方案是基于下面的帖子。
查找第一个自然因数,其乘因数可被x整除
我们找到第一个自然因数,其阶乘因数可被x整除!和y!使用上述方法。令阶乘可被x和y整除的第一个自然数分别为xf和yf 。我们的最终答案是yf – xf。该公式基于以下事实:如果i!可以被数字x整除,那么(i + 1)!,(i + 2)!,…也可以被x整除。
下面是实现。
C++
// C++ program to count natural numbers whose
// factorials are divisible by x but not y.
#include
using namespace std;
// GCD function to compute the greatest
// divisor among a and b
int gcd(int a, int b)
{
if ((a % b) == 0)
return b;
return gcd(b, a % b);
}
// Returns first number whose factorial
// is divisible by x.
int firstFactorialDivisibleNumber(int x)
{
int i = 1; // Result
int new_x = x;
for (i=1; i
Java
// Java program to count natural numbers whose
// factorials are divisible by x but not y.
class GFG
{
// GCD function to compute the greatest
// divisor among a and b
static int gcd(int a, int b)
{
if ((a % b) == 0)
return b;
return gcd(b, a % b);
}
// Returns first number whose factorial
// is divisible by x.
static int firstFactorialDivisibleNumber(int x)
{
int i = 1; // Result
int new_x = x;
for (i = 1; i < x; i++)
{
// Remove common factors
new_x /= gcd(i, new_x);
// We found first i.
if (new_x == 1)
break;
}
return i;
}
// Count of natural numbers whose factorials
// are divisible by x but not y.
static int countFactorialXNotY(int x, int y)
{
// Return difference between first natural
// number whose factorial is divisible by
// y and first natural number whose factorial
// is divisible by x.
return (firstFactorialDivisibleNumber(y) -
firstFactorialDivisibleNumber(x));
}
// Driver code
public static void main (String[] args)
{
int x = 15, y = 25;
System.out.print(countFactorialXNotY(x, y));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to count natural
# numbers whose factorials are
# divisible by x but not y.
# GCD function to compute the greatest
# divisor among a and b
def gcd(a, b):
if ((a % b) == 0):
return b
return gcd(b, a % b)
# Returns first number whose factorial
# is divisible by x.
def firstFactorialDivisibleNumber(x):
# Result
i = 1
new_x = x
for i in range(1, x):
# Remove common factors
new_x /= gcd(i, new_x)
# We found first i.
if (new_x == 1):
break
return i
# Count of natural numbers whose
# factorials are divisible by x but
# not y.
def countFactorialXNotY(x, y):
# Return difference between first
# natural number whose factorial
# is divisible by y and first
# natural number whose factorial
# is divisible by x.
return (firstFactorialDivisibleNumber(y) -
firstFactorialDivisibleNumber(x))
# Driver code
x = 15
y = 25
print(countFactorialXNotY(x, y))
# This code is contributed by Anant Agarwal.
C#
// C# program to count natural numbers whose
// factorials are divisible by x but not y.
using System;
class GFG
{
// GCD function to compute the greatest
// divisor among a and b
static int gcd(int a, int b)
{
if ((a % b) == 0)
return b;
return gcd(b, a % b);
}
// Returns first number whose factorial
// is divisible by x.
static int firstFactorialDivisibleNumber(int x)
{
int i = 1; // Result
int new_x = x;
for (i = 1; i < x; i++)
{
// Remove common factors
new_x /= gcd(i, new_x);
// We found first i.
if (new_x == 1)
break;
}
return i;
}
// Count of natural numbers whose factorials
// are divisible by x but not y.
static int countFactorialXNotY(int x, int y)
{
// Return difference between first natural
// number whose factorial is divisible by
// y and first natural number whose factorial
// is divisible by x.
return (firstFactorialDivisibleNumber(y) -
firstFactorialDivisibleNumber(x));
}
// Driver code
public static void Main ()
{
int x = 15, y = 25;
Console.Write(countFactorialXNotY(x, y));
}
}
// This code is contributed by nitin mittal.
PHP
Javascript
输出 :
5