This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,45 @@
function startsOnFriday(month, year)
{
// 0 is Sunday, 1 is Monday, ... 5 is Friday, 6 is Saturday
return new Date(year, month, 1).getDay() === 5;
}
function has31Days(month, year)
{
return new Date(year, month, 31).getDate() === 31;
}
function checkMonths(year)
{
var month, count = 0;
for (month = 0; month < 12; month += 1)
{
if (startsOnFriday(month, year) && has31Days(month, year))
{
count += 1;
document.write(year + ' ' + month + '<br>');
}
}
return count;
}
function fiveWeekends()
{
var
startYear = 1900,
endYear = 2100,
year,
monthTotal = 0,
yearsWithoutFiveWeekends = [],
total = 0;
for (year = startYear; year <= endYear; year += 1)
{
monthTotal = checkMonths(year);
total += monthTotal;
// extra credit
if (monthTotal === 0)
yearsWithoutFiveWeekends.push(year);
}
document.write('Total number of months: ' + total + '<br>');
document.write('<br>');
document.write(yearsWithoutFiveWeekends + '<br>');
document.write('Years with no five-weekend months: ' + yearsWithoutFiveWeekends.length + '<br>');
}
fiveWeekends();

View file

@ -0,0 +1,61 @@
var Months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'
];
var leap = 0,
// Relative offsets between first day of each month
offset = [3,0,3,2,3,2,3,3,2,3,2,3],
// Months that contain 31 days
longMonths = [1,3,5,7,8,10,12],
startYear = 1900,
year = startYear,
endYear = 2100,
// Jan 1, 1900 starts on a Monday
day = 1,
totalPerYear = 0,
total = 0,
without = 0;
for (; year < endYear + 1; year++) {
leap = totalPerYear = 0;
if (year % 4 === 0) {
if (year % 100 === 0) {
if (year % 400 === 0) {
leap = 1;
}
} else {
leap = 1;
}
}
for (var i = 0; i < offset.length; i++) {
for (var j = 0; day === 5 && j < longMonths.length; j++) {
if (i + 1 === longMonths[j]) {
console.log(year + '-' + Months[i]);
totalPerYear++;
total++;
break;
}
}
// February -- if leap year, then +1 day
if (i == 1) {
day = (day + leap) % 7;
} else {
day = (day + offset[i]) % 7;
}
}
if (totalPerYear === 0) {
without++;
}
}
console.log('Number of months that have five full weekends from 1900 to 2100: ' + total);
console.log('Number of years without any five full weekend months: ' + without);

View file

@ -0,0 +1,25 @@
longmonth = [1 3 5 7 8 10 12];
i = 1;
for y = 1900:2100
for m = 1:numel(longmonth)
[num,name] = weekday(datenum(y,longmonth(m),1));
if num == 6
x(i,:) = datestr(datenum(y,longmonth(m),1),'mmm yyyy'); %#ok<SAGROW>
i = i+1;
end
end
end
fprintf('There are %i months with 5 weekends between 1900 and 2100.\n',length(x))
fprintf('\n The first 5 months are:\n')
for j = 1:5
fprintf('\t %s \n',x(j,:))
end
fprintf('\n The final 5 months are:\n')
for j = length(x)-4:length(x)
fprintf('\t %s \n',x(j,:))
end

View file

@ -0,0 +1,6 @@
ms = as.Date(sapply(c(1, 3, 5, 7, 8, 10, 12),
function(month) paste(1900:2100, month, 1, sep = "-")))
ms = format(sort(ms[weekdays(ms) == "Friday"]), "%b %Y")
message("There are ", length(ms), " months with five weekends.")
message("The first five: ", paste(ms[1:5], collapse = ", "))
message("The last five: ", paste(tail(ms, 5), collapse = ", "))

View file

@ -6,31 +6,28 @@ if yStart=='' then yStart=1900 /*if not specified, use default. */
if yStop =='' then yStop =2100 /* " " " " " */
years=yStop-yStart+1 /*calculate the # of yrs in range*/
haps=0 /*num of five weekends happenings*/
yr5.=0 /*if a year has any five-weekends*/
!.=0 /*if a year has any five-weekends*/
do y=yStart to yStop /*process the years specified. */
do m=1 for 12; wd.=0 /*each month, each yr*/
do m=1 for 12; wd.=0 /*process each month, each year. */
if m==2 then month.2=28+leapyear(y) /*handle #days in Feb*/
do d=1 for month.m; dat_=y"-"right(m,2,0)'-'right(d,2,0)
?=left(date('W', dat_, "I"), 2); upper ?
wd.?=wd.?+1 /*? is 1st 2 chars of weekday*/
end /*d*/ /*WD.su=# of Sundays in month*/
parse upper value date('W', dat_, "I") with ? 3
wd.?=wd.?+1 /*? is the 1st 2 chars of weekday*/
end /*d*/ /*WD.su = # of Sundays in a month*/
if wd.su\==5 | wd.fr\==5 | wd.sa\==5 then iterate /*5 W.E.s?*/
haps=haps+1 /*bump ctr*/
say 'There are five weekends in' y date('M', dat_, "I")
yr5.y=1 /*indicate the year has 5WEs.*/
haps=haps+1; !.y=1 /*bump ctr; indicate yr has 5 WEs*/
end /*m*/
end /*y*/
say
say 'There were ' haps " occurrence"s(haps),
'of five-weekend months in year's(years) yStart''yStop; say
no5s=0
do y=yStart to yStop; if yr5.y then iterate /*skip if OK*/
no5s=no5s+1
say 'Year ' y " doesn't have any five-weekend months."
end /*y*/
say 'There were ' haps " occurrence"s(haps) 'of five-weekend months in year's(years) yStart''yStop
say; #=0
do y=yStart to yStop; if !.y then iterate /*skip if OK*/
#=#+1
say 'Year ' y " doesn't have any five-weekend months."
end /*y*/
say
say "There are " no5s ' year's(no5s),
"that haven't any five─weekend months in year"s(years) yStart''yStop
say "There are " # ' year's(#) "that haven't any five─weekend months in year"s(years) yStart''yStop
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────LEAPYEAR subroutine─────────────────*/
leapyear: procedure; parse arg y /*year could be: Y, YY, YYY, YYYY*/

View file

@ -7,31 +7,28 @@ if yStart=='' then yStart=1900 /*if not specified, use default. */
if yStop =='' then yStop =2100 /* " " " " " */
years=yStop-yStart+1 /*calculate the # of yrs in range*/
haps=0 /*num of five weekends happenings*/
yr5.=0 /*if a year has any five-weekends*/
do y=yStart to yStop /*process the years specified. */
do m=1 for 12; wd.=0 /*process each month in each year*/
if m==2 then month.2=28+leapyear(y) /*handle #days in Feb.*/
do d=1 for month.m
?=dow(m,d,y) /*get day-of-week for mm/dd/yyyy.*/
wd.?=wd.?+1 /*?: 1=Sun, 2=Mon, ∙∙∙ 7=Sat */
end /*d*/
if wd.1\==5 | wd.6\==5 | wd.7\==5 then iterate /*5 WEs ? */
haps=haps+1 /*bump ctr*/
say 'There are five weekends in' y word(@months,m)
yr5.y=1 /*indicate this year has 5 WEs. */
end /*m*/
end /*y*/
!.=0 /*if a year has any five-weekends*/
do y=yStart to yStop /*process the years specified. */
do m=1 for 12; wd.=0 /*process each month in each year*/
if m==2 then month.2=28+leapyear(y) /*handle # days in Feb.*/
do d=1 for month.m
?=dow(m,d,y) /*get day-of-week for mm/dd/yyyy.*/
wd.?=wd.?+1 /*?: 1=Sun, 2=Mon, ∙∙∙ 7=Sat */
end /*d*/
if wd.1\==5 | wd.6\==5 | wd.7\==5 then iterate /*5 WEs ? */
say 'There are five weekends in' y word(@months,m)
haps=haps+1; !.y=1 /*bump ctr; indicate yr has 5 WEs*/
end /*m*/
end /*y*/
say
say 'There were ' haps " occurrence"s(haps),
'of five-weekend months in year's(years) yStart''yStop; say
no5s=0
do y=yStart to yStop; if yr5.y then iterate /*skip if OK*/
no5s=no5s+1
say 'Year ' y " doesn't have any five-weekend months."
end /*y*/
say 'There were ' haps " occurrence"s(haps) 'of five-weekend months in year's(years) yStart''yStop
#=0; say
do y=yStart to yStop; if !.y then iterate /*skip if OK*/
#=#+1
say 'Year ' y " doesn't have any five-weekend months."
end /*y*/
say
say "There are " no5s ' year's(no5s),
"that haven't any five─weekend months in year"s(years) yStart''yStop
say "There are " # ' year's(#) "that haven't any five─weekend months in year"s(years) yStart''yStop
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────DOW─────────────────────────────────*/
dow: procedure; parse arg m,d,y; if m<3 then do; m=m+12; y=y-1; end

View file

@ -0,0 +1,28 @@
/*REXX program finds months with 5 weekends in them (given a date range)*/
month. =31 /*month days; Feb. is skipped. */
month.4=30; month.6=30; month.9=30; month.11=30 /*30-day months*/
yStart=1900; yStop=2100 /*define start and stop years. */
haps=0 /*num of five weekends happenings*/
!.=0 /*if a year has any five-weekends*/
do y=yStart to yStop /*process the years specified. */
do m=1 for 12; wd.=0 /*each month except Feb, each yr.*/
if m==2 then iterate /*if month is February, skip it. */
do d=1 for month.m; dat_=y"-"right(m,2,0)'-'right(d,2,0)
parse upper value date('W', dat_, "I") with ? 3
wd.?=wd.?+1 /*? is 1st 2 chars of tge weekday*/
end /*d*/ /*WD.su=# of Sundays in the month*/
if wd.su\==5 | wd.fr\==5 | wd.sa\==5 then iterate /*5 W.E.s?*/
say 'There are five weekends in' y date('M', dat_, "I")
haps=haps+1; !.y=1 /*bump ctr; indicate yr has 5 WEs*/
end /*m*/
end /*y*/
say
say 'There were ' haps " occurrences of five-weekend months in years" yStart''yStop; say
#=0
do y=yStart to yStop; if !.y then iterate /*skip if OK*/
#=#+1
say 'Year ' y " doesn't have any five-weekend months."
end /*y*/
say
say "There are " # " years that haven't any five─weekend months in years" yStart''yStop
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,23 @@
/*REXX program finds months with 5 weekends in them (given a date range)*/
month.=31; yStart=1900; yStop=2100 /*month days; range of years. */
month.2=0; month.4=0; month.6=0; month.9=0; month.11=0 /*¬31 day months*/
haps=0 /*num of five weekends happenings*/
!.=0 /*if a year has any five-weekends*/
do y=yStart to yStop /*process the years specified. */
do m=1 for 12; if month.m==0 then iterate /*test 31-day mons*/
dat_=y"-"right(m,2,0)'-01' /*get date in the proper format. */
if left(date('W',dat_,"I"),2)\=='Fr' then iterate /*Friday?*/
say 'There are five weekends in' y date('M', dat_, "I")
haps=haps+1; !.y=1 /*bump ctr; indicate yr has 5 WEs*/
end /*m*/
end /*y*/
say
say 'There were ' haps " occurrences of five-weekend months in years" yStart''yStop; say
#=0
do y=yStart to yStop; if !.y then iterate /*skip if OK*/
#=#+1
say 'Year ' y " doesn't have any five-weekend months."
end /*y*/
say
say "There are " # " years that haven't any five─weekend months in years" yStart''yStop
/*stick a fork in it, we're done.*/