All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Fifth Thursdays (Posted on 2003-10-06) Difficulty: 3 of 5
You have a club that meets the fourth Thursday of each month, and another one that meets the last Thursday of each month. Usually you have to choose to attend one meeting or the other as the fourth Thursday of the month is usually the last Thursday of the month. However, some months have five Thursdays. In those months you can attend both meetings.

Develop an algorithm to find all the fifth Thursdays of those months that have them, and set it to calculate them for ten years. You are given subroutines that convert Gregorian calendar dates to and from JD numbers, which are the number of days a given date is past a certain fixed date in the distant past (more than 6000 years ago). You also have a 2003 calendar available that tells you Jan. 2, 2003 was a Thursday.

  Submitted by Charlie    
Rating: 3.4000 (5 votes)
Solution: (Hide)
First convert January 2, 2003 to its JD form. Then add 7 successively to this JD to get JD's of all Thursdays. Use the conversion routine to find the Gregorian calendar date of each. If the day of the month is greater than 28, it is a 5th Thursday.

In the following code fragment, JD, MO, DA and YE are global variables used by the conversion subroutines.

mo = 1: da = 2: ye = 2003
GOSUB GregToJD
jd0 = jd
CLS
FOR jd = jd0 TO jd0 + 365.25 * 10 STEP 7
  GOSUB JDtoGreg
  IF da > 28 THEN
    PRINT USING "## ## ####"; mo; da; ye
  END IF
NEXT
END

By the way, subroutines that work to do those called functions in Basic are for example (old and not very elegant, but they work):

GregToJD:
REM :greg mo/da/ye --> jd at noon
GOSUB jul.to.jd
jd = jd + 2 - INT(cw(1) / 100) + INT(cw(1) / 400)
RETURN
jul.to.jd:
REM :jul mo/da/ye --> jd at noon
cw(0) = mo: cw(1) = ye: IF mo < 3 THEN cw(0) = mo + 12: cw(1) = ye - 1
jd = INT(365.25 * cw(1)) + INT(30.61 * (cw(0) + 1)) + da + 1720995!
RETURN

JDtoGreg:
REM:noon jd-->greg mo/da/ye
cw(0) = INT((jd - 1867216.25#) / 36524.25)
cw(0) = jd + 1 + cw(0) - INT(cw(0) / 4)
GOTO common.from.jd
jd.to.jul:
REM : noon jd-->jul mo/da/ye
cw(0) = jd
common.from.jd:
cw(0) = cw(0) + 1524
cw(1) = INT((cw(0) - 122.1) / 365.25)
cw(2) = INT(365.25 * cw(1))
cw(3) = INT((cw(0) - cw(2)) / 30.6001)
da = cw(0) - cw(2) - INT(30.61 * cw(3))
ye = cw(1) - 4716
mo = cw(3) - 1: IF mo > 12 THEN mo = mo - 12: ye = ye + 1
RETURN
By the way, the results:

1 30 2003
5 29 2003
7 31 2003
10 30 2003
1 29 2004
4 29 2004
7 29 2004
9 30 2004
12 30 2004
3 31 2005
6 30 2005
9 29 2005
12 29 2005
3 30 2006
6 29 2006
8 31 2006
11 30 2006
3 29 2007
5 31 2007
8 30 2007
11 29 2007
1 31 2008
5 29 2008
7 31 2008
10 30 2008
1 29 2009
4 30 2009
7 30 2009
10 29 2009
12 31 2009
4 29 2010
7 29 2010
9 30 2010
12 30 2010
3 31 2011
6 30 2011
9 29 2011
12 29 2011
3 29 2012
5 31 2012
8 30 2012
11 29 2012

Comments: ( You must be logged in to post comments.)
  Subject Author Date
JD plusnellie2003-10-12 13:23:10
A few thoughts..Brian Smith2003-10-07 14:53:33
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (1)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (6)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information