# Friday the 13th 4

Posted by **Cleve Moler**,

We all know that Friday the 13th is unlucky, but is it unlikely?

### Contents

#### Year 2012

I plan to post this article during the second week of July, 2012. The Friday in this week is a Friday the 13th, the third we've had so far this year. There were also ones in January and April. That seems like a lot. How often do we have three Friday the 13ths in the first seven months of a year? Well, it's not all that often. It usually happens only once every 28 years. The next time will be the year 2040. But sometimes, around the turn of centuries, it happens twice in 12 years. I mention all this to establish that our calendar does not have a simple periodic behavior. By the way, not to worry, after this week, it will be 14 months until the next Friday the 13th, in September, 2013.

#### Friday the 13th

Which brings us to the central topic of this post:

*What is the probability that the 13th of a month falls on a Friday?*

An obvious response is

*Easy question, the probability is 1/7.*

After all, there are seven days in a week and the 13th of a month is equally likely to fall on any one of them. Well, as we shall see, that's close, but not exactly right.

#### Calendars and Leap Years

Leap years make our calendar a nontrivial mathematical object. The leap year rule can be implemented by this anonymous function.

leapyear = @(y) mod(y,4)==0 & mod(y,100)~=0 | mod(y,400)==0;

This says that leap years happen every four years, except the turn of a century not divisible by 400 is skipped. Try a few year numbers.

y = [2012 2013 2000 2100]'; disp([y leapyear(y)])

2012 1 2013 0 2000 1 2100 0

So, this year is a leap year, next year is not, 2000 was a leap year, 2100 is not.

The leap year rule implies that our calendar has a period of 400 years. The calendar from 1601 to 2000 is being reused from 2001 to 2400. (Except the Gregorian calendar had not been invented in 1601, so I'm talking about the calendar that would have been used back then if they could have used today's calendar, but never mind.)

In a 400 year period, there are 97 leap years, 4800 months, 20871 weeks, and 146097 days. So the average number of days in a calendar year is not 365.25, but

```
format short
dpy = 365+97/400
```

dpy = 365.2425

We can compute the probability that the 13th of a month is a Friday by counting how many times that happens in 4800 months. The correct probability is then that count divided by 4800. Since 4800 is not divisible by 7, the probability does not reduce to 1/7.

#### Clock

MATLAB has a number of functions for doing computations involving calendars and dates. Many of these functions are in the MATLAB Toolbox, but some of the more specialized ones are in the Finance Toolbox. We encountered a few of these functions in my blog about biorhythms. The basis for all the functions is `clock`, which reads the system's clock and returns a 6-element vector

[year, month, date, hour, minute, seconds]

The first five elements have integer values. The sixth element has a fractional part whose accuracy depends upon the computer's internal clock. Here is the output generated when I `publish` this blog.

```
c = clock;
fprintf('clock = [ %4d %4d %5d %5d %5d %8.3f ]\n',c)
```

clock = [ 2012 7 5 11 53 14.258 ]

#### Datenum

The `datenum` function facilitates computations involving calendars by collapsing the `clock` vector into one value, the *serial date number*. This value is the number of days, and fractions of a day, since a reference time 20 centuries ago when `clock` would have been all zeros. Here are a couple of examples of the use of `datenum`. If you run this code yourself, your results should be different.

```
t = now;
fprintf('current_date_number = %10.3f\n',t)
date_string = datestr(t)
tday = fix(t)
tday_string = datestr(tday)
[week_day,week_day_name] = weekday(tday)
```

current_date_number = 735055.495 date_string = 05-Jul-2012 11:53:14 tday = 735055 tday_string = 05-Jul-2012 week_day = 5 week_day_name = Thu

#### Calendar number

The calendar for any year is determined by two pieces of information, the weekday of January 1st and whether or not the year is a leap year. So we need only 14 calendars. We could number all possible calendars, with the units digit specifying the starting week day and the tens digits indicating leap years. The 14 numbers would be `[1:7 11:17]`.

calendar_number = @(y) weekday(datenum(y,1,1)) + 10*leapyear(y);

If the calendar industry used this numbering scheme, here are the calendars you would need for the next 21 years.

y = (2012:2032)'; disp([y calendar_number(y)])

2012 11 2013 3 2014 4 2015 5 2016 16 2017 1 2018 2 2019 3 2020 14 2021 6 2022 7 2023 1 2024 12 2025 4 2026 5 2027 6 2028 17 2029 2 2030 3 2031 4 2032 15

#### Friday the 13th is likely

We are now ready to use the `weekday` function to count the number of times in a 400-year calendar cycle that the 13th of a month occurs on each of the various days of the week.

c = zeros(1,7); for y = 1601:2000 for m = 1:12 d = datenum([y,m,13]); w = weekday(d); c(w) = c(w) + 1; end end c

c = 687 685 685 687 684 688 684

A bar graph, with a line at a probability of 1/7, and week day axis labels.

bar(c) axis([0 8 680 690]) avg = 4800/7; line([0 8], [avg avg],'linewidth',4,'color','black') set(gca,'xticklabel',{'Su','M','Tu','W','Th','F','Sa'})

The probability for Friday is

p = c(6)/4800; fprintf('p = %8.6f\n',p) fprintf('1/7 = %8.6f\n',1/7)

p = 0.143333 1/7 = 0.142857

So, the 13th of a month is more likely to occur on Friday that any other day of the week. Only slightly more likely, I admit, but still ...

Get the MATLAB code

Published with MATLAB® 7.14

**82**views (last 30 days) | |

**Category:**- Fun

### Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.

## Recent Comments