HACKER RANK Algorithms - Implementation - Extra Long Factorials - Solution.'Rb' and 'Py'
Algorithms - Implementation - Extra Long Factorials - Solution
N!=N×(N−1)×(N−2)×⋯×3×2×1
Problem Statement
You are given an integer N . Print the factorial of this number.
Input
Input consists of a single integerN , where 1≤N≤100 .
Input consists of a single integer
Output
Print the factorial ofN .
Print the factorial of
Example
For an input of25 , you would print 15511210043330985984000000 .
For an input of
Note: Factorials of N>20 can't be stored even in a 64−bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.
We recommend solving this challenge using BigIntegers.
Solution:
PYTHON SOLUTION:from math import factorial print factorial(input())
RUBY SOLUTION:
# Enter your code here. Read input from STDIN. Print output to STDOUT
n=gets
sum=1
for i in 1..n.to_i
sum=sum*i.to_i
end
print sum
RUBY SOLUTION:# Enter your code here. Read input from STDIN. Print output to STDOUT n=gets sum=1 for i in 1..n.to_i sum=sum*i.to_i end print sum
Comments
Post a Comment