28
\$\begingroup\$

Your challenge is to write a program which, given a year, outputs the number of "Friday 13ths" in it.

Rules & Details:

  • You can take input via STDIN, or as an argument passed to your program.
  • You should output the result to STDOUT.
  • You may assume that input will be a valid year, and does not pre-date the Gregorian calendar (undefined behaviour is permitted in these cases).
  • Calendar/Date libraries are allowed.

This is a , so the shortest code (in bytes) wins.

(Related challenge link)

\$\endgroup\$
  • 7
    \$\begingroup\$ What is the required range of input? If it goes much before 1800, what assumptions should be made about switchover from Julian to Gregorian calendar? \$\endgroup\$ – Peter Taylor Dec 10 '13 at 22:47
  • \$\begingroup\$ @PeterTaylor I hadn't thought about it. If a date pre-dates gregorian then you can have undefined behaviour. \$\endgroup\$ – Cruncher Dec 11 '13 at 14:05
  • 1
    \$\begingroup\$ The first countries to adopt the Gregorian calendar did so in October 1582, following the bull of Gregory himself. Countries to adopt the new calendar late did not change until the 20th century, for example Greece introduced it on 1 March 1923. \$\endgroup\$ – Jeppe Stig Nielsen Dec 11 '13 at 14:16
  • \$\begingroup\$ @JeppeStigNielsen I don't know much about calendars and such. Whether they adopted them or not doesn't change what the gregorian dates are. Libraries should be able to calculate dates from quite a ways back I presume? \$\endgroup\$ – Cruncher Dec 11 '13 at 14:17
  • 3
    \$\begingroup\$ I am being offtopic here, I guess. Many libraries written by Anglo-American programmers use September 1752 as the "correct" time of change of calendars. This was when the British Empire changed. The new calendar was kept when U.S.A. was founded, of course. (As a curiosity, some SQL software has 1753 as the minimal year since they don't want to cope with the September 1752 issue.) However, using September 1752 is highly anglocentric. You are right Gregorian dates are the same whether they were used historically or not. That is the so-called proleptic Gregorian calendar. \$\endgroup\$ – Jeppe Stig Nielsen Dec 11 '13 at 14:38

32 Answers 32

1
2
0
\$\begingroup\$

JavaScript, 56 bytes

y=>(g=m=>m?g(--m)-(new Date(y,m,13).getDay()!=5):12)(12)

Try It Online!

|improve this answer|||||
\$\endgroup\$
-2
\$\begingroup\$

VB.net

Not a CodeGolf attempt

Iterator Function SpecificDayOfTheWeekInYear(Year As Integer, DayOfTheWeek As DayOfWeek ) As IEnumerable(of Date)
  Dim FirstDayOfTheYear = New Date(Year,1,1)
  Dim CurrentDay = FirstDayOfTheYear.AddDays( DayOfTheWeek - FirstDayOfTheYear.DayOfWeek )
  If CurrentDay.Year < Year Then CurrentDay = CurrentDay.AddDays(7)
  While CurrentDay.Year = Year
    Yield CurrentDay 
    CurrentDay=CurrentDay.AddDays(7)
  End While
End Function

Which makes looking for Friday the 13th's simple.

Dim FridayThe13ths =  From d in SpecificDayOfTheWeekInYear(2013,DayOfWeek.Friday )
                     Where d.Day=13

or a much simpler

Iterator Function YieldDayInMonth(Year As Integer, Day As Integer) As IEnumerable(of date)
  For M=1 To 12
   Yield New Date(Year,M,Day )
  Next
End Function

This makes the coding thus.

Dim f13 = YieldDayInMonth( 2013, 13).Where( Function(d) d.DayOfWeek = DayOfWeek.Friday)
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Why are you posting "Not a CodeGolf attempt" on a code golf? That seems a bit off topic and pointless. \$\endgroup\$ – Tim Seguine Apr 10 '14 at 18:55
  • \$\begingroup\$ @TimSeguine Bit like posting any vb.net entry is pointless, not like vb.net is ever going to "win" one of these is it? \$\endgroup\$ – Adam Speight Apr 10 '14 at 20:08
  • 2
    \$\begingroup\$ It's more about cleverness than winning. If people like your entry, then you are likely to get upvotes even if you don't win. Choosing not to golf on a code golf question seems like not even trying. \$\endgroup\$ – Tim Seguine Apr 10 '14 at 20:11
  • \$\begingroup\$ Relevant meta post: meta.codegolf.stackexchange.com/a/1456/11909 I am not the only one who downvotes when things don't follow the spec. \$\endgroup\$ – Tim Seguine Apr 18 '14 at 15:56
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.