Skip to content

Instantly share code, notes, and snippets.

@ihower
Created March 13, 2009 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ihower/78473 to your computer and use it in GitHub Desktop.
Save ihower/78473 to your computer and use it in GitHub Desktop.
Counting Black Friday
# http://www.geocities.com/calshing/dayofweek.htm
def get_week( year , month , day)
c = year.to_s[0, year.to_s.length-2 ].to_i
y = year.to_s[year.to_s.length-2,2]
a = (14-month)/12
y = y.to_i - a
m = month + 12*a - 2
y_div_4 = ( y == 0 )? 0 : (y/4)
return ( c/4 - 2*c + y + y_div_4 + ( (13*m-1)/5 + day ) ) % 7
end
def has_13_friday(year, month)
get_week(year, month, 13) == 5
end
# must > 1583-10-15
thirteen_friday_of_year = Array.new(10001,0)
1583.upto(10000) do |y|
1.upto(12) do |m|
if has_13_friday(y, m)
puts "#{y}-#{m}"
thirteen_friday_of_year[y] = thirteen_friday_of_year[y] + 1
end
end
end
thirteen_friday_of_year.each_with_index do |v, i|
puts "#{i} has #{v}" if v > 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment