Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Idle thoughts...Can I make this leap year function more elegant/efficient?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mark-Jason Dominus  
View profile  
 More options May 21 1998, 3:00 am
Newsgroups: comp.lang.perl.misc
From: m...@op.net (Mark-Jason Dominus)
Date: 1998/05/21
Subject: Re: Idle thoughts...Can I make this leap year function more elegant/efficient?

In article <356749eb.14708...@cnews.newsguy.com>,

Fritz Knack <fritz.kn...@POPULUS.net> wrote:
>I compacted this one as much as *I* could,

There's plenty of benefit in leaving it readable to people instead of
trying to compact it.  If you're trying to speed it up, maybe
memoization is a better option.

Still, if you want something cute, how about:

        sub is_leap {
          my $y = shift;
          !($y % 400 xor $y % 100 xor $y % 4);
        }

Maybe it bothers you that you alsways do three divisions.  In that
case use

        sub is_leap {
          my $y = shift;
          !($y%4 or ($y%100 xor $y%400));        
        }

Or maybe
    !( $year%4 || !($year%100) && $year%400 )

I would never use ``!(X%Y)''.  It's hard to understand.
I'd prefer (X%Y==0) instead:

    !( $year%4 || ($year%100==0) && $year%400 )

Reversing the sense of the test might (or might not) make it more
understandable?

        sub regular_year {
          my $year = shift;
          $year%4 || ($year%100==0) && $year%400
        }

There's always the fighting-without-fighting approach:

        %is_leap = map {($_ => 1)} (1201 .. 2399);
        map {$is_leap{$_*100} = 0} (13..15, 17..19, 21..23);

        sub is_leap {
          $is_leap{$_[0]};
        }

You know, this whole exercise seems like a waste of time.  Who cares
if your leap year function is efficient?  Is it a bottleneck?

Maybe this is a good opportunity to point out that the point of leap
years is to make the length of the calendar year more closely
approximate the length of the solar year, and that all intercalation
schemes are inaccurate.  The Gregorian Calendar was devised when
people had been using the Julian system for long enough to notice the
inaccuracies.  But there's a rule that we could adopt that is simpler
than the Gregorian system *and* more accurate.  Under the Dominus
Calendar, a year is a leap year if and only if:

        sub is_leap { $_[0] % 33 % 4 == 0 }

Now is the perfect time to institute this change. The switchover will
be easy: This rule coincides with the Gregorian rule until 2012, so we
have 13.5 years to change all our software.  (2013 is a leap year in
the Dominus Calendar, so we'd have to have everything changed by
then.)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google