HACKER RANK Project Euler 20 - Factorial Digit Sum - Solution.'Rb'
Project Euler Challenges-20 - Factorial Digit Sum Solution
Problem Statement
This problem is a programming version of Problem 20 from projecteuler.net
n! means n×(n−1)×⋯×3×2×1
For example, 10!=10×9×⋯×3×2×1=3628800 ,
and the sum of the digits in the number 10! is 3+6+2+8+8+0+0=27 .
Find the sum of the digits in the number N!
Input Format
The first line contains an integer T , i.e., number of test cases.
Next T lines will contain an integer N .
Output Format
Print the values corresponding to each test case.
Constraints
1≤T≤100
0≤N≤1000
Sample Input
2
3
6
Sample Output
6
9
Problem Statement
This problem is a programming version of Problem 20 from projecteuler.net
For example, 10!=10×9×⋯×3×2×1=3628800 ,
and the sum of the digits in the number10! is 3+6+2+8+8+0+0=27 .
and the sum of the digits in the number
Find the sum of the digits in the number N!
Input Format
The first line contains an integerT , i.e., number of test cases.
NextT lines will contain an integer N .
The first line contains an integer
Next
Output Format
Print the values corresponding to each test case.
Print the values corresponding to each test case.
Constraints
1≤T≤100
0≤N≤1000
Sample Input
2
3
6
Sample Output
6
9
Solution:
# Enter your code here. Read input from STDIN. Print output to STDOUT
a=gets
for i in 0...a.to_i
n=gets
sum=1
for i in 1..n.to_i
sum=sum*i.to_i
end
b=sum
while b.to_i>0 do
c=b.to_i%10
#print c
d= d.to_i + c.to_i
b=b.to_i/10
end
print d.to_i
print "\n";
d=0;
end
# Enter your code here. Read input from STDIN. Print output to STDOUT
a=gets
for i in 0...a.to_i
n=gets
sum=1
for i in 1..n.to_i
sum=sum*i.to_i
end
b=sum
while b.to_i>0 do
c=b.to_i%10
#print c
d= d.to_i + c.to_i
b=b.to_i/10
end
print d.to_i
print "\n";
d=0;
end
Comments
Post a Comment