This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.

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 ]

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

Joined: Feb 18, 2005
Posts: 1041

4

posted

0

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.

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

Joined: Feb 18, 2005
Posts: 1041

4

posted

0

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 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

Joined: Oct 11, 2004
Posts: 3830

posted

0

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).