Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Forum Search
Highly Rated Users
Forum Statistics
UsersTotal Post History
- Posts:
- 45703
- Topics:
- 9396
7-Day Post History
- New Posts:
- 342
- New Topics:
- 80
- Active Topics:
- 88
Our newest member
Other
-
FAQ
All times are UTC [ DST ]
Google Ads
LTD Puzzle 1: Friday the Thirteenths
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
35 posts • Page 1 of 2 • 1, 2
Please wait...
LTD Puzzle 1: Friday the Thirteenths
With another friday the thirteenth fast approaching, now seems like a good time for a themed programming puzzle. The goal is to identify all friday the thirteenths for a given timeframe. We'll use a relatively small number of years, like 10. This should make it a little easier in procedural languages.
The rules:
When posting code, please use the [hide]some code here[/hide] tags, so that you don't ruin the fun for others.
Be creative.
Be sure to mention what language it is!
Don't do drugs.
Points will be awarded for "cool code", as identified by whoever chooses to do so via LessThanDot's rating system (http://wiki.lessthandot.com/index.php/F ... ing_system). The winner will be everyone who learns something by either reading or writing the code presented.
Have fun!
Alex
The rules:
When posting code, please use the [hide]some code here[/hide] tags, so that you don't ruin the fun for others.
Be creative.
Be sure to mention what language it is!
Don't do drugs.
Points will be awarded for "cool code", as identified by whoever chooses to do so via LessThanDot's rating system (http://wiki.lessthandot.com/index.php/F ... ing_system). The winner will be everyone who learns something by either reading or writing the code presented.
Have fun!
Alex
And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.
My Crummy Web Page
My Crummy Web Page
-

AlexCuse - LTD Admin

-








- Posts: 3532
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
Re: Friday the Thirteenths
Here is the short version (i.e. doesn't bother with options, summaries, etc and is less efficient (cycles through every date, instead of just the 13th of every month)
Code is hidden, SHOW
a smile is worth a thousand kind words, so smile, it's easy! 
CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless

CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
-

damber - LTD Admin

-





- Posts: 1959
- Joined: Tue Oct 09, 2007 1:48 pm
- Location: North Wales, UK
Re: Friday the Thirteenths
Andy
-

HarleyQuinn - LTD Admin

-
- Posts: 308
- Joined: Tue Dec 18, 2007 3:14 pm
- Location: Sheffield, UK
Re: Friday the Thirteenths
Hers is one that uses VB.Net and linq and the console.
this one takes 708 milliseconds to spit out all the 13ths from 12/02/200 (yes 200) until 12/02/5000 yes 5000.
Code is hidden, SHOW
this one takes 708 milliseconds to spit out all the 13ths from 12/02/200 (yes 200) until 12/02/5000 yes 5000.
-

chrissie1 - LTD Admin

-










- Posts: 6038
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: Friday the Thirteenths
And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.
My Crummy Web Page
My Crummy Web Page
-

AlexCuse - LTD Admin

-








- Posts: 3532
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
Re: Friday the Thirteenths
Denis The SQL Menace
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
o
o
o @..@
(----)
( )--( )
o0..0o
-

SQLDenis - LTD Admin

-










- Posts: 11778
- Joined: Wed Oct 10, 2007 6:43 pm
- Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Re: Friday the Thirteenths
- sean
Re: Friday the Thirteenths
Sean I looked at that code for 5 minutes until I found what you were talking about 
yes that is a typo I forgot to change it in the WHERE clause (that was from a 1st version I posted, I have changed it now)
The code I ran was actually identical to what you posted

yes that is a typo I forgot to change it in the WHERE clause (that was from a 1st version I posted, I have changed it now)
The code I ran was actually identical to what you posted
Denis The SQL Menace
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
o
o
o @..@
(----)
( )--( )
o0..0o
-

SQLDenis - LTD Admin

-










- Posts: 11778
- Joined: Wed Oct 10, 2007 6:43 pm
- Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Re: Friday the Thirteenths
And of course I have to post an ASP/VBScript solution. I tried to do it differently then everyone else (at most it loops n+2 where n is number of 13's):
Apparently we need to work on the line wrap for the Hide function
Copy it into a text editor if you want to see the couple lines that are getting cut off.
Code is hidden, SHOW
Apparently we need to work on the line wrap for the Hide function
"Perhaps the more that a person is intellectual the more they are prone to misconceptions" - Taiichi Ohno
-

tarwn - LTD Admin

-





- Posts: 1451
- Joined: Fri Oct 12, 2007 11:10 am
- Location: Raleigh, NC, USA
Re: Friday the Thirteenths
Faster, better, stronger...
http://weblogs.asp.net/blogs/mikebosch/friday.png
http://weblogs.asp.net/blogs/mikebosch/friday.png
- xtcsonik
- Newbie

-
- Posts: 1
- Joined: Mon Jun 09, 2008 6:41 pm
-

cLFlaVA - LTD Senior Moderator

-
- Posts: 203
- Joined: Fri Oct 12, 2007 3:51 pm
- eksimba
- Newbie

-
- Posts: 1
- Joined: Tue Jun 10, 2008 4:47 am
Re: Friday the Thirteenths
Old school Perl 5
Usage: Perl f13.pl <start year> [end year]
Omit end year and it'll default to 10 years up
Runs in 477ms from year 200 to 5000 with the assumption we only care about the Gregorian calendar. If I take out the printf, it runs in 147ms. My PC is a 1.6GHz dual core Dell Optiplex 745 running XP.
I double commented the time measurement stuff because the module Time::HiRes is not installed by default and will fail to run on plain installs of Perl.
It's funny that in doing this exercise, I found an error in the DOW formula on the WikiPedia page. I commented my code change as "Fix for leap years".
Usage: Perl f13.pl <start year> [end year]
Omit end year and it'll default to 10 years up
Code is hidden, SHOW
Runs in 477ms from year 200 to 5000 with the assumption we only care about the Gregorian calendar. If I take out the printf, it runs in 147ms. My PC is a 1.6GHz dual core Dell Optiplex 745 running XP.
I double commented the time measurement stuff because the module Time::HiRes is not installed by default and will fail to run on plain installs of Perl.
It's funny that in doing this exercise, I found an error in the DOW formula on the WikiPedia page. I commented my code change as "Fix for leap years".
Last edited by spoulson on Tue Jun 10, 2008 9:02 pm, edited 2 times in total.
-

spoulson - Senior Apprentice

-


- Posts: 203
- Joined: Mon Jun 02, 2008 2:37 am
- Location: Middletown, DE, USA
Re: Friday the Thirteenths
C# 2.0
There's no need to waist memory on a creating a list or to loop through ALL dates in range..
There's no need to waist memory on a creating a list or to loop through ALL dates in range..
Code is hidden, SHOW
- webbes
- Newbie

-
- Posts: 4
- Joined: Tue Jun 10, 2008 3:09 pm
Re: Friday the Thirteenths
I would love to see a Python, Ruby, F# or Haskell solution
Denis The SQL Menace
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
o
o
o @..@
(----)
( )--( )
o0..0o
-

SQLDenis - LTD Admin

-










- Posts: 11778
- Joined: Wed Oct 10, 2007 6:43 pm
- Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Re: Friday the Thirteenths
Code is hidden, SHOW
- ilitirit
- Newbie

-
- Posts: 2
- Joined: Tue Jun 10, 2008 6:29 pm
Re: Friday the Thirteenths
The first time I wrote a function like this, it was in Pick basic. Haven't used that for a number of years, so here's one in VBA. Rather than cycling through all the months:
Code is hidden, SHOW
Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
-

traingamer - Sage

-


- Posts: 989
- Joined: Thu Feb 28, 2008 4:13 pm
- Location: St. Louis, MO, US
Friday the 13th's in RUBY
- coneybeare
- Newbie

-
- Posts: 1
- Joined: Tue Jun 10, 2008 8:14 pm
Re: Friday the Thirteenths
Java -- Just a quick implementation that prints every Friday 13th for the next 10 years (from whenever you run it).
Code is hidden, SHOW
- mbaird
- Newbie

-
- Posts: 1
- Joined: Tue Jun 10, 2008 9:52 pm
Re: Friday the Thirteenths
An implementation in python using tables to find friday the 13th according to the first day of the year:
Code is hidden, SHOW
- fullpunk
- Newbie

-
- Posts: 1
- Joined: Wed Jun 11, 2008 5:35 am
Re: Friday the Thirteenths
VB6. This function is VERY fast.
I wrote a VB6 app using this that calculates the years 1900 through 4899 (including displaying it in a textbox) 100 times. It takes about 0.72 seconds so that is 0.0072 seconds to calculate 5000 years worth of Friday the 13ths.
I wrote a VB6 app using this that calculates the years 1900 through 4899 (including displaying it in a textbox) 100 times. It takes about 0.72 seconds so that is 0.0072 seconds to calculate 5000 years worth of Friday the 13ths.
Code is hidden, SHOW
It's more cheesier if you do it right the first time!
-

Emtucifor - Senior Sage

-










- Posts: 2152
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: Friday the Thirteenths
And now that I've looked at all your code, I see that traingamer had the same idea and so did a couple of other people.
In fact, his solution's output is quite a bit more readable than mine!
I also am doing some extra stuff to calculate the correct index offset, but he just reordered the array list. So his way is superior if it's giving the right output.
In fact, his solution's output is quite a bit more readable than mine!

I also am doing some extra stuff to calculate the correct index offset, but he just reordered the array list. So his way is superior if it's giving the right output.
It's more cheesier if you do it right the first time!
-

Emtucifor - Senior Sage

-










- Posts: 2152
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: Friday the Thirteenths
ErikE wrote:And now that I've looked at all your code, I see that traingamer had the same idea and so did a couple of other people.
I wasn't aware of this until after participating in this puzzle, but there's a theorum of Babwani's Congruence that describes formulae for functionally computing a missing datetime field when the others are known. In his paper he also discusses the algorithm that traingamer, fullpunk, and ErikE used in their submissions. It's worth a read if you're ever tasked with coding fast datetime calculations.
-

spoulson - Senior Apprentice

-


- Posts: 203
- Joined: Mon Jun 02, 2008 2:37 am
- Location: Middletown, DE, USA
Re: Friday the Thirteenths
I used Excel for my analysis in a sort of brute-force graphical way:
1. Generate a list of all the Friday the 13ths for every year from 1900 through 9999.
2. Calculate the first day of the year (represented as 1 - 7 or 0 - 6) for each Fri 13th and whether the year is a leap year.
3. Use a pivot table to move months to their own columns at the top, including year and leap year information on the left.
4. Do an advanced data filter to get only unique variations of leap year + first day of year + Fri 13th pattern.
5. Notice there are only 14 variations, one for each day of the week in a leap and non-leap year.
6. Try as I might, I couldn't render the resulting pattern in a way that compressed easily or decompressed cheaply, so I finally just stuck the entire thing in an array, of sorts.
7. The list can be reduced to only 7 variations with the understanding that in leap years the "first day of the year" calculation will be advanced one for month 3 and up. However in my algorithm the extra logic for "month 3 and up" made the whole thing slower rather than just using the full 14-day map. For small ranges of years, the 7-variation list with the month 3 logic (or think of it as month 1 & 2 logic if you like) is faster.
After seeing traingamer's solution, I realized I could have simulated the output of a brute-force approach with:
Map(0) = "January 13, <year>" & vbcrlf & "October 13, <year>"
And the output would involve Replace(Map(index), "<year>", StartYear)
Now my somewhat cryptic and table-bound result (I used a textbox with JFMAMJJASOND at the top lined up with the x's) can be friendly looking but still just as fast.
Also, reading through the document found at spoulson's link, I wonder if calculating the day of the week for the month rather than the entire year could yield some optimization for my code. Obviously I'm doing more calculation than traingamer.
1. Generate a list of all the Friday the 13ths for every year from 1900 through 9999.
2. Calculate the first day of the year (represented as 1 - 7 or 0 - 6) for each Fri 13th and whether the year is a leap year.
3. Use a pivot table to move months to their own columns at the top, including year and leap year information on the left.
4. Do an advanced data filter to get only unique variations of leap year + first day of year + Fri 13th pattern.
5. Notice there are only 14 variations, one for each day of the week in a leap and non-leap year.
6. Try as I might, I couldn't render the resulting pattern in a way that compressed easily or decompressed cheaply, so I finally just stuck the entire thing in an array, of sorts.
7. The list can be reduced to only 7 variations with the understanding that in leap years the "first day of the year" calculation will be advanced one for month 3 and up. However in my algorithm the extra logic for "month 3 and up" made the whole thing slower rather than just using the full 14-day map. For small ranges of years, the 7-variation list with the month 3 logic (or think of it as month 1 & 2 logic if you like) is faster.
After seeing traingamer's solution, I realized I could have simulated the output of a brute-force approach with:
Map(0) = "January 13, <year>" & vbcrlf & "October 13, <year>"
And the output would involve Replace(Map(index), "<year>", StartYear)
Now my somewhat cryptic and table-bound result (I used a textbox with JFMAMJJASOND at the top lined up with the x's) can be friendly looking but still just as fast.
Also, reading through the document found at spoulson's link, I wonder if calculating the day of the week for the month rather than the entire year could yield some optimization for my code. Obviously I'm doing more calculation than traingamer.
It's more cheesier if you do it right the first time!
-

Emtucifor - Senior Sage

-










- Posts: 2152
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: Friday the Thirteenths
From the looks of it, if you wanted to generalize the day of week calculation, you should be able to generate a lookup table containing 4 calendar years of day/month/dow, because it just repeats. Then lookup by day, month, and year mod 4. No runtime calculation.
-

spoulson - Senior Apprentice

-


- Posts: 203
- Joined: Mon Jun 02, 2008 2:37 am
- Location: Middletown, DE, USA
35 posts • Page 1 of 2 • 1, 2


LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.