views:

1132

answers:

5

In order to allocate enough space for an array element for each weekday of a year, I'm trying to work out the maximum number of rows I would need. That lead me onto the question of how I would work it out.

Would you have to calculate the number of days in every year for the next n years and go with that? Or is there (as I suspect) a more elegant solution involving the numbers 365, 366, 2 and 7?

Which libraries would help?

+14  A: 

The maximum number of days in a year is 366, this gives us 52 full weeks. In those 52 weeks there are at least 52 * 5 = 260 weekdays.

We are left with 2 days (52 * 7 = 364), it is possible that these 2 days are weekdays.

So maximum number of weekdays in a year is 262.

GvS
+1, with a remark. It is not only possible that the 2 days are weekdays, it is extremely likely. Besides we are looking at the max value ;-).
Gamecat
Tx, I did not want to calculate the probability that they are a weekday, but a small mental experiement made it clear it is possible.
GvS
+8  A: 

The first thing to remember is that there are only 14 possible patterns of days in a year: two for each day of the week (one for leap year, one for non-leap year).

The second thing to remember is that the first 364 days of the year are irrelevant, because thats a multiple of 7 (and thus there are 5 x 52 = 260 weekdays in the first 364 days always).

So you only care about the last 1-2 days.

They can both be weekdays so the answer to the question is 262.

If you want a fast algorithm to work out the number of weekdays in any given year, you just need to come up with a formula that turns a year into the right year pattern. This shouldn't be too hard given that leap years are predictable.

cletus
+5  A: 

The answers by cletus and GvS are mathematically correct, but I am wondering if you are doing too much effort. If you just have to make certain your array is big enough, calculate with 53 weeks with 5 working days, ergo 265 days. Since a year is always 52 and a fraction weeks long, just rounding up to the next full integer will always give you enough elements.

If your array elements are not enourmously big in memory consumption, you can live with the small amount of waste because of elements that you will never be used. Good enough is good enough.

And you will have saved at least 10 minutes working out the exact solution (well, that's what I would probably need at least ;-) which you can spend coding.

Treb
But, since we know the maximum is 262 days, what's the point allocating that extra space? That answer really only makes sense in the absence of the answers from cletus and GvS...
Dominic Rodger
Yes, now we know the exact value. My point is that sometimes you don't need to know it. A <emphasis>good</emphasis> estimate is often preferable to spending long hours figuring out the exact answer.
Treb
A: 

Here is how you could calculate an exact amount of week days in a particular year using C# (.NET Framework):

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // in which year which should count week days
            var year = DateTime.Now.Year;

            // first day of this year
            var date = new DateTime(year, 1, 1);

            var count = 0;

            // if date is still within our year, proceed
            while (date.Year < year + 1)
            {
                if (date.DayOfWeek == DayOfWeek.Monday ||
                    date.DayOfWeek == DayOfWeek.Tuesday ||
                    date.DayOfWeek == DayOfWeek.Wednesday ||
                    date.DayOfWeek == DayOfWeek.Thursday ||
                    date.DayOfWeek == DayOfWeek.Friday)
                {
                    count++;
                }
                date.AddDays(1);
            }

            Console.Write(count);
        }
    }
}
Koistya Navin
This will certainly work as a brute force algorithm but wouldn't it be more efficient to just count the weekdays for the first and last week and add 250 (5 days * 50 weeks) for all of the year's whole weeks? Why increment, one at a time, for 250 repetitions when you know what the answer will be?
Mark Brittingham
This way you will also need to take into consideration leap year.
Koistya Navin
+4  A: 

According to this website

http://kalender-365.de/weekdays.php

there are 263 weekdays between February 1 2012 and February 1, 2013, exceeding the maximum of 262 claimed above.

Eric Hawk