From 0ebf9cb8173d052fa533963870c0a0e907b4d39f Mon Sep 17 00:00:00 2001 From: Chaudhary Sarimurrab <35756749+sarimurrab@users.noreply.github.com> Date: Wed, 1 Apr 2020 13:15:32 +0530 Subject: [PATCH] Contributing for large number factorial --- ...thon challenging programming exercises.txt | 49 ++++++++++++++++--- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 97af5aaf..8cfe64e4 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -52,13 +52,50 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Solution: -def fact(x): - if x == 0: - return 1 - return x * fact(x - 1) +# Python program to compute factorial +# of big numbers + +import sys +def factorial( n) : + res = [None]*500 + # Initialize result + res[0] = 1 + res_size = 1 + + x = 2 + while x <= n : + res_size = multiply(x, res, res_size) + x = x + 1 + + print ("Factorial of given number is") + i = res_size-1 + while i >= 0 : + sys.stdout.write(str(res[i])) + sys.stdout.flush() + i = i - 1 + + +def multiply(x, res,res_size) : + + carry = 0 # Initialize carry + i = 0 + while i < res_size : + prod = res[i] *x + carry + res[i] = prod % 10; # Store last digit of + # 'prod' in res[] + carry = prod/10; # Put rest in carry + i = i + 1 + + # Put carry in res and increase result size + while (carry) : + res[res_size] = carry % 10 + carry = carry / 10 + res_size = res_size + 1 + + return res_size + +print factorial(n) -x=int(raw_input()) -print fact(x) #----------------------------------------# #----------------------------------------#