📜  数阶乘数程序

📅  最后修改于: 2021-04-29 09:53:16             🧑  作者: Mango

非负整数的阶乘是所有小于或等于n的整数的乘积。例如,阶乘6是720的6 * 5 * 4 * 3 * 2 * 1。

递归解决方案:
阶乘可以使用以下递归公式进行计算。

n! = n * (n-1)!
  n! = 1 if n = 0 or n = 1 

以下是阶乘的实现。

C++
// C++ program to find factorial of given number
#include 
using namespace std;
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
  
// Driver code
int main()
{
    int num = 5;
    cout << "Factorial of "
         << num << " is " << factorial(num) << endl;
    return 0;
}
  
// This code is contributed by Shivi_Aggarwal


C
// C program to find factorial of given number
#include 
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
  
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}


Java
// Java program to find factorial of given number
class Test {
    // method to find factorial of given number
    static int factorial(int n)
    {
        if (n == 0)
            return 1;
  
        return n * factorial(n - 1);
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}


Python3
# Python 3 program to find 
# factorial of given number
  
# Function to find factorial of given number
def factorial(n):
       
    if n == 0:
        return 1
      
    return n * factorial(n-1)
   
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
   
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to find factorial
// of given number
using System;
  
class Test {
    // method to find factorial
    // of given number
    static int factorial(int n)
    {
        if (n == 0)
            return 1;
  
        return n * factorial(n - 1);
    }
  
    // Driver method
    public static void Main()
    {
        int num = 5;
        Console.WriteLine("Factorial of "
                          + num + " is " + factorial(5));
    }
}
  
// This code is contributed by vt_m


PHP


C++
// C++ program for factorial of a number
#include 
using namespace std;
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    int res = 1, i;
    for (i = 2; i <= n; i++)
        res *= i;
    return res;
}
  
// Driver code
int main()
{
    int num = 5;
    cout << "Factorial of "
         << num << " is "
         << factorial(num) << endl;
    return 0;
}
  
// This code is contributed by Shivi_Aggarwal


C
#include 
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    int res = 1, i;
    for (i = 2; i <= n; i++)
        res *= i;
    return res;
}
  
int main()
{
    int num = 5;
    printf(
        "Factorial of %d is %d", num, factorial(num));
    return 0;
}


Java
// Java program to find factorial of given number
class Test {
    // Method to find factorial of the given number
    static int factorial(int n)
    {
        int res = 1, i;
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}


Python3
# Python 3 program to find 
# factorial of given number
  
# Function to find factorial of given number
def factorial(n):
       
    res = 1
      
    for i in range(2, n+1):
        res *= i
    return res
  
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
   
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to find
// factorial of given number
using System;
  
class Test {
    // Method to find factorial
    // of given number
    static int factorial(int n)
    {
        int res = 1, i;
  
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
  
    // Driver method
    public static void Main()
    {
        int num = 5;
        Console.WriteLine(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}
  
// This code is contributed by vt_m


PHP


C
// C program for factorial of a number
#include 
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
     if(n == 0)
          return 1;
    int i = n, fact = 1;
    while (n / i != n) {
        fact = fact * i;
        i--;
    }
    return fact;
}
  
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}


C++
// C++ program for factorial of a number
#include 
using namespace std;
  
// function to find factorial of given
// number using while loop
unsigned int factorial(unsigned int n)
{
    if(n == 0)
          return 1;
    int i = n, fact = 1;
    while (n / i != n) {
        fact = fact * i;
        i--;
    }
    return fact;
}
  
// Driver code
int main()
{
    int num = 5;
    cout << "Factorial of "
         << num << " is "
         << factorial(num) << endl;
    return 0;
}
// This code is contributed by Shivi_Aggarwal


Java
// Java program to find factorial of given number
class Test {
    // Method to find factorial of the given number
    static int factorial(int n)
    {
        if(n == 0)
           return 1;
        int i = n, fact = 1;
        while (n / i != n) {
            fact = fact * i;
            i--;
        }
        return fact;
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}


Python3
# Python 3 program to find 
# factorial of given number
  
# Function to find factorial of given number
def factorial(n):
    if(n == 0):
       return 1
    i = n
    fact = 1
      
    while(n / i != n):
        fact = fact * i
        i -= 1
          
    return fact
  
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
   
# This code is contributed by Smitha Dinesh Semwal


C#
// C# program to find
// factorial of given number
using System;
  
class Test {
    // Method to find factorial
    // of given number
    static int factorial(int n)
    {
        if(n == 0)
            return 1;
        int i = n, fact = 1;
        while (n / i != n) {
            fact = fact * i;
            i--;
        }
        return fact;
    }
  
    // Driver method
    public static void Main()
    {
        int num = 5;
        Console.WriteLine(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}


C++
// C++ program to find factorial of given number
#include 
  
int factorial(int n)
{
    // single line to find factorial
    return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
  
// Driver Code
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}
  
// This code is contributed by  Rithika palaniswamy.


Java
// Java program to find factorial
// of given number
class Factorial {
  
    int factorial(int n)
    {
  
        // single line to find factorial
        return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
    }
  
    // Driver Code
    public static void main(String args[])
    {
        Factorial obj = new Factorial();
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + obj.factorial(num));
    }
}
  
// This code is contributed by Anshika Goyal.


Python3
# Python 3 program to find
# factorial of given number
  
def factorial(n):
  
    # single line to find factorial
    return 1 if (n == 1 or n == 0) else n * factorial(n - 1) 
  
  
# Driver Code
num = 5
print ("Factorial of", num, "is",
      factorial(num))
  
# This code is contributed
# by Smitha Dinesh Semwal.


C#
// C# program to find factorial
// of the given number
using System;
  
class Factorial {
  
    int factorial(int n)
    {
  
        // single line to find factorial
        return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
    }
  
    // Driver Code
    public static void Main()
    {
        Factorial obj = new Factorial();
        int num = 5;
  
        Console.WriteLine(
            "Factorial of " + num
            + " is " + obj.factorial(num));
    }
}
  
// This code is contributed by vt_m.


PHP


输出:

Factorial of 5 is 120

迭代解决方案:
阶乘也可以迭代计算,因为递归对于大量数字而言可能是昂贵的。在这里,我们展示了同时使用for和while循环的迭代方法。
使用For循环

C++

// C++ program for factorial of a number
#include 
using namespace std;
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    int res = 1, i;
    for (i = 2; i <= n; i++)
        res *= i;
    return res;
}
  
// Driver code
int main()
{
    int num = 5;
    cout << "Factorial of "
         << num << " is "
         << factorial(num) << endl;
    return 0;
}
  
// This code is contributed by Shivi_Aggarwal

C

#include 
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
    int res = 1, i;
    for (i = 2; i <= n; i++)
        res *= i;
    return res;
}
  
int main()
{
    int num = 5;
    printf(
        "Factorial of %d is %d", num, factorial(num));
    return 0;
}

Java

// Java program to find factorial of given number
class Test {
    // Method to find factorial of the given number
    static int factorial(int n)
    {
        int res = 1, i;
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}

Python3

# Python 3 program to find 
# factorial of given number
  
# Function to find factorial of given number
def factorial(n):
       
    res = 1
      
    for i in range(2, n+1):
        res *= i
    return res
  
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
   
# This code is contributed by Smitha Dinesh Semwal

C#

// C# program to find
// factorial of given number
using System;
  
class Test {
    // Method to find factorial
    // of given number
    static int factorial(int n)
    {
        int res = 1, i;
  
        for (i = 2; i <= n; i++)
            res *= i;
        return res;
    }
  
    // Driver method
    public static void Main()
    {
        int num = 5;
        Console.WriteLine(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}
  
// This code is contributed by vt_m

的PHP


输出 :

Factorial of 5 is 120

使用While循环

C

// C program for factorial of a number
#include 
  
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
     if(n == 0)
          return 1;
    int i = n, fact = 1;
    while (n / i != n) {
        fact = fact * i;
        i--;
    }
    return fact;
}
  
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}

C++

// C++ program for factorial of a number
#include 
using namespace std;
  
// function to find factorial of given
// number using while loop
unsigned int factorial(unsigned int n)
{
    if(n == 0)
          return 1;
    int i = n, fact = 1;
    while (n / i != n) {
        fact = fact * i;
        i--;
    }
    return fact;
}
  
// Driver code
int main()
{
    int num = 5;
    cout << "Factorial of "
         << num << " is "
         << factorial(num) << endl;
    return 0;
}
// This code is contributed by Shivi_Aggarwal

Java

// Java program to find factorial of given number
class Test {
    // Method to find factorial of the given number
    static int factorial(int n)
    {
        if(n == 0)
           return 1;
        int i = n, fact = 1;
        while (n / i != n) {
            fact = fact * i;
            i--;
        }
        return fact;
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}

Python3

# Python 3 program to find 
# factorial of given number
  
# Function to find factorial of given number
def factorial(n):
    if(n == 0):
       return 1
    i = n
    fact = 1
      
    while(n / i != n):
        fact = fact * i
        i -= 1
          
    return fact
  
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
   
# This code is contributed by Smitha Dinesh Semwal

C#

// C# program to find
// factorial of given number
using System;
  
class Test {
    // Method to find factorial
    // of given number
    static int factorial(int n)
    {
        if(n == 0)
            return 1;
        int i = n, fact = 1;
        while (n / i != n) {
            fact = fact * i;
            i--;
        }
        return fact;
    }
  
    // Driver method
    public static void Main()
    {
        int num = 5;
        Console.WriteLine(
            "Factorial of " + num
            + " is " + factorial(5));
    }
}

输出 :

Factorial of 5 is 120

上述迭代解决方案的时间复杂度为O(n)。

一线解决方案(使用三元运算符):

C++

// C++ program to find factorial of given number
#include 
  
int factorial(int n)
{
    // single line to find factorial
    return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
}
  
// Driver Code
int main()
{
    int num = 5;
    printf("Factorial of %d is %d", num, factorial(num));
    return 0;
}
  
// This code is contributed by  Rithika palaniswamy.

Java

// Java program to find factorial
// of given number
class Factorial {
  
    int factorial(int n)
    {
  
        // single line to find factorial
        return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
    }
  
    // Driver Code
    public static void main(String args[])
    {
        Factorial obj = new Factorial();
        int num = 5;
        System.out.println(
            "Factorial of " + num
            + " is " + obj.factorial(num));
    }
}
  
// This code is contributed by Anshika Goyal.

Python3

# Python 3 program to find
# factorial of given number
  
def factorial(n):
  
    # single line to find factorial
    return 1 if (n == 1 or n == 0) else n * factorial(n - 1) 
  
  
# Driver Code
num = 5
print ("Factorial of", num, "is",
      factorial(num))
  
# This code is contributed
# by Smitha Dinesh Semwal.

C#

// C# program to find factorial
// of the given number
using System;
  
class Factorial {
  
    int factorial(int n)
    {
  
        // single line to find factorial
        return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);
    }
  
    // Driver Code
    public static void Main()
    {
        Factorial obj = new Factorial();
        int num = 5;
  
        Console.WriteLine(
            "Factorial of " + num
            + " is " + obj.factorial(num));
    }
}
  
// This code is contributed by vt_m.

的PHP


输出:

Factorial of 5 is 120

上述解决方案导致少量溢出。请参阅大数阶乘以获取适用于大数的解决方案。