File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programming Diversions and the fly likes Day of week Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Day of week" Watch "Day of week" New topic
Author

Day of week

Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1012
    
    3
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

Joined: Aug 07, 2003
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

Joined: Nov 15, 2004
Posts: 31
But it scores < not = 42.

But he is near the answer.


I know the answer to the ultimite question!!!
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    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

Joined: Feb 18, 2005
Posts: 1012
    
    3
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

Joined: Mar 06, 2001
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

Joined: Feb 18, 2005
Posts: 1012
    
    3
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

    Joined: Jun 05, 2001
    Posts: 128
    ankur rathi
    Ranch Hand

    Joined: Oct 11, 2004
    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

    Joined: Oct 11, 2004
    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

    Joined: Feb 02, 2009
    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

    Joined: Feb 02, 2009
    Posts: 7
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Day of week