Five weekends: Difference between revisions

Added Easylang
m (→‎{{header|RPL}}: typo in comment)
(Added Easylang)
 
(5 intermediate revisions by 4 users not shown)
Line 931:
1984, 1990, 2001, 2007, 2012, 2018, 2029, 2035, 2040, 2046, 2057, 2063, 2068, 2074,
2085, 2091, 2096}}}</syntaxhighlight>
 
===Using integer math===
An alternative to the date object manipulation methods above.
<syntaxhighlight lang="applescript">on monthsWithFiveWeekends(startYear, endYear)
set Dec1 to (current date)
tell Dec1 to set {its day, its month, its year} to {1, December, startYear - 1}
set daysFromBaseFriday to (Dec1's weekday as integer) - Friday
set longMonths to {"January", "March", "May", "July", "August", "October", "December"}
set daysBetween to {31, 59, 61, 61, 31, 61, 61} -- Days since starts of preceding long months.
set hits to {}
set hitlessYears to {}
repeat with y from startYear to endYear
set noHIts to true
-- Find long months that begin on Fridays.
repeat with i from 1 to 7
set daysFromBaseFriday to daysFromBaseFriday + (daysBetween's item i)
if ((i = 2) and (y mod 4 = 0) and ((y mod 100 > 0) or (y mod 400 = 0))) then ¬
set daysFromBaseFriday to daysFromBaseFriday + 1 -- Leap year.
if (daysFromBaseFriday mod 7 = 0) then
set end of hits to (longMonths's item i) & (space & y)
set noHIts to false
end if
end repeat
if (noHIts) then set end of hitlessYears to y
end repeat
return {hits:hits, hitlessYears:hitlessYears}
end monthsWithFiveWeekends
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
on task()
set {startYear, endYear} to {1900, 2100}
set theResults to monthsWithFiveWeekends(startYear, endYear)
set output to {((count theResults's hits) as text) & " of the months from " & startYear & ¬
" to " & endYear & " have five weekends,", ¬
"the first and last five of these months being:"}
set end of output to join(theResults's hits's items 1 thru 5, ", ") & " …"
set end of output to "… " & join(theResults's hits's items -5 thru -1, ", ")
set hitlessCount to (count theResults's hitlessYears)
set end of output to linefeed & hitlessCount & " of the years have no such months:"
set cut to (hitlessCount + 1) div 2
set end of output to join(theResults's hitlessYears's items 1 thru cut, ", ")
set end of output to join(theResults's hitlessYears's items (cut + 1) thru -1, ", ")
return join(output, linefeed)
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"201 of the months from 1900 to 2100 have five weekends,
the first and last five of these months being:
March 1901, August 1902, May 1903, January 1904, July 1904 …
… March 2097, August 2098, May 2099, January 2100, October 2100
 
29 of the years have no such months:
1900, 1906, 1917, 1923, 1928, 1934, 1945, 1951, 1956, 1962, 1973, 1979, 1984, 1990, 2001
2007, 2012, 2018, 2029, 2035, 2040, 2046, 2057, 2063, 2068, 2074, 2085, 2091, 2096"</syntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">longMonths: [1 3 5 7 8 10 12]
dates: []
 
yearsWithout: 0
 
loop 1900..2100 'year [
found?: false
loop longMonths 'month [
dt: to :date .format:"d-M-YYYY" ~"1-|month|-|year|"
if friday? dt [
'dates ++ @[@[dt\Month year]]
found?: true
]
]
if not? found? ->
inc 'yearsWithout
]
 
print.lines map first.n:5 dates 'd -> ~"|to :string d\0|, |to :string d\1|"
print "..."
print.lines map last.n:5 dates 'd -> ~"|to :string d\0|, |to :string d\1|"
 
print ""
print ["Found" size dates "months in total."]
print ["There are" yearsWithout "years without any month that has 5 full weekends."]</syntaxhighlight>
 
{{out}}
 
<pre>March, 1901
August, 1902
May, 1903
January, 1904
July, 1904
...
March, 2097
August, 2098
May, 2099
January, 2100
October, 2100
 
Found 201 months in total.
There are 29 years without any month that has 5 full weekends.</pre>
 
=={{header|AutoHotkey}}==
Line 1,656 ⟶ 1,764:
2091
2096
</pre>
 
===Using C++20===
<syntaxhighlight lang="c++">
 
#include <chrono>
#include <iostream>
#include <vector>
 
int main() {
const std::vector<std::chrono::month> long_months = { std::chrono::January, std::chrono::March,
std::chrono::May, std::chrono::July, std::chrono::August, std::chrono::October, std::chrono::December };
 
int month_count = 0;
int blank_years = 0;
for ( int y = 1900; y <= 2100; ++y ) {
bool blank_year = true;
for ( std::chrono::month m : long_months ) {
std::chrono::year_month_day first_of_month{std::chrono::year{y}, m, std::chrono::day{1}};
if ( std::chrono::weekday{first_of_month} == std::chrono::Friday ) {
std::cout << first_of_month.year() << " " << first_of_month.month() << std::endl;
month_count++;
blank_year = false;
}
}
if ( blank_year ) {
blank_years++;
}
}
std::cout << "Found " << month_count << " months with five Fridays, Saturdays and Sundays." << std::endl;
std::cout << "There were " << blank_years << " years with no such months." << std::endl;
}
</syntaxhighlight>
{{ out }}
<pre>
1901 Mar
1902 Aug
1903 May
1904 Jan
1904 Jul
// elided... //
2097 Mar
2098 Aug
2099 May
2100 Jan
2100 Oct
Found 201 months with five Fridays, Saturdays and Sundays.
There were 29 years with no such months.
</pre>
 
Line 2,144 ⟶ 2,300:
Writeln(Format('Years with no 5 weekend months: %d', [lYearsWithout]));
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func leap year .
return if year mod 4 = 0 and (year mod 100 <> 0 or year mod 400 = 0)
.
func weekday year month day .
normdoom[] = [ 3 7 7 4 2 6 4 1 5 3 7 5 ]
c = year div 100
r = year mod 100
s = r div 12
t = r mod 12
c_anchor = (5 * (c mod 4) + 2) mod 7
doom = (s + t + (t div 4) + c_anchor) mod 7
anchor = normdoom[month]
if leap year = 1 and month <= 2
anchor = (anchor + 1) mod1 7
.
return (doom + day - anchor + 7) mod 7 + 1
.
mdays[] = [ 31 28 31 30 31 30 31 31 30 31 30 31 ]
mon$[] = [ "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" ]
#
for year = 1900 to 2100
for m to 12
if mdays[m] = 31 and weekday year m 1 = 6
print year & "-" & mon$[m]
sum += 1
.
.
.
print "Total : " & sum
</syntaxhighlight>
{{out}}
<pre>
1901-Mar
1902-Aug
1903-May
1904-Jan
1904-Jul
.
.
2097-Mar
2098-Aug
2099-May
2100-Jan
2100-Oct
Total : 201
</pre>
 
=={{header|Elixir}}==
Line 7,163 ⟶ 7,368:
{{libheader|Wren-date}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascriptwren">import "./date" for Date
import "./seq" for Lst
 
var dates = []
1,995

edits