libin (owner)

Fork Of

gist: 78473 by ihower Counting Black Friday

Revisions

gist: 78785 Download_button fork
public
Public Clone URL: git://gist.github.com/78785.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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