c++ - Ruby Program : Trailing zeros in the nth Power of a numbers factorial -
i trying port c++ code ruby. running script gives me "execution timed out" error.
this ruby code:
t = gets.to_i t.times = gets.to_i b = gets.to_i c = 0 j = 5 until j <= j*5 c += a/j end puts c*b end this c++ code:
#include<iostream> main() { long t, a, b, = 0, j, c; std::cin >> t; for( ; < t ; i++) { std::cin >> >> b; c = 0; for( j = 5 ; j <= ; j *= 5) c += a/j; std::cout << c * b << '\n'; } } my input is:
2 100 10 5 4 any output is:
240 4 there 2 test-cases:
- the number of zeroes trailing in
(100!)^10 - the number of zeroes trailing in
(5!)^4
i write calculation in ruby this:
a = 100 b = 10 ((1..a).inject(:*)**b).to_s[/0*$/].size #=> 240 where (1..a).inject(:*) calculates a!, **b exponential function, to_s translates number string, [/0*$/] extracts trailing zeros und size counts them...
Comments
Post a Comment