Skip to content

Instantly share code, notes, and snippets.

@JFFail
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/b98e95ab75aa8ef5aa96 to your computer and use it in GitHub Desktop.
Save JFFail/b98e95ab75aa8ef5aa96 to your computer and use it in GitHub Desktop.
Solution to Reddit Daily Programmer #214
#!/usr/env/ruby
#http://www.reddit.com/r/dailyprogrammer/comments/35l5eo/20150511_challenge_214_easy_calculating_the/
#values = [5, 6, 11, 13, 19, 20, 25, 26, 28, 37]
values = [37, 81, 86, 91, 97, 108, 109, 112, 112, 114, 115, 117, 121, 123, 141]
sum = 0
mid_sum = 0
#Calculate the mean.
values.each do |x|
sum += x
end
#Determine the mean.
mean = sum.to_f / values.length
#Now figure out the square of the difference between each number and the mean.
values.each do |x|
mid_sum += (x - mean) ** 2
end
#Calculate the variance.
variance = mid_sum.to_f / values.length
#Square root to find the standard deviation.
deviation = Math.sqrt(variance)
puts "The standard deviation is: #{deviation}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment