For 13 calendar days. How to calculate the number of days in it using one mathematical formula based on the month number? What will we use

Recently, after spending a long time without sleep, I thought about how you can find out the number of days in a month, knowing its number. There are nursery rhymes on this topic, there is a way to count by tiles, but these methods do not suit me - I plunged into the search for a mathematical formula, which I derived after some time.

So the task

Formally In other words, we need to get a function f(x) that would give the following list of values ​​(which, by the way, I found somewhere on the Internet, and did not derive it using mnemonic rules):

It is worth considering that we only receive the month number as an argument, i.e. we do not take into account leap years, and f(2) = 28.

If you want to know the result I got, scroll to the bottom of this page. What will be described below is the derivation of the desired formula.

What will we use

In addition to addition, subtraction and multiplication, I will use two operations: integer division and the remainder operation. Let me remind you what it is:

  • Integer division, or "division with rounding down". For me it will be presented as a regular division: a / b - meaning ⌊a / b⌋. For example, 5 / 3 = 1.
  • Taking the remainder modulo. I will traditionally denote division with a remainder: a % b = a - (a / b) * b. For example, 5% 3 = 2.

They have the same precedence and are left associative.

The Basics, or the Rule with Many Exceptions

Let's try to find a pattern that would satisfy as many argument values ​​as possible. Typically, the number of days in a month fluctuates between 30 and 31. At the same time, you can notice the dependence of this number on the parity of the month - which means we will use the operation of taking the remainder modulo 2. It seems that this should be something like:

f₁(x) = 30 + x%2


Not a bad start! Ignoring February, for which we will obviously have to resort to some tricks, we will be glad that we were able to adjust the function to the first half of the year. And then, starting in August, the parity must be changed to the opposite. This can be done by replacing x%2 in the first version of the formula for (x+1)%2:

f₂(x) = 30 + (x + 1) % 2


As expected, now the first half of the year is already outside the range of correct values, but the months from August to December gave what was needed. Let's find a way to combine these two parts.

Mask

We need the +1 in the dividend to be “activated” only when the argument reaches values ​​greater than 8, i.e. we need to apply some mask. In this case, the values ​​of the argument cannot exceed 12. This means that integer division of the argument by 8 is ideal for us:


Exactly what we need. Let's use this output:

f₃(x) = 30 + (x + x / 8) % 2


Woohoo! Everything is correct, except February. How unexpected.

February

All months have 30 or 31 days, but February has 28 (let me remind you, we do not consider leap years).

Historical reference: In the Romanesque calendar, February was the last month of the year - agree, adding one day in leap years to the end of the calendar makes more intuitive sense. However, we use the Gregorian calendar, in which the shortest month was moved closer to the beginning by the will of one of the wise rulers.

In the most recent version of our formula, February got a full 30 days. That’s why we need to cut off a couple of days from him. Naturally, some other months will suffer from this: either to the left of February, or to the right of it in our list - however, there are much fewer months on the right, so we will have to sacrifice January, then adjusting the formula for it as well. You can cut off days for the first and second months using the expression 2%x.

Note: this post is a translation of the article cmcenroe.me/2014/12/05/days-in-month-formula.html ( Part I), as well as the author’s addition to it ( Part II). The material should not be taken seriously, but rather as a mental exercise that requires little more than school-level knowledge of arithmetic and has no practical application. Happy reading everyone!

Part I

Introduction

Recently, after another sleepless night, I was thinking about methods for remembering the number of days in each month of the year. There is a counting rhyme for this, as well as a way to count on your knuckles, but neither one nor the other suited me. I wondered if there might be some kind of mathematical formula for solving such a problem, and - not finding one during a cursory study - I challenged myself to create it.

Formalizing In other words, it is necessary to find the function f, such that the value f(x) for every month x, represented by a number from 1 to 12, equals the number of days in that month. Table of argument and function values:

x 1 2 3 4 5 6 7 8 9 10 11 12
f(x) 31 28 31 30 31 30 31 31 30 31 30 31

If you were tempted to try it yourself before reading my solution, now is the time. If you prefer to immediately see the ready answer, then look under the spoiler.

Answer


Below are my steps to find a solution.

Mathematical apparatus

First, let's briefly refresh our memory on two vital operators in solving this problem: integer division and remainder of division.

Integer division This is an operator used in many programming languages ​​to divide two integers and remove the fractional part from the quotient. I will portray it as . For example:

Remainder of the division This is an operator that finds the remainder of division. Many programming languages ​​use the symbol % , I will use constructions like , for example:

Note that the remainder of the division has equal priority with the division.

Basics

So, let's use our mathematical apparatus to obtain the basic formula. A normal month has 30 or 31 days, so we can alternate between 1 and 0 and then simply add a constant to that number:

We get a table with the correct values ​​highlighted in bold:
x 1 2 3 4 5 6 7 8 9 10 11 12
f(x) 31 30 31 30 31 30 31 30 31 30 31 30

Not a bad start! There are already correct values ​​for January and for the months from March to July inclusive. February is a special case, and we will deal with it a little later. After July, for the remaining months, the order of receiving 0 and 1 should be reversed.
To do this, we can add 1 to the dividend:

x 1 2 3 4 5 6 7 8 9 10 11 12
f(x) 30 31 30 31 30 31 30 31 30 31 30 31

The values ​​for August through December are now correct, but as expected, the values ​​for the other months are incorrect. Let's see how we can combine these formulas.

Mask overlay

This requires a piecewise function, but - since this seemed boring to me - I thought about another solution, using one part of the function on one interval, the other on another.
I believe that the easiest way would be to find an expression that is equal to 1 in one application area and 0 in the rest. The method in which by multiplying an argument by an expression we exclude it from the formula outside its scope, I called “mask imposition”, because this behavior is similar to a kind of bit mask.
To use this method, in the last part of our function we need to find an expression equal to 1 when , and - since the argument values ​​​​are always less than 16 - integer division by 8 works fine for this.
x 1 2 3 4 5 6 7 8 9 10 11 12
x ⁄ 8 ⌋ 0 0 0 0 0 0 0 1 1 1 1 1

Now, using this mask, using an expression instead of 1 in the dividend, we can reverse the order of obtaining 0 and 1 in the formula:

x 1 2 3 4 5 6 7 8 9 10 11 12
f(x) 31 30 31 30 31 30 31 31 30 31 30 31

Eureka! Everything is correct, except for February. Surprise surprise.

February

Any month has 30 or 31 days, except February with its 28 (leap years are beyond the scope of this task). Currently, according to our formula, it has 30 days, so it would be nice to subtract the expression equal to 2 at .
The best I could come up with is this, which applies the mask to all months after February:
x 1 2 3 4 5 6 7 8 9 10 11 12
2 mod x 0 0 2 2 2 2 2 2 2 2 2 2

Changing the base constant to 28 and adding 2 to the remaining months, we get the formula:

x 1 2 3 4 5 6 7 8 9 10 11 12
f(x) 29 28 31 30 31 30 31 31 30 31 30 31

Unfortunately, January is now 2 days shorter. But, fortunately, it is very easy to get an expression that will only apply to the first month: it is the inverse of the number rounded down. Multiplying it by 2 we get the final formula:

x 1 2 3 4 5 6 7 8 9 10 11 12
f(x) 31 28 31 30 31 30 31 31 30 31 30 31

Afterword

Here it is - a formula for getting the number of days in any month of the year, using simple arithmetic. Next time you're wondering how many days there are in September, just use this one-line JavaScript function:

Function f(x) ( return 28 + (x + Math.floor(x/8)) % 2 + 2 % x + 2 * Math.floor(1/x); )

Part II

Introduction

In the first part, a short and even slightly elegant formula was obtained, the main advantages of which are the simplicity of the mathematical apparatus, the absence of branches and conditional expressions, and conciseness. The disadvantages - besides the fact that you will not use it in your project - include the lack of checking for leap and non-leap years.
So I set myself the task of creating a function f, such that the value f(x, y) for every month x, represented by a number from 1 to 12 and the year y, greater than 0, equals the number of days in a month x per year y.
For the impatient, there is a ready answer under the spoiler, but for the rest, I ask you to follow me.

Answer

Remainder of the division: mod And ⌊⌋

For visual clarity, let’s agree that in some formulas the division operator with remainder is replaced by lower brackets, where it seemed necessary to me:

Leap year

During a leap year, an additional calendar day is introduced: February 29. As you know, a leap year is a year that is divisible by 4 and not divisible by 100, or divisible by 400. Let us write the expression identical to this statement:

To convert this expression into an algebraic one, it is necessary to apply an injection of the form to the result of the expression:

Which will allow you to get 1 when dividing without a remainder and 0 when dividing with a remainder, in order to use it in the formula for determining the number of days in a month.

As a function g" you can use 1 minus the remainder of the division for:

x 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
g"(x) Infinity 1 0 0 0 0 0 0 0 0 0 0 0 0 0

It is easy to see that by increasing the dividend and divisor by 1 we get the correct formula for:
x 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
g"(x) 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Thus, we write the expression as:


And let's write the expression as:

Using this approach we get the following function g(y), the value of which will be 1 if the year is a leap year, or 0 otherwise:

y 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
g(y) 0 0 1 0 0 0 1 0 0 0 1
y 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900 3000
g(y) 1 0 0 0 1 0 0 0 1 0 0

Leap years are shown in bold.

Let me remind you that, within the framework of the accepted agreement, the operator for obtaining the remainder of division can be depicted as mod, and ⌊⌋.

Mask overlay

In the formula, the part is an amendment that adds 2 days to January. If we remove the factor 2 and replace the 1 with 2 in the numerator, then this formula will add 2 days to January and 1 day to February, which gives us the key to adding a day in a leap year. For clarity, we use an intermediate value in the formula g(y) and as y We use 2000 (leap) and 2001 (non-leap) years:

x 1 2 3 4 5 6 7 8 9 10 11 12
f(x, 2000) 31 29 31 30 31 30 31 31 30 31 30 31
f(x, 2001) 30 28 31 30 31 30 31 31 30 31 30 30

The values ​​for all months except January of non-leap years are correct.

To correct this annoying misunderstanding, let’s add 1 day to January using the formula already known to us:


Or:

x 1 2 3 4 5 6 7 8 9 10 11 12
f(x, 2000) 31 29 31 30 31 30 31 31 30 31 30 31
f(x, 2001) 31 28 31 30 31 30 31 31 30 31 30 30

Conclusion

As a result, a much more cumbersome, but more universal formula has been obtained, which can also be used to obtain the number of days in a month of a certain year:

Function f(x, y) ( return 28 + ((x + Math.floor(x / 8)) % 2) + 2 % x + Math.floor((1 + (1 - (y % 4 + 2) % (y % 4 + 1)) * ((y % 100 + 2) % (y % 100 + 1)) + (1 - (y % 400 + 2) % (y % 400 + 1))) / x) + Math.floor(1/x) - Math.floor(((1 - (y % 4 + 2) % (y % 4 + 1)) * ((y % 100 + 2) % (y % 100 + 1 )) + (1 - (y % 400 + 2) % (y % 400 + 1)))/x )
Example in C# ideone.com/fANutz.

1 . I don’t know how to use such mnemonics, so I looked at the sign on the Internet.
2 . "The Basics", or "Rule With Many Exceptions", like most rules.
3 . Originally, in the Roman calendar, February was the last month of the year, so it makes sense that it is shorter than all the others. There is also logic in adding or removing a day at the end of the year, so its length is variable.

Upd. 1:
Alternative translation of the first part in

The free online calculator Kontur.Accounting will help you and tell you how many days have passed between two given dates. In addition, if you have a need, you can count how many calendar, weekends or working days (hours) a specified period of a year or several years contains.

How many days between dates? Instructions

You simply set a specific start and end day and get a quote in a split second. The online calculator calculates all data independently. If you change the original days of the week, the result is automatically recalculated to take into account leap years.

Important: you cannot take monthly working days/hours from last year’s calculations and provide them as calculations - the data will vary. Therefore, it is better to use a calculator.

So, the procedure is:

  1. In the “Start date” and “End date” fields, select the start and end day of the countdown, respectively, starting from 2013 and ending in the future in 2018.
  2. Set the number of working hours in a day in the next field. By default, this field is already set to 8 hours (40-hour work week), but you can change this number.
  3. On the right side of the screen in the banner you will see the result: working days, calendar days and working hours between the specified dates. The results must be copied and saved in your document.

What can you use a calculator for?

  1. To calculate penalties and delays under contracts
  2. How to understand the efficiency of using a resource and the deadlines for use
  3. How to avoid accidentally scheduling tasks on a weekend
  4. How much time is left until the deadline

Example:

You are an accountant. The manager has asked you in the next couple of minutes to provide data on the number of working hours that all company employees must work in February. You can easily determine the number of employees - you have the numbers in front of your eyes. But the number of hours needs to be counted.... How many days are there in February? Is it a leap year? What days were weekends? How to determine the number of days of holidays?

Solution: just use our widget. You will receive all the information automatically; you do not need desktop calendars and calculators.

Did you like this calculator? Then try our other options

Do you want to do accounting, send reports and make calculations in a convenient and simple web service? Try Kontur.Accounting for free for 14 days! We will quickly teach you how to use the service and answer all your questions!

The date calculator is designed to calculate the number of days between dates, as well as to find a date by adding or subtracting a certain number of days to a known date.

Add days to date

In order to find out what date will be in a certain number of days, use this option. Enter the starting date and the number of days to add to it. To subtract, use a minus value. The calculator also has an option to add only working days.

Calculating the number of days between dates

This calculation method will answer the question "how many days have passed since the date". Enter the start date and end date and click the "calculate" button. The calculator will show how many days there are between the entered dates. Separately, the calculator will show the number of working days.

Using this option, you can calculate how many days are left until a certain event, for example, a birthday or holiday. To do this, enter today's date in the start date field, and the event date in the end date field.

Holidays

The calculator can calculate, add and subtract both calendar days and working days. Official non-working holidays are:

  • January 1,2,3,4,5,6,8 - New Year holidays
  • January 7 - Orthodox Christmas
  • February 23 - Defender of the Fatherland Day
  • March 8 - International Women's Day
  • May 1 - Spring and Labor Day
  • May 9 - Victory Day
  • June 12 - Russia Day
  • November 4 - National Unity Day

If a holiday falls on a Saturday or Sunday, it is moved to the next working day. But sometimes the weekend is moved to a completely different place on the calendar. For example, Saturday and Sunday that fall on the New Year holidays may be moved to May in order to extend the May holidays.

When calculating days, the calculator takes into account both official holiday dates and all transfers.