# What does this thing do?

$$(d+(5m+m/9)/2+5y/4-y/C+y/CD+1/(m/3+1)*(2-(1/(y\%4+1)+1/(y\%CD+1)-1/(y\%C+1))))\%7$$

1. What does this thing do? Explain.

2. Can you make it simpler i.e. fewer characters (I will give a nice bounty for this)?

Important information (Hintish):

This is not a riddlish trick. It is a math formula that would run as Java code.

• I clicked "edit" to copy the TeX for your formula and realised there's a "%7" at the end. For some reason it's invisible in the display form. You need to edit somehow to make it visible! – Rand al'Thor Apr 12 '15 at 13:20

The only thing I can think of is

it gives you the day of the week from the date of the year (d,m,y with C and CD giving you information about the century and 4-century for leap-year purposes).

Now I have to work out the algebra to prove it - thanks Cop for wasting my time on this! :-[

Edit: looks similar to the C formula

here.

## Progress

The input quantities are as follows:

$d$ is the day, $m$ is the month, $y$ is the year, $C=100$, $CD=400$ (Roman numerals - crafty!).

First consider the quantity $(1/(y\%4+1)+1/(y\%CD+1)-1/(y\%C+1))$. There are four cases to consider:

• if $y$ is not a multiple of 4, then the first fraction is at most $\frac{1}{2}$ and (since 4 divides $CD$ and $C$) the whole sum is less than 1

• if $y$ is a multiple of 4 but not of $C$, then the first fraction is exactly 1 and either the other two are equal or the $C$ one is smaller than the $CD$ one, so the whole sum is at least 1

• if $y$ is a multiple of $C$ but not of $CD$, then the first and last fractions are exactly 1 and cancel out to leave the whole sum less than 1

• if $y$ is a multiple of $CD$, then all three fractions are exactly 1 and the whole sum is 1.

So $(2-(1/(y\%4+1)+1/(y\%CD+1)-1/(y\%C+1)))$ is less than or equal to 1 iff

the year denoted by $y$ is a leap year.

Also $(m/3+1)$ is less than 2 iff

the month denoted by $m$ is January or February.

So the final term in the sum, $(1/(m/3+1)(2-(1/(y\%4+1)+1/(y\%CD+1)-1/(y\%C+1))))$, is greater than $\frac{1}{2}$ iff we need to alter the final answer due to

a leap year.

• well done :) you get a small bounty for part 2 – d'alar'cop Apr 12 '15 at 13:39
• @d'alar'cop a) What is part 2? b) I don't need any more bounties! :-o – Rand al'Thor Apr 12 '15 at 13:41
• part 2 is simplifying the formula further... with a proof :p also the progress doesn't fully explain why it works or doesn't – d'alar'cop Apr 12 '15 at 13:50
• @d'alar'cop Bleurgh ... don't know if I can be bothered :-p Maybe I'll run it on a load of test cases and say "the evidence suggests it works" or something. – Rand al'Thor Apr 12 '15 at 13:53
• please also note that the division is "integer division" meaning that the result always rounds down to the nearest integer. – d'alar'cop Apr 14 '15 at 10:32