📜  查找第一个自然因数,其乘因数可被x整除

📅  最后修改于: 2021-04-29 07:30:24             🧑  作者: Mango

给定数字x,任务是找到第一个自然数i,其阶乘数可被x整除。
例子 :

Input  : x = 10
Output : 5
5 is the smallest number such that 
(5!) % 10 = 0

Input  : x = 16
Output : 6
6 is the smallest number such that 
(6!) % 16 = 0

一个简单的解决方案是从1迭代到x-1,然后为每个数字检查i!可被x整除。

C++
// A simple C++ program to find first natural
// number whose factorial divides x.
#include 
using namespace std;
 
// Returns first number whose factorial
// divides x.
int firstFactorialDivisibleNumber(int x)
{
    int i = 1; // Result
    int fact = 1;
    for (i = 1; i < x; i++) {
        fact = fact * i;
        if (fact % x == 0)
            break;
    }
 
    return i;
}
 
// Driver code
int main(void)
{
    int x = 16;
    cout << firstFactorialDivisibleNumber(x);
    return 0;
}


Java
// A simple Java program to find first natural
// number whose factorial divides x
class GFG {
 
    // Returns first number whose factorial
    // divides x.
    static int firstFactorialDivisibleNumber(int x)
    {
        int i = 1; // Result
        int fact = 1;
        for (i = 1; i < x; i++) {
            fact = fact * i;
            if (fact % x == 0)
                break;
        }
 
        return i;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int x = 16;
        System.out.print(firstFactorialDivisibleNumber(x));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# A simple python program to find
# first natural number whose
# factorial divides x.
 
# Returns first number whose
# factorial divides x.
def firstFactorialDivisibleNumber(x):
    i = 1; # Result
    fact = 1;
    for i in range(1, x):
        fact = fact * i
        if (fact % x == 0):
            break
    return i
 
# Driver code
x = 16
print(firstFactorialDivisibleNumber(x))
 
# This code is contributed
# by 29AjayKumar


C#
// A simple C# program to find first natural
// number whose factorial divides x
using System;
 
class GFG {
 
    // Returns first number whose factorial
    // divides x.
    static int firstFactorialDivisibleNumber(int x)
    {
        int i = 1; // Result
        int fact = 1;
        for (i = 1; i < x; i++) {
            fact = fact * i;
            if (fact % x == 0)
                break;
        }
 
        return i;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 16;
 
        Console.Write(
            firstFactorialDivisibleNumber(x));
    }
}
 
// This code is contributed by nitin mittal


PHP


Javascript


C++
// C++ program to find first natural number
// whose factorial divides x.
#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
// divides x.
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;
}
 
// Driver code
int main(void)
{
    int x = 16;
    cout << firstFactorialDivisibleNumber(x);
    return 0;
}


Java
// Efficient Java program to find first
// natural number whose factorial divides x.
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
    // divides 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;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int x = 16;
        System.out.print(firstFactorialDivisibleNumber(x));
    }
}
// This code is contributed by Anant Agarwal.


Python3
#  Python3 program to find first natural number
#  whose factorial divides x.
 
  
#  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
#  divides x.
def firstFactorialDivisibleNumber(x):
    i = 1 #  Result
    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
  
#  Driver code
def main():
    x = 16
    print(firstFactorialDivisibleNumber(x))
 
if __name__ == '__main__':
    main()
 
# This code is contributed by 29AjayKumar


C#
// Efficient C# program to find first
// natural number whose factorial
// divides x.
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 divides 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;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 16;
        Console.Write(
            firstFactorialDivisibleNumber(x));
    }
}
 
// This code is contributed by nitin mittal.


PHP


C++
// A cpp program for finding
// the Special Factorial Number
#include 
#include 
 
using boost::multiprecision::cpp_int;
using namespace std;
 
// function for calculating factoial
cpp_int fact(int n)
{
    cpp_int num = 1;
     
    for (int i = 1; i <= n; i++)
        num = num * i;
     
    return num;
}
 
// function for check Special_Factorial_Number
int Special_Factorial_Number(int k)
{
     
    for(int i = 1 ; i <= k ; i++ )
    {
        // call fact function and the
        // Modulo with k and check
        // if condition is TRUE then return i
        if ( ( fact (i) % k ) == 0 )
        {
            return i;
        }
    }
}
 
//driver function
int main()
{
    // taking input
    int k = 16;
     
    cout<


Java
// Java program for finding
// the Special Factorial Number
public class GFG {
 
// function for calculating factoial
    static int fact(int n) {
        int num = 1;
 
        for (int i = 1; i <= n; i++) {
            num = num * i;
        }
 
        return num;
    }
 
// function for check Special_Factorial_Number
    static int Special_Factorial_Number(int k) {
 
        for (int i = 1; i <= k; i++) {
            // call fact function and the
            // Modulo with k and check
            // if condition is TRUE then return i
            if (fact(i) % k == 0) {
                return i;
            }
        }
        return 0;
    }
 
//driver function
    public static void main(String[] args) {
        // taking input
        int k = 16;
        System.out.println(Special_Factorial_Number(k));
 
    }
}
 
/*This code is contributed by Rajput-Ji*/


Python3
# Python 3 program for finding
# the Special Factorial Number
 
# function for calculating factoial
def fact( n):
    num = 1
    for i in range(1, n + 1):
        num = num * i
    return num
 
# function for check Special_Factorial_Number
def Special_Factorial_Number(k):
     
    for i in range(1, k + 1):
         
        # call fact function and the
        # Modulo with k and check
        # if condition is TRUE then return i
        if (fact(i) % k == 0):
            return i
    return 0
 
# Driver Code
if __name__ == '__main__':
     
    # taking input
    k = 16
    print(Special_Factorial_Number(k))
 
# This code is contributed by Rajput-Ji


C#
// C# program for finding
// the Special Factorial Number
using System;
public class GFG{
 
 
// function for calculating factoial
    static int fact(int n) {
        int num = 1;
 
        for (int i = 1; i <= n; i++) {
            num = num * i;
        }
 
        return num;
    }
 
// function for check Special_Factorial_Number
    static int Special_Factorial_Number(int k) {
 
        for (int i = 1; i <= k; i++) {
            // call fact function and the
            // Modulo with k and check
            // if condition is TRUE then return i
            if (fact(i) % k == 0) {
                return i;
            }
        }
        return 0;
    }
 
//driver function
    public static void Main() {
        // taking input
        int k = 16;
        Console.WriteLine(Special_Factorial_Number(k));
 
    }
}
 
// This code is contributed by 29AjayKumar


PHP


输出 :

6

如果我们采用这种幼稚的方法,我们将不会超过20岁!或21! (long long int将有其上限)。
更好的解决方案可以避免溢出。该解决方案基于以下观察结果。

  • 如果我!可以被x整除,那么(i + 1)!,(i + 2)!,…也可以被x整除。
  • 对于数字x,所有阶乘i!当i> = x时可以被x整除。
  • 如果数字x是质数,则x之下的阶乘都不能将其除,因为x不能与较小的数字相乘而形成。

下面是算法

1) Run a loop for i = 1 to n-1
       
   a) Remove common factors
      new_x /= gcd(i, new_x);

   b) Check if we found first i.
      if (new_x == 1)
          break;

2) Return i

下面是上述想法的实现:

C++

// C++ program to find first natural number
// whose factorial divides x.
#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
// divides x.
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;
}
 
// Driver code
int main(void)
{
    int x = 16;
    cout << firstFactorialDivisibleNumber(x);
    return 0;
}

Java

// Efficient Java program to find first
// natural number whose factorial divides x.
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
    // divides 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;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int x = 16;
        System.out.print(firstFactorialDivisibleNumber(x));
    }
}
// This code is contributed by Anant Agarwal.

Python3

#  Python3 program to find first natural number
#  whose factorial divides x.
 
  
#  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
#  divides x.
def firstFactorialDivisibleNumber(x):
    i = 1 #  Result
    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
  
#  Driver code
def main():
    x = 16
    print(firstFactorialDivisibleNumber(x))
 
if __name__ == '__main__':
    main()
 
# This code is contributed by 29AjayKumar

C#

// Efficient C# program to find first
// natural number whose factorial
// divides x.
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 divides 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;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 16;
        Console.Write(
            firstFactorialDivisibleNumber(x));
    }
}
 
// This code is contributed by nitin mittal.

的PHP


输出 :

6

使用Boost库的另一种方法
(感谢ajay0007贡献了这种方法)
在这里,我们使用boost库来有效地计算阶乘的值。
先决条件:boost-multiprecision-library

C++

// A cpp program for finding
// the Special Factorial Number
#include 
#include 
 
using boost::multiprecision::cpp_int;
using namespace std;
 
// function for calculating factoial
cpp_int fact(int n)
{
    cpp_int num = 1;
     
    for (int i = 1; i <= n; i++)
        num = num * i;
     
    return num;
}
 
// function for check Special_Factorial_Number
int Special_Factorial_Number(int k)
{
     
    for(int i = 1 ; i <= k ; i++ )
    {
        // call fact function and the
        // Modulo with k and check
        // if condition is TRUE then return i
        if ( ( fact (i) % k ) == 0 )
        {
            return i;
        }
    }
}
 
//driver function
int main()
{
    // taking input
    int k = 16;
     
    cout<

Java

// Java program for finding
// the Special Factorial Number
public class GFG {
 
// function for calculating factoial
    static int fact(int n) {
        int num = 1;
 
        for (int i = 1; i <= n; i++) {
            num = num * i;
        }
 
        return num;
    }
 
// function for check Special_Factorial_Number
    static int Special_Factorial_Number(int k) {
 
        for (int i = 1; i <= k; i++) {
            // call fact function and the
            // Modulo with k and check
            // if condition is TRUE then return i
            if (fact(i) % k == 0) {
                return i;
            }
        }
        return 0;
    }
 
//driver function
    public static void main(String[] args) {
        // taking input
        int k = 16;
        System.out.println(Special_Factorial_Number(k));
 
    }
}
 
/*This code is contributed by Rajput-Ji*/

Python3

# Python 3 program for finding
# the Special Factorial Number
 
# function for calculating factoial
def fact( n):
    num = 1
    for i in range(1, n + 1):
        num = num * i
    return num
 
# function for check Special_Factorial_Number
def Special_Factorial_Number(k):
     
    for i in range(1, k + 1):
         
        # call fact function and the
        # Modulo with k and check
        # if condition is TRUE then return i
        if (fact(i) % k == 0):
            return i
    return 0
 
# Driver Code
if __name__ == '__main__':
     
    # taking input
    k = 16
    print(Special_Factorial_Number(k))
 
# This code is contributed by Rajput-Ji

C#

// C# program for finding
// the Special Factorial Number
using System;
public class GFG{
 
 
// function for calculating factoial
    static int fact(int n) {
        int num = 1;
 
        for (int i = 1; i <= n; i++) {
            num = num * i;
        }
 
        return num;
    }
 
// function for check Special_Factorial_Number
    static int Special_Factorial_Number(int k) {
 
        for (int i = 1; i <= k; i++) {
            // call fact function and the
            // Modulo with k and check
            // if condition is TRUE then return i
            if (fact(i) % k == 0) {
                return i;
            }
        }
        return 0;
    }
 
//driver function
    public static void Main() {
        // taking input
        int k = 16;
        Console.WriteLine(Special_Factorial_Number(k));
 
    }
}
 
// This code is contributed by 29AjayKumar

的PHP


输出 :

6