0% found this document useful (0 votes)
8 views

Ex-1 Factorial

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Ex-1 Factorial

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EX NO 01

DATE FACTORIAL

19/07/24

AIM:
To implement the Python program to find the factorial of number using the three
methods such that :-
Looping method
Recursion method
Stiriling’s approximation

ALGORITHM:

# method 1: Looping :

 Get the user input and initialize the fact = 1 .


 Iterate from 1 to n using a for loop.
 Use the following formula to calculate .
Fact = Fact * i
 The final output has been printed.

# method 2 : Recursion :

 Define a recursive function called factorial that taks an integer n as argument.


 Set up a base case if n is 1 or 0 return 1 since the factorial of 0 or 1 is 1
 In the recursive case, call the factorial function with n-1 as the argument and
multiply the result by n.
 Return the result obtained from the recursive call

# method 3: Stiriling’s approximation :

 Import the Math package .


 Define the function approximation takes an integer N as argument .
 Set up a base case if n is 1 or 0 return 1 since the factorial of 0 or 1 is 1 .
 Calculate the factorial by the formula
N! = sqrt (2*pi*N)*(N**N)*exp(-N)* exp(lambda)
Where 1/12*(N+1) < lambda < 1/(12 * N)

Lambda = difference (upper bound and lower bound) / 2 + lower bound


 The final output has been printed
 Error approximation = Absolute error / Exact error
PROGRAM:

#Factorial
#Method 1 Looping/Iteration

n=int(input("Enter the number whose Fact to be found:"))


fact=1
while n>0:
fact=fact*n
n=n-1
print("Factorialof n:",fact)

#Method 2: Recursion

n=int(input("Enter the number whose Fact to be found:"))


def factorial(n):
if n==0:
return 1
return n*factorial(n-1)
print(factorial(n))

#Method 3: Stirling's approximation for big numbers

N=int(input("Enter the number whose Fact to be found:"))


import math

lb=(1/(12*N)+1)
rb=1/(12*N)
D=rb-lb
LBDA=lb+D

Nfact=(math.sqrt((2*3.14)*N)*(N**N)*(math.exp(-N))*(math.exp(LBDA)))
#where 1/(12*N+1)< lambda <1/(12*N)

print("Factorial of N:",Nfact)

def factorial(N):
if N==0:
return 1
return N*factorial(N-1)
print("Actual Factorial:",factorial(N))

#Find the error of approximation for N>100


print("Error approx:",factorial(N)-Nfact)

import matplotlib.pyplot as plt

N_Fact = [40, 60, 80, 100, 120, 140]


Err_Approx = [2.068 * 10 ** 44, 2.109 * 10 ** 78, 1.814 * 10 ** 115, 2.365 * 10
** 154, 1.695 * 10 ** 195,
3.412 * 10 ** 237]
plt.plot(N_Fact, Err_Approx, marker='o')
plt.title('Error Approximation')
plt.xlabel('N Value')
plt.ylabel('Error Approximation')
plt.show()

OUTPUT :

Enter the number whose Fact to be found:9


Factorialof n: 362880

Enter the number whose Fact to be found:6


720

Enter the number whose Fact to be found:100


Factorial of N: 9.330255649720954e+157
Actual Factorial:
9332621544394415268169923885626670049071596826438162146859296389521
7599993229915608941463976156518286253697920827223758251185210916864
000000000000000000000000
Error approx: 2.365894673461102e+154
RESULT:

The Python program to find the factorial of the given number using the
above 3 approaches have been executed and error approximation has been
done.

You might also like