递归程序找到大量的阶乘
给定一个大数N ,任务是使用递归找到N的阶乘。
Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.
例子:
Input : N = 100
Output : 933262154439441526816992388562667004-907159682643816214685929638952175999-932299156089414639761565182862536979-208272237582511852109168640000000000-00000000000000
Input : N = 50
Output : 3041409320171337804361260816606476884-4377641568960512000000000000
迭代方法:本文的第 1 组讨论了迭代方法。在这里,我们讨论了递归方法。
递归方法:为了递归地解决这个问题,算法改变了递归调用相同函数并将结果乘以数字n 的方式。请按照以下步骤解决问题:
- 如果n小于等于2,则将n乘以1并将结果存储在向量中。
- 否则,调用函数multiply(n, factorialRecursiveAlgorithm(n – 1))来寻找答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// MUltiply the number x with the number
// represented by res array
vector multiply(long int n, vector digits)
{
// Initialize carry
long int carry = 0;
// One by one multiply n with
// individual digits of res[]
for (long int i = 0; i < digits.size(); i++) {
long int result
= digits[i] * n + carry;
// Store last digit of 'prod' in res[]
digits[i] = result % 10;
// Put rest in carry
carry = result / 10;
}
// Put carry in res and increase result size
while (carry) {
digits.push_back(carry % 10);
carry = carry / 10;
}
return digits;
}
// Function to recursively calculate the
// factorial of a large number
vector factorialRecursiveAlgorithm(
long int n)
{
if (n <= 2) {
return multiply(n, { 1 });
}
return multiply(
n, factorialRecursiveAlgorithm(n - 1));
}
// Driver Code
int main()
{
long int n = 50;
vector result
= factorialRecursiveAlgorithm(n);
for (long int i = result.size() - 1; i >= 0; i--) {
cout << result[i];
}
cout << "\n";
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// MUltiply the number x with the number
// represented by res array
static Integer []multiply(int n, Integer []digits)
{
// Initialize carry
int carry = 0;
// One by one multiply n with
// individual digits of res[]
for (int i = 0; i < digits.length; i++) {
int result
= digits[i] * n + carry;
// Store last digit of 'prod' in res[]
digits[i] = result % 10;
// Put rest in carry
carry = result / 10;
}
// Put carry in res and increase result size
LinkedList v = new LinkedList();
v.addAll(Arrays.asList(digits));
while (carry>0) {
v.add(new Integer(carry % 10));
carry = carry / 10;
}
return v.stream().toArray(Integer[] ::new);
}
// Function to recursively calculate the
// factorial of a large number
static Integer []factorialRecursiveAlgorithm(
int n)
{
if (n <= 2) {
return multiply(n, new Integer[]{ 1 });
}
return multiply(
n, factorialRecursiveAlgorithm(n - 1));
}
// Driver Code
public static void main(String[] args)
{
int n = 50;
Integer []result
= factorialRecursiveAlgorithm(n);
for (int i = result.length - 1; i >= 0; i--) {
System.out.print(result[i]);
}
System.out.print("\n");
}
}
// This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
// MUltiply the number x with the number
// represented by res array
static int[] multiply(int n, int[] digits)
{
// Initialize carry
int carry = 0;
// One by one multiply n with
// individual digits of res[]
for (int i = 0; i < digits.Length; i++)
{
int result
= digits[i] * n + carry;
// Store last digit of 'prod' in res[]
digits[i] = result % 10;
// Put rest in carry
carry = result / 10;
}
// Put carry in res and increase result size
LinkedList v = new LinkedList();
foreach (int i in digits)
{
v.AddLast(i);
}
while (carry > 0)
{
v.AddLast((int)(carry % 10));
carry = carry / 10;
}
return v.ToArray();
}
// Function to recursively calculate the
// factorial of a large number
static int[] factorialRecursiveAlgorithm(
int n)
{
if (n <= 2)
{
return multiply(n, new int[] { 1 });
}
return multiply(
n, factorialRecursiveAlgorithm(n - 1));
}
// Driver Code
public static void Main()
{
int n = 50;
int[] result = factorialRecursiveAlgorithm(n);
for (int i = result.Length - 1; i >= 0; i--)
{
Console.Write(result[i]);
}
Console.Write("\n");
}
}
// This code is contributed by gfgking
Javascript
Python3
# Python 3 program for the above approach
# MUltiply the number x with the number
# represented by res array
def multiply(n, digits):
# Initialize carry
carry = 0
# One by one multiply n with
# individual digits of res[]
for i in range(len(digits)):
result = digits[i] * n + carry
# Store last digit of 'prod' in res[]
digits[i] = result % 10
# Put rest in carry
carry = result // 10
# Put carry in res and increase result size
while (carry):
digits.append(carry % 10)
carry = carry // 10
return digits
# Function to recursively calculate the
# factorial of a large number
def factorialRecursiveAlgorithm(n):
if (n <= 2):
return multiply(n, [1])
return multiply(
n, factorialRecursiveAlgorithm(n - 1))
# Driver Code
if __name__ == "__main__":
n = 50
result = factorialRecursiveAlgorithm(n)
for i in range(len(result) - 1, -1, -1):
print(result[i], end="")
输出
30414093201713378043612608166064768844377641568960512000000000000
时间复杂度: O(n)
辅助空间:O(K),其中 K 是输出中的最大位数