Skip to content

Instantly share code, notes, and snippets.

@cyrusstoller
Created February 10, 2012 21:42
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 cyrusstoller/1793159 to your computer and use it in GitHub Desktop.
Save cyrusstoller/1793159 to your computer and use it in GitHub Desktop.
Embed.ly challenge http://apply.embed.ly/
$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
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
$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