Announcement

Collapse
No announcement yet.

Can the first day of a century fall on a Sunday?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can the first day of a century fall on a Sunday?

    A programming challenge:

    Answer here - http://www.straightdope.com/columns/...ll-on-a-sunday (No Peeking)

    ======================================================
    "The follies which a man regrets the most in his life
    are those which he didn't commit
    when he had the opportunity."
    -- Helen Rowland
    ======================================================
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

  • #2
    Running this Q-n-D program bears this out. Sunday along with Tuesday and Thursday. Interesting.


    Code:
    REM ********************************************************************
    REM * AstroDay code function lifted from:                              *
    REM * It finds the day of week the 1st of the month falls on           *
    REM * http://www.powerbasic.com/support/forums/Forum4/HTML/003463.html *
    REM * Author: Semen Matusovski                                         *
    REM ********************************************************************
        DECLARE FUNCTION AstroDay(year AS LONG, _           '
                                 month AS LONG, _           '
                                 day   AS LONG) _           '
                                       AS LONG              '
                                                            '
    FUNCTION PBMAIN                                         '
        LOCAL day, month, year AS LONG
        LOCAL t, col AS LONG
        LOCAL c0, c1, c2, c3, c4, c5, c6 AS LONG
                                                            '
        CONSOLE SCREEN 40,85                                '
        COLOR 14,1
        CLS
    
        LOCATE  3, 5 : PRINT;"   Sunday:";
        LOCATE  5, 5 : PRINT;"   Mondsy:";
        LOCATE  7, 5 : PRINT;"  Tuesday:";
        LOCATE  9, 5 : PRINT;"Wednesday:";
        LOCATE 11, 5 : PRINT;" Thursday:";
        LOCATE 13, 5 : PRINT;"   Friday:";
        LOCATE 15, 5 : PRINT;" Saturday:";
    
        f$ = "##,###"
        col = 16
        month = 1
        day = 1
        year = 0
        DO
        IF INKEY$ = CHR$(27) THEN EXIT LOOP
        year = year + 100
        LOCATE 1,5 : PRINT;year
    
        t = astroday(year, month, day) MOD 7
        IF t = 0 THEN INCR c0
        IF t = 1 THEN INCR c1
        IF t = 2 THEN INCR c2
        IF t = 3 THEN INCR c3
        IF t = 4 THEN INCR c4
        IF t = 5 THEN INCR c5
        IF t = 6 THEN INCR c6
    
        LOCATE  3,col : PRINT;c0
        LOCATE  5,col : PRINT;c1
        LOCATE  7,col : PRINT;c2
        LOCATE  9,col : PRINT;c3
        LOCATE 11,col : PRINT;c4
        LOCATE 13,col : PRINT;c5
        LOCATE 15,col : PRINT;c6
    
        LOOP
        WAITKEY$
    
                                                            '
        END FUNCTION                                        '
                                                            '
    REM *****************************                       '
    REM * What day of the week does *                       '
    REM * the 1st fall on?          *                       '
    REM *****************************                       '
                                                            '
    FUNCTION AstroDay(year AS LONG, _                       '
                     month AS LONG, _                       '
                     day   AS LONG) AS LONG                 '
        DIM y AS DOUBLE                                     '
        y = year + (month - 2.85) / 12                      '
        AstroDay = INT(INT(INT(367 * y) - 1.75 * _          '
                   INT(y) + day) -0.75 * _                  '
                   INT(0.01 * y)) + 1721116                 '
        END FUNCTION                                        '
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.

    Comment


    • #3
      Code:
       LOCAL ST as SYSTEMTIME , FT AS FILETIME
      
        FOR YYYY =  1101 TO 9901  STEP 100  ' probably out of range
              ST.wYear = YYYY 
              ST.wMOnth  = 1
              ST.wDay     = 1 
              SystemTimeToFileTime  st, ft 
              FiletimeToSystemTime   ft, st    ' will return ST with day of week filled in 
              IF st.wDayOfWeek =  0 THEN    ' Sunday 
                  ItCanHappen  = %TRUE 
                  EXIT FOR
             END IF 
       NEXT
      
       If ISTRUE ItcanHappen THEN 
         ....
      MCM
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Not sure the point unless prepping to prevent a Y3K problem?


        Had to razz...but got the point
        Engineer's Motto: If it aint broke take it apart and fix it

        "If at 1st you don't succeed... call it version 1.0"

        "Half of Programming is coding"....."The other 90% is DEBUGGING"

        "Document my code????" .... "WHYYY??? do you think they call it CODE? "

        Comment


        • #5
          Well done Mel. Extra points for speed of reply (1 hour).

          Code:
          If ISTRUE ItcanHappen THEN ..MCM
          Bravo. Cleverly done, Bro. (Points deducted for non-compileable untested code though)

          PS, Had you peeked, the solution (MUCH more complicated/convoluted/longer/... than yours or Mel's) used 00's as first days of the centuries, not 01's. Wouldn't affect your solution though.

          PSS, wouldn't the first century (AD) start at 0, not 1101, as well? (alas and alack ... more point deductions)

          ===================================
          "The secret of (political) success
          is honesty and fair dealing.
          If you can fake those,
          you've got it made."
          Groucho Marx
          ===================================
          Last edited by Gösta H. Lovgren-2; 14 Feb 2009, 09:13 PM.
          It's a pretty day. I hope you enjoy it.

          Gösta

          JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
          LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

          Comment


          • #6
            PSS, wouldn't the first century (AD) start at 0, not 1101
            No. Our current calendar jumped from 1 BC to 1 AD. There is no year zero.

            Something I forgot when I wrote the program. However, like you noted, it doesn't matter. Same results calculating Sunday whether the starting year is zero or one.
            There are no atheists in a fox hole or the morning of a math test.
            If my flag offends you, I'll help you pack.

            Comment


            • #7
              I do "Big Ideas". Details are left to staff.

              BTW, the snippet may be non-compileable, but it is NOT an untested technique. I do this all the time.
              Michael Mattias
              Tal Systems (retired)
              Port Washington WI USA
              [email protected]
              http://www.talsystems.com

              Comment


              • #8
                Originally posted by Michael Mattias View Post
                I do "Big Ideas". Details are left to staff.

                BTW, the snippet may be non-compileable, but it is NOT an untested technique. I do this all the time.
                Not to be "snippy", Mr. Big Head, but if it can't be run, it's untested, unless you are stating you have tested it and gotten results under conditions given. (As Mel did.) In which case I believe you.
                It's a pretty day. I hope you enjoy it.

                Gösta

                JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                Comment


                • #9
                  Mel,
                  are you sure your code works? Leave it running for a minute and it appears that the first day of the year stops falling on any of the 7 weekdays!

                  Paul.

                  Comment


                  • #10
                    Originally posted by Paul Dixon View Post
                    ...are you sure your code works...
                    Well, after calculating several hundred thousand millenia into the future, it probably does fall short after a while.
                    There are no atheists in a fox hole or the morning of a math test.
                    If my flag offends you, I'll help you pack.

                    Comment


                    • #11
                      Originally posted by Mel Bishop View Post
                      Well, after calculating several hundred thousand millenia into the future, it probably does fall short after a while.
                      Maybe won't have weekdays in the future. And Mel just proved that.

                      ==========================
                      "While we are postponing,
                      life speeds by."
                      Seneca (3BC
                      65AD)
                      ==========================
                      It's a pretty day. I hope you enjoy it.

                      Gösta

                      JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
                      LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

                      Comment

                      Working...
                      X