vinod's erudition

we learn everything from failure not from success so keep failing

Home Ruby Rails Javascript Python Agentic AI

What I Learnt Today-20-10-2015

Posted on October 20, 2015 by vinod

###Puzzle Have the function SimpleAdding(num) add up all the numbers from 1 to num. For the test cases, the parameter num will be any number from 1 to 1000

def SimpleAdding(num)

  # code goes here

  sum = (1..num.to_i).inject {|sum, n| sum + n}
  #sum =  (1..num.to_i).reduce(:+)

  return sum 
         
end
   
# keep this function call here 

# to see how to enter arguments in Ruby scroll down   

puts SimpleAdding(STDIN.gets)