Five weekends: Difference between revisions

Content added Content deleted
(Ada solution added)
(added Inform 7 solution)
Line 315: Line 315:
r[1]{[n-4 .. n]};
r[1]{[n-4 .. n]};
# [ "Mar-2097", "Aug-2098", "May-2099", "Jan-2100", "Oct-2100" ]</lang>
# [ "Mar-2097", "Aug-2098", "May-2099", "Jan-2100", "Oct-2100" ]</lang>

=={{header|Inform 7}}==
Inform 7 has no built-in date functions, so this solution implements date types and a day-of-week function.

<lang inform7>Calendar is a room.

When play begins:
let happy month count be 0;
let sad year count be 0;
repeat with Y running from Y1900 to Y2100:
if Y is a sad year, increment the sad year count;
repeat with M running through months:
if M of Y is a happy month:
say "[M] [year number of Y].";
increment the happy month count;
say "Found [happy month count] month[s] with five weekends and [sad year count] year[s] with no such months.";
end the story.

Section - Years

A year is a kind of value. Y1 specifies a year.

To decide which number is year number of (Y - year):
decide on Y / Y1.

To decide if (N - number) is divisible by (M - number):
decide on whether or not the remainder after dividing N by M is zero.

Definition: a year (called Y) is a leap year:
let YN be the year number of Y;
if YN is divisible by 400, yes;
if YN is divisible by 100, no;
if YN is divisible by 4, yes;
no.

Section - Months

A month is a kind of value. The months are defined by the Table of Months.

Table of Months
month month number
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12

A month has a number called length. The length of a month is usually 31.
September, April, June, and November have length 30. February has length 28.

To decide which number is number of days in (M - month) of (Y - year):
let L be the length of M;
if M is February and Y is a leap year, decide on L + 1;
otherwise decide on L.

Section - Weekdays

A weekday is a kind of value. The weekdays are defined by the Table of Weekdays.

Table of Weekdays
weekday weekday number
Saturday 0
Sunday 1
Monday 2
Tuesday 3
Wednesday 4
Thursday 5
Friday 6

To decide which weekday is weekday of the/-- (N - number) of (M - month) of (Y - year):
let MN be the month number of M;
let YN be the year number of Y;
if MN is less than 3:
increase MN by 12;
decrease YN by 1;
let h be given by Zeller's Congruence;
let WDN be the remainder after dividing h by 7;
decide on the weekday corresponding to a weekday number of WDN in the Table of Weekdays.

Equation - Zeller's Congruence
h = N + ((MN + 1)*26)/10 + YN + YN/4 + 6*(YN/100) + YN/400
where h is a number, N is a number, MN is a number, and YN is a number.

To decide which number is number of (W - weekday) days in (M - month) of (Y - year):
let count be 0;
repeat with N running from 1 to the number of days in M of Y:
if W is the weekday of the N of M of Y, increment count;
decide on count.

Section - Happy Months and Sad Years

To decide if (M - month) of (Y - year) is a happy month:
if the number of days in M of Y is 31 and the weekday of the 1st of M of Y is Friday, decide yes;
decide no.

To decide if (Y - year) is a sad year:
repeat with M running through months:
if M of Y is a happy month, decide no;
decide yes.</lang>

Output:
<pre>March 1901.
August 1902.
May 1903.
January 1904.
July 1904.
...
March 2097.
August 2098.
May 2099.
January 2100.
October 2100.
Found 201 months with five weekends and 29 years with no such months.</pre>


=={{header|J}}==
=={{header|J}}==