📜  不使用乘法的数字的阶乘

📅  最后修改于: 2022-05-13 01:56:06.041000             🧑  作者: Mango

不使用乘法的数字的阶乘

给定一个正数N ,任务是在不使用乘法运算符的情况下计算N的阶乘。

例子:

观察:

A*B=A+A+A+A...B times.

This observation can be used as follows:
5!=5*4*3*2*1
=(5+5+5+5)*3*2*1
=(20+20+20)*2*1
=60+60
=120

方法:这个问题可以使用嵌套循环的概念来解决。可以使用另一个循环手动计算答案,而不是使用乘法运算符。请按照以下步骤解决问题:

  1. 将变量ans初始化为N
  2. 使用变量iN-1迭代到1 ,然后执行以下操作:
    • 将变量sum初始化为0
    • 使用变量j0迭代到i-1,并将ans添加到sum
    • sum添加到ans
  3. 打印答案

下面是上述方法的实现

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to calculate factorial of the number
// without using multiplication operator
int factorialWithoutMul(int N)
{
    // variable to store the final factorial
    int ans = N;
 
    // Outer loop
    for (int i = N - 1; i > 0; i--) {
        int sum = 0;
 
        // Inner loop
        for (int j = 0; j < i; j++)
            sum += ans;
        ans = sum;
    }
    return ans;
}
 
// Driver code
int main()
{
    // Input
    int N = 5;
 
    // Function calling
    cout << factorialWithoutMul(N) << endl;
    return 0;
}


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
    // Function to calculate factorial of the number
    // without using multiplication operator
    public static int factorialWithoutMul(int N)
    {
        // variable to store the final factorial
        int ans = N;
 
        // Outer loop
        for (int i = N - 1; i > 0; i--) {
            int sum = 0;
 
            // Inner loop
            for (int j = 0; j < i; j++)
                sum += ans;
            ans = sum;
        }
        return ans;
    }
    // Driver code
    public static void main(String[] args)
    {
        int N = 5;
 
        // Function calling
        System.out.println(factorialWithoutMul(N));
        // This code is contributed by Potta Lokesh
    }
}


Python3
# Python3 program for the above approach
 
# Function to calculate factorial of the number
# without using multiplication operator
def factorialWithoutMul(N):
     
    # Variable to store the final factorial
    ans = N
 
    # Outer loop
    i = N - 1
     
    while (i > 0):
        sum = 0
 
        # Inner loop
        for j in range(i):
            sum += ans
             
        ans = sum
        i -= 1
         
    return ans
 
# Driver code
if __name__ == '__main__':
     
    # Input
    N = 5
 
    # Function calling
    print(factorialWithoutMul(N))
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function to calculate factorial of the number
// without using multiplication operator
static int factorialWithoutMul(int N)
{
     
    // Variable to store the final factorial
    int ans = N;
 
    // Outer loop
    for(int i = N - 1; i > 0; i--)
    {
        int sum = 0;
 
        // Inner loop
        for(int j = 0; j < i; j++)
            sum += ans;
             
        ans = sum;
    }
    return ans;
}
 
// Driver code
public static void Main()
{
     
    // Input
    int N = 5;
 
    // Function calling
    Console.Write(factorialWithoutMul(N));
}
}
         
// This code is contributed by SURENDRA_GANGWAR


Javascript


输出
120

时间复杂度: O(N 2 )
辅助空间: O(1)