Friday the 13th

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

Problem Description

Here's a program to find dates meeting constraints on month of year, day of month, or day of week  within a given range of years.  

Background & Techniques

Today being Friday the 13th, I was looking for an excuse to  introduce the grandkids to the word meaning "fear of Friday the 13th", friggatriskaidekaphobia.   I was going to send an email telling them that it would be a good excuse to stay home from school today.   I didn't think of it in time, but it did arouse my curiosity about  future occurrences.

That led me to spend couple of hours coding the program posted here.  The problem  was generalized to display a schedule of dates meeting the conditions  described above.  

Notes for programmers 

From the programming point of view, it was  fun discover Delphi date formatting routines which simplify operations such as testing a day, month, and year set for validity or for incrementing a date by a month.  

The program has about 100 lines of code of which 30 were generated by Delphi.  Most of the other 70 written by me were comments lines or gathering information from the input controls before we start the date testing loop.  The loop logic looks has about 20 lines of code and works like this:  (User inputs are in red)

  1. Initialize a day, month, and year based on user inputs
  2. Convert the day, month year fields to a date field
  3. If the day of week of the date matches the user input day of week request, or the user said any day of week was OK, display the date as a match.
  4. If any day of the month is OK, then
    bulletIf any month is OK then increment date by one day (making a new day, month, and year),
    bulletOtherwise a specific month was requested, so set the day back to 1 and ncrement the year by 1 (making a new day, month , and year).
  5. otherwise a specific day of the month was requested and we check this
    bulletIf any month is OK then increment the date by a month (making a new day, month and year).
    bulletOtherwise a specific month was requested, so increment the date by a year (making a new day, month , and year).
  6. If the year of the new date is less than or equal to stop year,  and  the requested number of results has been met, then loop back to Step 2.

The above loop logic , equired about 20 lines of code and was the most fun to write and debug.

February 15, 2015:  Having survived Friday the 13th, I resurrected this program  to check for other occurrences this year and discovered that we have two more (in March and November).  That's the maximum that can occur in a year and we won't see three in a year again until 2026, then 2037.   It's an interesting mental exercise to understand why there are 11 year gaps going forward but only 6 years since the last occurrence when I first posted this program in in 2009.   

 

Running/Exploring the Program 

bullet Download source
bullet Download  executable

Suggestions for Further Explorations

Add other variable Holiday date options (Thanksgiving, Easter, those moved to a particular Monday of the month to make more long weekends,  etc.)

 

Original Date: February 13, 2009

Modified: May 11, 2018

 

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.