Created
February 10, 2012 21:42
-
-
Save cyrusstoller/1793159 to your computer and use it in GitHub Desktop.
Embed.ly challenge http://apply.embed.ly/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$cache = [] | |
def fib(n) | |
return $cache[n] unless $cache[n].nil? | |
return 1 if n == 0 | |
res = n * fib(n-1) | |
$cache << res | |
return $cache[n-1] | |
end | |
sum = 0 | |
i = 0 | |
while sum != 8001 | |
i += 1 | |
num = fib(i) | |
digits = num.to_s.split('') | |
sum = digits.reduce(0) do |tsum, value| | |
tsum + value.to_i | |
end | |
end | |
puts i-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'nokogiri' | |
module Enumerable | |
def sum | |
self.inject(0){|accum, i| accum + i } | |
end | |
def mean | |
self.sum/self.length.to_f | |
end | |
def sample_variance | |
m = self.mean | |
sum = self.inject(0){|accum, i| accum +(i-m)**2 } | |
sum/(self.length - 1).to_f | |
end | |
def standard_deviation | |
return Math.sqrt(self.sample_variance) | |
end | |
end | |
doc = Nokogiri::HTML(open("http://apply.embed.ly/static/data/2.html")) | |
depths = [] | |
doc.css('p').each do |node| | |
depths << node.ancestors.count | |
end | |
puts depths.standard_deviation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$cache = [] | |
def fib(n) | |
return $cache[n] unless $cache[n].nil? | |
return 1 if n == 0 | |
res = n * fib(n-1) | |
$cache << res | |
return $cache[n-1] | |
end | |
total_words = fib(900) | |
sum_words = 0 | |
for i in 1..900 | |
sum_words += total_words/i | |
end | |
sum = 0 | |
i = 0 | |
while sum <= sum_words/2 | |
i += 1 | |
sum += total_words/i | |
end | |
puts i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment