Calculating future occurrences of Friday the 13th

前端 未结 7 1733
庸人自扰
庸人自扰 2020-12-15 12:48

I\'d like to be able to start with a year, and calculate occurrences of Friday the 13th. A brute force solution is easy and obvious. I have something slightly better, but

7条回答
  •  时光取名叫无心
    2020-12-15 13:38

    This is how I would do it:

    • Assume year is known and is an integer.

    • Loop from 1 to 12

      • Create date with loop index, year and 13 for the day

        • Determine day of week as per established algorithms

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

    If you want to start with a month and year (you have to assume some sort of year), your algorithm becomes

    • Assume year is known and an integer

    • Assume month is known and is an integer

    • Loop

      • Create date with index of loop as year, known month variable, and 13 for the day

      • Determine day of week as per established algorithms

      • If day of week calculate above is Friday, return date, else

      • Else increment year by 1

提交回复
热议问题