Skip to content

Instantly share code, notes, and snippets.

@barbchoy
Last active September 29, 2017 05:14
Show Gist options
  • Save barbchoy/f37e1e2954adbdc2e76b2a2f026c3ea8 to your computer and use it in GitHub Desktop.
Save barbchoy/f37e1e2954adbdc2e76b2a2f026c3ea8 to your computer and use it in GitHub Desktop.
null created by barbchoy - https://repl.it/Br7J/2382
# Write a method that takes in an integer `num` and returns the sum of
# all integers between zero and num, up to and including `num`.
#
# Difficulty: easy.
def sum_nums(num)
sum=0
i=0
while i<num+1
sum=sum+i
i+=1
end
return sum
end
# These are tests to check that your code is working. After writing
# your solution, they should all print true.
puts("\nTests for #sum_nums")
puts("===============================================")
puts('sum_nums(1) == 1: ' + (sum_nums(1) == 1).to_s)
puts('sum_nums(2) == 3: ' + (sum_nums(2) == 3).to_s)
puts('sum_nums(3) == 6: ' + (sum_nums(3) == 6).to_s)
puts('sum_nums(4) == 10: ' + (sum_nums(4) == 10).to_s)
puts('sum_nums(5) == 15: ' + (sum_nums(5) == 15).to_s)
puts("===============================================")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment