How to find the number of Friday the 13ths in a given year

This was a thing I did for a high school number theory assignment, which I then turned into a JavaScript program.

(I wrote this in terms of pencil-and-paper arithmetic; my intent at the time was to make it in a similar style to certain "math magic" things, where it was like, do this long series of operations and then suddenly I can figure out your number. I'm not sure if I succeeded in that, and I'm not sure this is the clearest or more efficient way to do this.)

(Note: this does not work for really old dates because they didn't use a calendar with leap years back then.)

One way

  1. Write down the following numbers
    1. The year (four-digit form)
    2. 1/4 of the year (remove any digits after the decimal point)
    3. The year, leaving off the last two digits
    4. The number you wrote down for b, leaving off the last two digits
  2. Add the numbers you wrote for a, b, and d above, then subtract the number you wrote for c.
  3. Repeatedly perform the following steps to the digits of the number you got in the previous step until you have a number less than 7:
    1. If there are any 7's in the number, change all 7's to 0's.
    2. If there are any 8's in the number, change all 8's to 1's.
    3. If there are any 9's in the number, change all 9's to 2's.
    4. Add six times the thousands digit, two times the hundreds digit, three times the tens digit, and one times the ones digit. If the number is greater than ten thousand, consider all but the last three digits to be the thousands digit (i.e., in the number 123000, multiply 123 by six).
  4. Find your number on the table below.

Another way

  1. Separate the first two digits of the year from the last two digits of the year (e.g., 1999 becomes 19, 99). If the year has more than four digits, remove any extra digits from the beginning (12345 becomes 23, 45). If the year has fewer than four digits, add zeroes to the beginning (1 becomes 00, 01) (but see the note above about old dates). Call the first two digits x and the last two digits y.
  2. Divide y by 4, removing any fractional part. Add that number from the original y to get a new y.
  3. Do one of the following:
    • If x is a multiple of four, leave y alone.
    • If x is even, but not a multiple of four, add 3 to y.
    • If x is one more than a multiple of four (or three less than a multiple of four), add 5 to y.
    • If x is one less than a multiple of four (or three more than a multiple of four), add 1 to y.
  4. Repeatedly perform the following steps to the digits of the number you got in the previous step until you have a number less than 7:
    1. If there are any 7's in the number, change all 7's to 0's.
    2. If there are any 8's in the number, change all 8's to 1's.
    3. If there are any 9's in the number, change all 9's to 2's.
    4. Add two times the hundreds digit (if any), three times the tens digit, and one times the ones digit.
  5. Find that number in the table below.

The table

Your numberFriday 13 in these months
0January (except leap years), October
1January (leap year only), April, July
2September, December
3June
4February (except leap years), March, November
5February (leap year only), August
6May

JavaScript version

Enter a year: