给定整数N ,任务是找到1到N的数量计数,该数量可以被2到10的所有数字整除。
例子:
Input: N = 3000
Output: 1
2520 is the only number below 3000 which is divisible by all the numbers from 2 to 10.
Input: N = 2000
Output: 0
方法:让我们将2到10的数字分解。
2 = 2
3 = 3
4 = 22
5 = 5
6 = 2 * 3
7 = 7
8 = 23
9 = 32
10 = 2 * 5
如果一个数字可以被2到10的所有数字整除,则其因式分解应至少包含2的幂, 3的幂, 3的至少2的幂, 5的整数和7的至少1的幂。因此可以写成:
x * 23 * 32 * 5 * 7 i.e. x * 2520.
因此,任何可以被2520整除的数字都可以被2到10的所有数字整除。因此,此类数字的数量为N / 2520 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of numbers
// from 1 to n which are divisible by
// all the numbers from 2 to 10
int countNumbers(int n)
{
return (n / 2520);
}
// Driver code
int main()
{
int n = 3000;
cout << countNumbers(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the count of numbers
// from 1 to n which are divisible by
// all the numbers from 2 to 10
static int countNumbers(int n)
{
return (n / 2520);
}
// Driver code
public static void main(String args[])
{
int n = 3000;
System.out.println(countNumbers(n));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
# Function to return the count of numbers
# from 1 to n which are divisible by
# all the numbers from 2 to 10
def countNumbers(n):
return n // 2520
# Driver code
n = 3000
print(countNumbers(n))
# This code is contributed
# by Shrikant13
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the count of numbers
// from 1 to n which are divisible by
// all the numbers from 2 to 10
static int countNumbers(int n)
{
return (n / 2520);
}
// Driver code
public static void Main(String []args)
{
int n = 3000;
Console.WriteLine(countNumbers(n));
}
}
// This code is contributed by Arnab Kundu
PHP
Javascript
输出:
1