查找数字的子阶乘
给定一个整数N,任务是找到表示为!N的数字的子阶乘。一个数的子阶乘是使用以下数N的递归关系定义的:
!N = (N-1) [ !(N-2) + !(N-1) ]
where !1 = 0 and !0 = 1
一些子因子是:
n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
!n | 1 | 0 | 1 | 2 | 9 | 44 | 265 | 1, 854 | 14, 833 | 133, 496 | 1, 334, 961 | 14, 684, 570 | 176, 214, 841 | 2, 290, 792, 932 |
例子:
Input: N = 4
Output: 9
Explanation:
!4 = !(4-1)*4 + (-1)4 = !3*4 + 1
!3 = !(3 – 1)*3 + (-1)3 = !2*3 – 1
!2 = !(2 – 1)*2 + (-1)2 = !1*2 + 1
!1 = !(1 – 1)*1 + (-1)1 = !0*1 – 1
Since !0 = 1, therefore !1 = 0, !2 = 1, !3 = 2 and !4 = 9.
Input: N = 0
Output: 1
方法:数N的子阶乘也可以计算为:
Expanding this gives
=> !N = ( N! )*( 1 – 1/(1!) + (1/2!) – (1/3!) …….. (1/N!)*(-1)N )
因此,上述系列可用于查找数字 N 的子阶乘。请按照以下步骤查看如何:
- 初始化变量,比如res = 0 、 fact = 1和count = 0 。
- 使用i遍历从1到N的范围并执行以下操作:
- 将事实更新为事实*i。
- 如果计数是偶数,则将 res 更新为res = res – (1 / fact) 。
- 如果计数是奇数,则将 res 更新为res = res + (1 / fact) 。
- 将 count 的值增加1。
- 最后,返回fact*(1 + res) 。
下面是上述方法的实现:
C++
/// C++ program for the above approach
#include
using namespace std;
// Function to find the subfactorial
// of the number
double subfactorial(int N)
{
// Initialize variables
double res = 0, fact = 1;
int count = 0;
// Iterating over range N
for (int i = 1; i <= N; i++) {
// Fact variable store
// factorial of the i
fact = fact * i;
// If count is even
if (count % 2 == 0)
res = res - (1 / fact);
else
res = res + (1 / fact);
// Increase the value of
// count by 1
count++;
}
return fact * (1 + res);
}
// Driver Code
int main()
{
int N = 4;
cout << subfactorial(N);
return 0;
}
Java
/// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the subfactorial
// of the number
static double subfactorial(int N)
{
// Initialize variables
double res = 0, fact = 1;
int count = 0;
// Iterating over range N
for (int i = 1; i <= N; i++) {
// Fact variable store
// factorial of the i
fact = fact * i;
// If count is even
if (count % 2 == 0)
res = res - (1 / fact);
else
res = res + (1 / fact);
// Increase the value of
// count by 1
count++;
}
return fact * (1 + res);
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
System.out.println((int)(subfactorial(N)));
}
}
// This code is contributed by ukasp.
Python3
# python program for the above approach
# Function to find the subfactorial
# of the number
def subfactorial(N):
# Initialize variables
res = 0
fact = 1
count = 0
# Iterating over range N
for i in range(1, N+1):
# Fact variable store
# factorial of the i
fact = fact * i
# If count is even
if (count % 2 == 0):
res = res - (1 / fact)
else:
res = res + (1 / fact)
# Increase the value of
# count by 1
count += 1
return fact * (1 + res)
# Driver Code
if __name__ == "__main__":
N = 4
print(subfactorial(N))
# This code is contributed by rakeshsahni
C#
/// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the subfactorial
// of the number
static double subfactorial(int N)
{
// Initialize variables
double res = 0, fact = 1;
int count = 0;
// Iterating over range N
for (int i = 1; i <= N; i++) {
// Fact variable store
// factorial of the i
fact = fact * i;
// If count is even
if (count % 2 == 0)
res = res - (1 / fact);
else
res = res + (1 / fact);
// Increase the value of
// count by 1
count++;
}
return fact * (1 + res);
}
// Driver Code
public static void Main()
{
int N = 4;
Console.Write(subfactorial(N));
}
}
// This code is contributed by ipg2016107.
Javascript
输出
9
时间复杂度: O(N)
辅助空间: O(1)