Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions 100+ Python challenging programming exercises.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#----------------------------------------#

#----------------------------------------#
Expand Down