Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Day of week

Ryan McGuire
Ranch Hand
Posts: 1061
4
Your assignment, should you choose to accept it, is to come up with the Day Of The Week calculation that is easiest to perform mentally. The algorithm has to work for for any date from Jan 1, 1900 to Dec 31, 2099. (Two centuries ought to be enough.) The final result of the calculation must come out so that a 0 represents Sunday, 1 Monday, ... 6 Saturday.

The exact fitness calulation will be this:

As you can see allowance was made for the fact that x mod 4 is easier to calculate in your head than x mod 7.

Let's see an example algorithm/calculation (March 2, 2005)

Can I cut down that 24 points for 12 constants? Hmmm... 5, 1, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3. I don't see anything.

Surely, there is an algorithm that scores fewer than 42 points (and still comes out with the right answer).

Ryan
[ March 02, 2005: Message edited by: Ryan McGuire ]

David Harkness
Ranch Hand
Posts: 1646
Originally posted by Ryan McGuire:
Surely, there is an algorithm that scores fewer than 42 points
Forget the algorithm -- you've just found the answer to life, the universe, and everything!
[ March 02, 2005: Message edited by: David Harkness ]

Mitch Ikeson
Ranch Hand
Posts: 31
But it scores < not = 42.

But he is near the answer.

Eric Pascarello
author
Rancher
Posts: 15385
6
I personally would just type this into the address bar:

Would your algo. account for leap year? To lazy to test!

Eric

Ryan McGuire
Ranch Hand
Posts: 1061
4
Originally posted by Eric Pascarello:
I personally would just type this into the address bar:

Would your algo. account for leap year? To lazy to test!

Eric

Very nice if you're near a computer. Notice, however, that I'm going for easy to memorize/perform in one's head. One might infer that I don't plan on running this on a compueter.

The divide by four in the first step is what handles leap years.

Ryan

David O'Meara
Rancher
Posts: 13459
Can I cut down that 24 points for 12 constants? Hmmm... 5, 1, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3. I don't see anything.

Not exactly in the rules, but why not call it 511462403513 (ie one constant ) and define the maths to pop the M'th digit? What are the 'points' costs of abitrary div, mod, power, log functions?

[edit - yeah yeah, i read it a bit closer ]
[ March 05, 2005: Message edited by: David O'Meara ]

Ryan McGuire
Ranch Hand
Posts: 1061
4
Originally posted by Ryan McGuire:

...

The exact fitness calulation will be this:
...

2 things:
• Any MOD operation that's really easy counts the same as a MOD 4. I would say that MOD 2, 4, 5, 10, 20, 40, 50, 100 all fit in this class. Maybe MOD 3 and 30 fit in here too. I'd have to say MOD 7 and 12 don't.
• What may be obvious is that I'm looking for a DayOfWeek calculation that's easy to do in one's head. The "fitness calculation" score was an attempt to characterize "easy to do in head". If you can make an argument that you've found an improvement that makes the calculation easier despite having a higher score, I'd love to see it.

• Ryan

Sonny Pondrom
Ranch Hand
Posts: 128

ankur rathi
Ranch Hand
Posts: 3830
Ryan McGuire wrote:Your assignment, should you choose to accept it, is to come up with the Day Of The Week calculation that is easiest to perform mentally. The algorithm has to work for for any date from Jan 1, 1900 to Dec 31, 2099. (Two centuries ought to be enough.) The final result of the calculation must come out so that a 0 represents Sunday, 1 Monday, ... 6 Saturday.

The exact fitness calulation will be this:

As you can see allowance was made for the fact that x mod 4 is easier to calculate in your head than x mod 7.

Let's see an example algorithm/calculation (March 2, 2005)

Can I cut down that 24 points for 12 constants? Hmmm... 5, 1, 1, 4, 6, 2, 4, 0, 3, 5, 1, 3. I don't see anything.

Surely, there is an algorithm that scores fewer than 42 points (and still comes out with the right answer).

Ryan
[ March 02, 2005: Message edited by: Ryan McGuire ]

A doubt in 3rd line. 24 points are for remembering 12 constants but adding will also accumulate 3 more points. Right?

ankur rathi
Ranch Hand
Posts: 3830
March 2, 2005

Total points = 40, if consider MOD 100 for 2 points.

No better than yours, this will not work for leap years, this will only work for 21st century (2000s).

Reference: http://www.terra.es/personal2/grimmer/#Monthcodes

Pankaj Rathod
Greenhorn
Posts: 7
Method to calulate day of week.

(works for year from 1900) (takes care of leap years)

Example date: 16-Feb-2009
...
...

Edit: The method posted was wrong. Realised the mistake.

Pankaj Rathod
Greenhorn
Posts: 7