The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   The Coat Room (http://hintsforums.macworld.com/forumdisplay.php?f=8)
-   -   Math + Leap Years = Frustration (http://hintsforums.macworld.com/showthread.php?t=96529)

Felix_MC 12-05-2008 07:26 PM

Math + Leap Years = Frustration
 
Mathematically, a leap year is a year that can be divided by 4, but not by 100 OR can be divided by 400. This can be represented by
Code:

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

However there seems to be something off about it..
Take year 700 for example. 700 is divisible by both 4 and 100, and not by 400, so therefore it's not a leap year. However year 704 is divisible by 4, and not by 100, so therefore it should be a leap year. But since leap years occur every 4 years, and 704 - 4 = 700, shouldn't 700 also be a leap year?

So, anyone got an idea how that works out? :confused:

hayne 12-05-2008 07:35 PM

Quote:

Originally Posted by Felix_MC (Post 506959)
But since leap years occur every 4 years

There's your problem - it is not true that leaps years occur every 4 years!
You correctly explained the Gregorian convention for leap years at the beginning of your post. It clearly implies that leap years usually occur every 4 years - but not always!
More details if needed: http://en.wikipedia.org/wiki/Leap_year

cpragman 12-05-2008 07:48 PM

You can use the built-in OS function to determine this, that way, if Apple has to update tha algorithm, your code doesn't need to change.

http://developer.apple.com/documenta...ibexslt.3.html

Jasen 12-05-2008 07:56 PM

Our calendar is so bizarre.
We need a metric calendar.:D

And cpragman had the right idea. Don't try to reinvent the wheel unless you have to, this logic has already been figured out. Most OS's will have some way to grab this programmatically.

cwtnospam 12-05-2008 08:14 PM

Since the next 'leap year that isn't' doesn't occur until 2100, I don't think any of us need to worry, but here it is using both methods describe in http://en.wikipedia.org/wiki/Leap_year ;)
PHP Code:

<?php
$startyear 
2008;
$endyear 2100;
for(
$year=$startyear;$year<=$endyear;$year++)
{
if(((
$year == 0) && ($year 100 != 0)) || ( $year 400 ==0))
    {
    echo 
$year." is a leap year! ";
    }
    else
    {
    echo 
$year." is not a leap year ";
    }
if(
$year 400 == 0)
    {    
    echo 
" --- ".$year." is a leap year! ";
    } else if (
$year 100 == 0)
        { 
        echo 
" --- ".$year." is not a leap year ";
        } else if (
$year == 0)
            { 
                echo 
" --- ".$year." is a leap year! ";
            } else
            {
            echo 
" --- ".$year." is not a leap year ";;
            }
    echo 
"<BR>";
}
?>

2008 is a leap year! --- 2008 is a leap year!
2009 is not a leap year --- 2009 is not a leap year
2010 is not a leap year --- 2010 is not a leap year
2011 is not a leap year --- 2011 is not a leap year
2012 is a leap year! --- 2012 is a leap year!
2013 is not a leap year --- 2013 is not a leap year
2014 is not a leap year --- 2014 is not a leap year
2015 is not a leap year --- 2015 is not a leap year
2016 is a leap year! --- 2016 is a leap year!
2017 is not a leap year --- 2017 is not a leap year
2018 is not a leap year --- 2018 is not a leap year
2019 is not a leap year --- 2019 is not a leap year
2020 is a leap year! --- 2020 is a leap year!
2021 is not a leap year --- 2021 is not a leap year
2022 is not a leap year --- 2022 is not a leap year
2023 is not a leap year --- 2023 is not a leap year
2024 is a leap year! --- 2024 is a leap year!
2025 is not a leap year --- 2025 is not a leap year
2026 is not a leap year --- 2026 is not a leap year
2027 is not a leap year --- 2027 is not a leap year
2028 is a leap year! --- 2028 is a leap year!
2029 is not a leap year --- 2029 is not a leap year
2030 is not a leap year --- 2030 is not a leap year
2031 is not a leap year --- 2031 is not a leap year
2032 is a leap year! --- 2032 is a leap year!
2033 is not a leap year --- 2033 is not a leap year
2034 is not a leap year --- 2034 is not a leap year
2035 is not a leap year --- 2035 is not a leap year
2036 is a leap year! --- 2036 is a leap year!
2037 is not a leap year --- 2037 is not a leap year
2038 is not a leap year --- 2038 is not a leap year
2039 is not a leap year --- 2039 is not a leap year
2040 is a leap year! --- 2040 is a leap year!
2041 is not a leap year --- 2041 is not a leap year
2042 is not a leap year --- 2042 is not a leap year
2043 is not a leap year --- 2043 is not a leap year
2044 is a leap year! --- 2044 is a leap year!
2045 is not a leap year --- 2045 is not a leap year
2046 is not a leap year --- 2046 is not a leap year
2047 is not a leap year --- 2047 is not a leap year
2048 is a leap year! --- 2048 is a leap year!
2049 is not a leap year --- 2049 is not a leap year
2050 is not a leap year --- 2050 is not a leap year
2051 is not a leap year --- 2051 is not a leap year
2052 is a leap year! --- 2052 is a leap year!
2053 is not a leap year --- 2053 is not a leap year
2054 is not a leap year --- 2054 is not a leap year
2055 is not a leap year --- 2055 is not a leap year
2056 is a leap year! --- 2056 is a leap year!
2057 is not a leap year --- 2057 is not a leap year
2058 is not a leap year --- 2058 is not a leap year
2059 is not a leap year --- 2059 is not a leap year
2060 is a leap year! --- 2060 is a leap year!
2061 is not a leap year --- 2061 is not a leap year
2062 is not a leap year --- 2062 is not a leap year
2063 is not a leap year --- 2063 is not a leap year
2064 is a leap year! --- 2064 is a leap year!
2065 is not a leap year --- 2065 is not a leap year
2066 is not a leap year --- 2066 is not a leap year
2067 is not a leap year --- 2067 is not a leap year
2068 is a leap year! --- 2068 is a leap year!
2069 is not a leap year --- 2069 is not a leap year
2070 is not a leap year --- 2070 is not a leap year
2071 is not a leap year --- 2071 is not a leap year
2072 is a leap year! --- 2072 is a leap year!
2073 is not a leap year --- 2073 is not a leap year
2074 is not a leap year --- 2074 is not a leap year
2075 is not a leap year --- 2075 is not a leap year
2076 is a leap year! --- 2076 is a leap year!
2077 is not a leap year --- 2077 is not a leap year
2078 is not a leap year --- 2078 is not a leap year
2079 is not a leap year --- 2079 is not a leap year
2080 is a leap year! --- 2080 is a leap year!
2081 is not a leap year --- 2081 is not a leap year
2082 is not a leap year --- 2082 is not a leap year
2083 is not a leap year --- 2083 is not a leap year
2084 is a leap year! --- 2084 is a leap year!
2085 is not a leap year --- 2085 is not a leap year
2086 is not a leap year --- 2086 is not a leap year
2087 is not a leap year --- 2087 is not a leap year
2088 is a leap year! --- 2088 is a leap year!
2089 is not a leap year --- 2089 is not a leap year
2090 is not a leap year --- 2090 is not a leap year
2091 is not a leap year --- 2091 is not a leap year
2092 is a leap year! --- 2092 is a leap year!
2093 is not a leap year --- 2093 is not a leap year
2094 is not a leap year --- 2094 is not a leap year
2095 is not a leap year --- 2095 is not a leap year
2096 is a leap year! --- 2096 is a leap year!
2097 is not a leap year --- 2097 is not a leap year
2098 is not a leap year --- 2098 is not a leap year
2099 is not a leap year --- 2099 is not a leap year
2100 is not a leap year --- 2100 is not a leap year

Jasen 12-05-2008 08:28 PM

So according to that php code, Felix's IF statement is perfectly valid.
It is that easy.

Jay Carr 12-05-2008 09:30 PM

I remember I had to write a program for a class once that had to find out if a year was a leap year, but then it also had to take the date and figure out what day of the week it was as well... Finding the day of the week was an interesting problem...

Felix_MC 12-05-2008 09:40 PM

Quote:

Originally Posted by Zalister (Post 506984)
I remember I had to write a program for a class once that had to find out if a year was a leap year, but then it also had to take the date and figure out what day of the week it was as well... Finding the day of the week was an interesting problem...

Funny you should mention that, because that's pretty much what I'm trying to do! :D
I am writing a calendar program in C++ that amongst other thing will take any date in time (ex. Jan 5, 1623) and tell you what day of the week it is. I needed to know which years are leap years to balance out my functions, but it turns out I can get around it by using the Doomsday Rule. :rolleyes:

Felix_MC 12-05-2008 09:41 PM

Quote:

Originally Posted by hayne (Post 506961)
There's your problem - it is not true that leaps years occur every 4 years!
You correctly explained the Gregorian convention for leap years at the beginning of your post. It clearly implies that leap years usually occur every 4 years - but not always!
More details if needed: http://en.wikipedia.org/wiki/Leap_year

aaah. Didn't know that ;)

Felix_MC 12-05-2008 09:51 PM

Quote:

Originally Posted by cpragman (Post 506963)
You can use the built-in OS function to determine this, that way, if Apple has to update tha algorithm, your code doesn't need to change.

http://developer.apple.com/documenta...ibexslt.3.html

Actually I'm in Visual Studios 08 in Windows XP right now, so I don't know if they have a build-in C++ time/date library.
Besides, I like reinventing the wheel if it's challenging enough :rolleyes:

Quote:

Originally Posted by Jasen (Post 506965)
Our calendar is so bizarre.
We need a metric calendar.:D

I wonder how no one thought of that before.. I'd be much easier if everything was based on 10. :p

cwtnospam 12-05-2008 09:54 PM

Quote:

Originally Posted by Jasen (Post 506965)
We need a metric calendar.:D

First, we need to get the Earth to go around the sun in some multiple of 10 days. :D

Jay Carr 12-06-2008 04:36 AM

Quote:

Originally Posted by cwtnospam (Post 506988)
First, we need to get the Earth to go around the sun in some multiple of 10 days. :D

Well, there's an interesting math question for you then. Roughly how much force would it take to move the earth into an orbit that is 360 days?

fazstp 12-06-2008 05:06 AM

Quote:

Originally Posted by Zalister (Post 507007)
Well, there's an interesting math question for you then. Roughly how much force would it take to move the earth into an orbit that is 360 days?

Can we get it to spin slower but stay in the same orbit?

benwiggy 12-06-2008 10:20 AM

Quote:

Originally Posted by Jasen (Post 506965)
We need a metric calendar.

No, we need to use the Grand Cycle of 19-years, and the lunar 56-month cycle!

I have to say that the OS X NSCalendar and related objects are really useful. I recently wrote a little app that calculates Easter and all moveable Christian feasts for any given year. (Niche market!)

Jasen 12-06-2008 11:50 AM

Quote:

Originally Posted by Felix_MC (Post 506987)
Actually I'm in Visual Studios 08 in Windows XP right now, so I don't know if they have a build-in C++ time/date library.
Besides, I like reinventing the wheel if it's challenging enough.

I use VB and C#.
Day of the week?
iDayOfWeek = DatePart(DateInterval.Weekday, today)
:p

Jay Carr 12-06-2008 03:58 PM

Quote:

Originally Posted by fazstp (Post 507009)
Can we get it to spin slower but stay in the same orbit?

You mean just make the days longer? Yeah, that would work too I guess (plus I'd get more sleep, right?) Any takers? :)

fazstp 12-18-2008 03:50 PM

Just thought I'd chuck this in the mix;

Calls to scrap the 'leap second' grow

blubbernaut 12-18-2008 06:44 PM

Quote:

Originally Posted by cwtnospam (Post 506988)
First, we need to get the Earth to go around the sun in some multiple of 10 days. :D

At least we could divide our days into time slices in base ten! What's with 60 seconds in an minute, 60 minutes in an hour, 24 hours in a day??

cwtnospam 12-18-2008 07:42 PM

Beats the heck out of me.

Mikey-San 12-18-2008 07:57 PM

is this the thread for calendar antics

$ cal 9 1752

Jay Carr 12-18-2008 08:41 PM

Translation please? Someone?

fazstp 12-18-2008 09:45 PM

Quote:

Originally Posted by Zalister (Post 509248)
Translation please? Someone?

When Mikey-San says it's a leap year, that year better damn well leap :D.

wdympcf 12-18-2008 10:44 PM

Quote:

Originally Posted by blubbernaut (Post 509234)
At least we could divide our days into time slices in base ten! What's with 60 seconds in an minute, 60 minutes in an hour, 24 hours in a day??

Perhaps this is a valid explanation? Why is a minute divided into 60 seconds, an hour into 60 minutes, yet there are only 24 hours in a day?

Hal Itosis 12-18-2008 11:36 PM

Quote:

Originally Posted by Mikey-San (Post 509243)
is this the thread for calendar antics

$ cal 9 1752

Code:

  September 1752
Su Mo Tu We Th Fr Sa
      1  2 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Looks like we can blame the Brits for that one:
And that from and after the said first Day of January 1752, the several Days of each Month shall go on, and be reckoned and numbered in the same Order; and the Feast of Easter, and other moveable Feasts thereon depending, shall be ascertained according to the same Method, as they now are, until the 2nd Day of September in the said Year 1752 inclusive;

and that the natural Day next immediately following the said 2nd Day of September, shall be called, reckoned and accounted to be the 14th Day of September, omitting for that Time only the 11 intermediate nominal Days of the common Calendar;

Here's a link with TMI (too much information):
The Best of Dates, The Worst Of Dates
-HI-

Jay Carr 12-19-2008 07:55 AM

Wow, my day finder didn't account for that madness. I guess that means that I'm way off at this point. I should point that out to my professor though so that all CS students following in my footsteps can suffer...

@fazstp -- Where did that thread go anyway? :D


All times are GMT -5. The time now is 12:35 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.