📜  双阶乘

📅  最后修改于: 2021-04-29 08:38:12             🧑  作者: Mango

非负整数n的双阶乘是从1到n的所有与n具有相同奇偶校验(奇数或偶数)的整数的乘积。它也被称为数字的半因子,并用!!表示 。例如,双阶乘因数9是9 * 7 * 5 * 3 * 1,即945。请注意,此定义的结果是0! = 1。
例子:

Input: 6
Output: 48
Note that 6*4*2 = 48

Input: 7
Output: 105
Note that 7*5*3 = 105

对于偶数n,双阶乘为:
n!!=\prod_{k=1}^{n/2}(2k)=n(n-2)(n-4).....4*2
对于奇数n,双阶乘为:
n!!=\prod_{k=1}^{{n+1}/2}(2k-1)=n(n-2)(n-4).....3*1

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

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

以下是双重析因的实现。

C++
#include
  
// function to find double factorial of given number
unsigned int doublefactorial(unsigned int n)
{
    if (n == 0 || n==1)
      return 1;
    return n*doublefactorial(n-2);
}
  
int main()
{
    printf("Double factorial is %d", doublefactorial(5));
    return 0;
}


Java
import java.io.*;
 
class GFG {
 
    // function to find double factorial
    // of given number
    static long doublefactorial(long n)
    {
        if (n == 0 || n==1)
            return 1;
             
        return n * doublefactorial(n - 2);
    }
 
    // Driver code
    static public void main (String[] args)
    {
        System.out.println("Double factorial"
            + " is " + doublefactorial(5));
    }
}
 
// This code is contributed by anuj_67.


Python3
# function to find double
# factorial of given number
def doublefactorial(n):
 
    if (n == 0 or n == 1):
        return 1;
    return n * doublefactorial(n - 2);
 
# Driver Code
print("Double factorial is",
       doublefactorial(5));
 
# This code is contributed
# by Smitha


C#
using System;
 
class GFG {
 
    // function to find double factorial
    // of given number
    static uint doublefactorial(uint n)
    {
        if (n == 0 || n==1)
            return 1;
             
        return n * doublefactorial(n - 2);
    }
 
    // Driver code
    static public void Main ()
    {
        Console.WriteLine("Double factorial"
             + " is " + doublefactorial(5));
    }
}
 
// This code is contributed by anuj_67.


PHP


Javascript


C++
#include
  
// function to find double factorial of given number
unsigned int doublefactorial(unsigned int n)
{
    int res = 1;
    for (int i=n; i>=0; i=i-2)
    {
        if (i==0 || i==1)
            return res;
        else
            res *= i;
    }
}
  
int main()
{
    printf("Double factorial is %d", doublefactorial(5));
    return 0;
}


Java
// Java Program to find double factorial
// of given number
import java .io.*;
 
class GFG {
     
    // function to find double factorial
    // of given number
    static int doublefactorial(int n)
    {
        int res = 1;
        for (int i = n; i >= 0; i = i-2)
        {
            if (i == 0 || i == 1)
                return res;
            else
                res *= i;
        }
         
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        System.out.println("Double factorial"
             + " is " + doublefactorial(5));
    }
}
 
// This code is Contributed by Anuj_67


Python3
# Python3 Program to find double
# factorial of given number
 
# Function to find double
# factorial of given number
def doublefactorial(n):
    res = 1;
    for i in range(n, -1, -2):
        if(i == 0 or i == 1):
            return res;
        else:
            res *= i;
     
# Driver Code
print("Double factorial is",
        doublefactorial(5));
 
# This code is contributed by mits


C#
// C# Program to find double factorial
// of given number
using System;
 
class GFG {
     
    // function to find double factorial
    // of given number
    static int doublefactorial(int n)
    {
        int res = 1;
        for (int i = n; i >= 0; i = i-2)
        {
            if (i == 0 || i == 1)
                return res;
            else
                res *= i;
        }
         
        return res;
    }
 
    // Driver code   
    static void Main()
    {
        Console.Write("Double factorial"
          + " is " + doublefactorial(5));
    }
}
 
// This code is Contributed by Anuj_67


PHP
= 0; $i = $i - 2)
    {
        if ($i == 0 or $i == 1)
            return $res;
        else
            $res *= $i;
    }
}  
     
    // Driver Code
    echo "Double factorial is ", doublefactorial(5);
 
// This code is contributed by anuj_67.
?>


输出:

Double factorial is 15

迭代解决方案:
双阶乘也可以迭代计算,因为递归对于大量数字可能是昂贵的。

C++

#include
  
// function to find double factorial of given number
unsigned int doublefactorial(unsigned int n)
{
    int res = 1;
    for (int i=n; i>=0; i=i-2)
    {
        if (i==0 || i==1)
            return res;
        else
            res *= i;
    }
}
  
int main()
{
    printf("Double factorial is %d", doublefactorial(5));
    return 0;
}

Java

// Java Program to find double factorial
// of given number
import java .io.*;
 
class GFG {
     
    // function to find double factorial
    // of given number
    static int doublefactorial(int n)
    {
        int res = 1;
        for (int i = n; i >= 0; i = i-2)
        {
            if (i == 0 || i == 1)
                return res;
            else
                res *= i;
        }
         
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        System.out.println("Double factorial"
             + " is " + doublefactorial(5));
    }
}
 
// This code is Contributed by Anuj_67

Python3

# Python3 Program to find double
# factorial of given number
 
# Function to find double
# factorial of given number
def doublefactorial(n):
    res = 1;
    for i in range(n, -1, -2):
        if(i == 0 or i == 1):
            return res;
        else:
            res *= i;
     
# Driver Code
print("Double factorial is",
        doublefactorial(5));
 
# This code is contributed by mits

C#

// C# Program to find double factorial
// of given number
using System;
 
class GFG {
     
    // function to find double factorial
    // of given number
    static int doublefactorial(int n)
    {
        int res = 1;
        for (int i = n; i >= 0; i = i-2)
        {
            if (i == 0 || i == 1)
                return res;
            else
                res *= i;
        }
         
        return res;
    }
 
    // Driver code   
    static void Main()
    {
        Console.Write("Double factorial"
          + " is " + doublefactorial(5));
    }
}
 
// This code is Contributed by Anuj_67

的PHP

= 0; $i = $i - 2)
    {
        if ($i == 0 or $i == 1)
            return $res;
        else
            $res *= $i;
    }
}  
     
    // Driver Code
    echo "Double factorial is ", doublefactorial(5);
 
// This code is contributed by anuj_67.
?>

输出:

Double factorial is 15

上述解决方案的时间复杂度为O(n)。
重要事项:

  1. 双阶乘和阶乘使用以下公式关联。
Note : n!! means double factorial.
If n is even, i.e., n = 2k
   n!! = 2kk!
Else (n = 2k + 1)
   n!! = (2k)! / 2kk! 
  1. 双阶乘经常在组合运算中使用。有关应用程序列表,请参阅Wiki。一个示例应用是对奇数n的完整图K n + 1的完美匹配的计数。