Factorial of a number without using multiplication
Given a positive number N, the task is to calculate the factorial of N without using the multiplication operator.
Examples:
Input: N = 5
Output:
120
Explanation:
5*4*3*2*1=120
Input: N = 7
Output:
5040
Observation:
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
Approach 1: The problem can be solved using the concept of nested loops. Instead of using the multiplication operator, the answer can be manually calculated by using another loop. Follow the steps below to solve the problem:
- Initialize a variable ans to N.
- Iterate from N-1 to 1, using the variable i, and do the following:
- Initialize a variable sum to 0.
- Iterate from 0 to i-1, using the variable j, and add ans to sum
- Add sum to ans.
- Print ans.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int factorialWithoutMul( int N)
{
int ans = N;
for ( int i = N - 1; i > 0; i--) {
int sum = 0;
for ( int j = 0; j < i; j++)
sum += ans;
ans = sum;
}
return ans;
}
int main()
{
int N = 5;
cout << factorialWithoutMul(N) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int factorialWithoutMul( int N)
{
int ans = N;
for ( int i = N - 1 ; i > 0 ; i--) {
int sum = 0 ;
for ( int j = 0 ; j < i; j++)
sum += ans;
ans = sum;
}
return ans;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(factorialWithoutMul(N));
}
}
|
Python3
def factorialWithoutMul(N):
ans = N
i = N - 1
while (i > 0 ):
sum = 0
for j in range (i):
sum + = ans
ans = sum
i - = 1
return ans
if __name__ = = '__main__' :
N = 5
print (factorialWithoutMul(N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int factorialWithoutMul( int N)
{
int ans = N;
for ( int i = N - 1; i > 0; i--) {
int sum = 0;
for ( int j = 0; j < i; j++)
sum += ans;
ans = sum;
}
return ans;
}
public static void Main()
{
int N = 5;
Console.Write(factorialWithoutMul(N));
}
}
|
Javascript
<script>
function factorialWithoutMul(N)
{
let ans = N;
for (let i = N - 1; i > 0; i--) {
let sum = 0;
for (let j = 0; j < i; j++)
sum += ans;
ans = sum;
}
return ans;
}
let N = 5;
document.write(factorialWithoutMul(N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Approach 2: The problem can be solved by dividing with the reciprocal of the next number instead of multiplying it. In algebra, ab also means a / (1/b). We will be using the same concept to find the factorial of a number without using asterisk. Follow the steps below to solve the problem:
- Take a variable of integer type (here: n) which would store the value of which we’re finding the factorial.
- Initialize a variable (here: p) which would serve to be the factorial of n.
- Start iterating from n to 1. In each step, set the value of p to be the same as that of p divided by the reciprocal of iterator (here: i).
- Print p.
Below is the implementation of the above approach in java:
C++
#include <iostream>
using namespace std;
int factorial( int n)
{
int p = 1;
for ( int i = n; i >= 1; i--)
{
p = p / (1.0 / i);
}
return p;
}
int main()
{
int n = 5;
cout << factorial(n) << endl;
return 0;
}
|
Java
public class Factorial {
int factorial( int n)
{
int p = 1 ;
for ( int i = n; i >= 1 ;
i--) {
p = ( int )(p / ( 1.0 / i));
}
return p;
}
public static void main(String[] Args)
{
Factorial fact
= new Factorial();
int n = 5 ;
System.out.println(fact.factorial(
n));
}
}
|
Python3
def factorial(n):
p = 1
for i in range (n, 0 , - 1 ):
p = p / ( 1.0 / i)
return p
n = 5
print (factorial(n))
|
C#
using System;
public class Factorial {
int factorial( int n)
{
int p = 1;
for ( int i = n; i >= 1; i--)
{
p = ( int )(p / (1.0 / i));
}
return p;
}
static public void Main()
{
Factorial fact
= new Factorial();
int n = 5;
Console.WriteLine(fact.factorial(
n));
}
}
|
Javascript
function factorial(n)
{
let p = 1;
for (let i = n; i >= 1; i--)
{
p = p / (1.0 / i);
}
return p;
}
let n = 5;
console.log(factorial(n))
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
14 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...