Superstitious Programming

Python 2, 157 144 136 Bytes

My solution uses the Gauss-Algorithm. Input is the year as Integer. Output is the list of months with a friday 13th as numbers (1-12). Probably some more golfing possible, but its getting late... Gonna edit this one tomorrow und get this down a bit more. Suggestions are always welcome in the meantime!

def f(i):y=lambda m:(i-1,i)[m>2];print[m for m in range(1,13)if(13+int(2.6*((m-2)%12,12)[m==2]-.2)+y(m)%4*5+y(m)%100*4+y(m)%400*6)%7==5]

edit: Got it down to 144 by replacing the for-loop with a list comprehesion und making some other small adjustments.

edit2: Golfed it down to 136 with the suggestions from Morgan Thrapp and fixed the bug he discovered. Thanks a lot! :)


C - 164 153 112 bytes

I found a nice little solution using a heavily modified version of Schwerdtfeger's method. It encodes the necessary table in an integer using base 7, modified to fit in a signed 32-bit word. It outputs the month as an ASCII-character, with January encoded as 1, February as 2 and so on, with October encoded as :, November encoded as ; and December encoded as <.

t=1496603958,m;main(y){for(scanf("%d",&y),y--;(y%100+y%100/4+y/100%4*5+t+5)%7||putchar(m+49),t;t/=7)2-++m||y++;}

Here it is slightly ungolfed:

t=1496603958,m;
main(y){
  for(
    scanf("%d",&y),y--;
    (y%100+y%100/4+y/100%4*5+t+5)%7||putchar(m+49),t;
    t/=7
  )
    2-++m||y++;
}

I am sure there are a few ways to make it even smaller, but I think the algorithm, or a slight variation thereof, is nearly ideal for finding the months where Friday the 13th occurs (with respect to code size). Notes:

  1. If a 64-bit word could have been used it would be possible to get rid of an annoying addition (+5).
  2. The variable m isn't actually necessary, since the month we are looking at is deducible from t.

I leave my older answer below, seeing as it uses a completely different method not seen in other answers here.


This is based on a solution to a related problem (https://codegolf.stackexchange.com/a/22531/7682).

Y,M,D,d;main(y){for(scanf("%d",&y);Y<=y;++D>28+(M^2?M+(M>7)&1^2:!(Y&3)&&(Y%25||!(Y&15)))&&(D=1,M=M%12+1)<2&&Y++)(d=++d%7)^1||D^13||y^Y||printf("%d,",M);}

It basically simulates the Gregorian calendar, advancing one day at a time, printing the month when it is a Friday and the 13th. Here it is in a slightly more readable form:

Y,M,D,d;
main(y){
  for(
    scanf("%d",&y);
    Y<=y;
    ++D>28+(
      M^2
        ?M+(M>7)&1^2
        :!(Y&3)&&(Y%25||!(Y&15))
    )&&(
      D=1,
      M=M%12+1
    )<2&&Y++
  )
    (d=++d%7)^1||D^13||y^Y||
      printf("%d,",M);
}

Tags:

Date

Code Golf