Calculation of future events Friday the 13th

I would like to start with a year and calculate the events of Friday the 13th. The brute force solution is easy and clear. I have something a little better, but I have no doubt that someone else can come up with an elegant algorithm for this.

Perhaps a little more complicated, it would be interesting for me to give the program a month, and whether we find it next year, which this month will be Friday the 13th.

Feel free to use pseudo-code, but I expect people to vote more for working code samples in your favorite language.

+6
language-agnostic algorithm datetime
source share
6 answers

Any month that starts on Sunday has Friday the thirteenth. There are only 14 combinations, possibly knowing which day the first year is (with or without a leap year and with the sun). You just need to calculate it once and get it. You would only check 14 * 12 possible months to start with, well, given the reasons.

result element of the table (since 2009, 2010):

[Thursday,false] => Feb, March, Nov [Friday,false] => Aug 

to fill out the table, you have the common month Jan (31), Feb (28) .. and then iterate with the seed of each day of the week, marking months starting on Sunday, as well as with and without a leap year. Pretty straight forward, and once you're done, you can share it with us :)

+10
source share

Since your brute force algorithm seems to be an intuitive option for iterating by day, you may not have considered the Doomsday Algorithm . This allows you to simply check if it is the 13th heel. As far as I know, this is the most effective solution to the problem.

+10
source share

Here is an example of PHP code that goes through a fairly straightforward date loop in a range. I would change this to check the 13th of every month for Friday, instead of checking every Friday for the 13th century, as in this example.

+3
source share

One thing that I noticed is that the first month falls on Sunday during the months from Friday the 13th. You can probably use this to simplify the calculation.

+2
source share
 initialize startDate to 13th of the month given in the current year while (true) { if (startDate.dayOfWeek == Date.FRIDAY) break; else startDate.year ++; } return startDate.year; 
+1
source share

Here's how I do it:

  • Suppose the year is known and is an integer.

  • Loop 1 to 12

    • Create date with cycle index, year and 13 during the day

      • Determine the day of the week according to established algorithms

      • If the day of the week calculated above is Friday, do your job

If you want to start with a month and a year (you have to take some year), your algorithm will become

  • Suppose the year is known and the integer

  • Suppose the month is known and is an integer

  • Loop

    • Create date with cycle index as year, month month variable and 13 for day

    • Determine the day of the week according to established algorithms

    • If the day of the week is calculated higher - Friday, return date, else

    • Another year increment of 1

+1
source share

All Articles