#!/usr/bin/env ruby
#ruby 2.0.0 version test
# negative factorial and plus each result number
n=ARGV[0].to_i
def n_fact(n)
if n > -1
puts "argument must be less then 0"
elsif n == -1
return n
else
return (n*n_fact(n+1))
end
end
result = n_fact(n)
puts "negative fact value : #{result}"
def nf_sum(r)
r = r.to_s
if r[0] == "-"
r1 = -(r[1].to_i)
for i in 2..(r.size-1)
puts "#{r1}+#{r[i]}=#{r1+=r[i].to_i}"
end
# putting result
puts "So last sum is = #{r1}"
else # If the result value is positive
sum = 0
r.each_char { |r| sum += r.to_i }
puts sum
end
end
nf_sum(result)
#Testing ^^;
oyj@oyjmint ~/rp $ ruby negative_fact.rb -7
negative fact value : -5040
-5+0=-5
-5+4=-1
-1+0=-1
So last sum is = -1
oyj@oyjmint ~/rp $ ruby negative_fact.rb -8
negative fact value : 40320
9
No comments:
Post a Comment