Find the last Sunday of each month: Difference between revisions

Add Easylang
(Add Easylang)
 
(11 intermediate revisions by 5 users not shown)
Line 1,270:
2013-Nov-24
2013-Dec-29
</pre>
===Using C++20===
Using C++20 this task can be completed without external libraries.
<syntaxhighlight lang="c++">
 
#include <chrono>
#include <iostream>
 
int main() {
std::cout << "The dates of the last Sunday in each month of 2023:" << std::endl;
 
for ( unsigned int m = 1; m <= 12; ++m ) {
std::chrono::days days_in_month = std::chrono::sys_days{std::chrono::year{2023}/m/std::chrono::last}
- std::chrono::sys_days{std::chrono::year{2023}/m/1} + std::chrono::days{1};
 
const unsigned int last_day = days_in_month / std::chrono::days{1};
std::chrono::year_month_day ymd{std::chrono::year{2023}, std::chrono::month{m}, std::chrono::day{last_day}};
 
while ( std::chrono::weekday{ymd} != std::chrono::Sunday ) {
ymd = std::chrono::sys_days{ymd} - std::chrono::days{1};
}
 
std::cout << ymd << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
The dates of the last Sunday in each month of 2023:
2023-01-29
2023-02-26
2023-03-26
2023-04-30
2023-05-28
2023-06-25
2023-07-30
2023-08-27
2023-09-24
2023-10-29
2023-11-26
2023-12-31
</pre>
 
Line 1,498 ⟶ 1,539:
readln;
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 ]
proc last_sundays year . .
for m to 12
d = mdays[m]
if m = 2 and leap year = 1
d = 29
.
d -= weekday year m d - 1
m$ = m
if m < 10
m$ = "0" & m
.
print year & "-" & m$ & "-" & d
.
.
last_sundays 2023
</syntaxhighlight>
 
=={{header|Elixir}}==
Line 2,272 ⟶ 2,350:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Last_day_of_each_month_of_a_year%2C_being_a_given_weekday}}
 
'''Solution'''
 
The following function retrieves the last day of each month of a year, being a given weekday:
 
[[File:Fōrmulæ - Last day of each month of a year, being a given weekday 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Last day of each month of a year, being a given weekday 02.png]]
 
[[File:Fōrmulæ - Last day of each month of a year, being a given weekday 03.png]]
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
Long y, c, d, i
 
void local fn sunday( m as Short, nd as Short)
if d > nd then d -= 7 // Don't overshoot
print y @"–"m @"–"d
d = d + 35 - nd // Advance 5 weeks minus month length
end fn
 
void local fn month( m as Short )
select m
case 1
if ( y mod 4 > 0 ) or ( y mod 100 == 0 and y mod 400 > 0 )
fn sunday( m, 31)
fn sunday( m + 1, 28)
else
d += 1 // Extra day for leap year
fn sunday( m, 31 )
fn sunday( m + 1, 29 )
end if
case 3, 5, 7, 8, 10, 12
fn sunday( m, 31 )
case 4, 6, 9, 11
fn sunday( m, 30 )
end select
end fn
 
y = 2024
// Concentrate from D. Knuth: CACM 1962;5:209
c = y / 100 + 1
d = ( 3 * c / 4 ) - ( 5 * y / 4 )
d = ( d mod 7 ) + 36 // A Sunday in February
for i = 1 to 12
fn month( i )
next
 
handleevents
</syntaxhighlight>
Output:
<pre>
2024-1-28
2024-2-25
2024-3-31
2024-4-28
2024-5-26
2024-6-30
2024-7-28
2024-8-25
2024-9-29
2024-10-27
2024-11-24
2024-12-29
</pre>
 
=={{header|Gambas}}==
Line 4,522 ⟶ 4,668:
2013-11-24
2013-12-29
</pre>
 
=={{header|RPL}}==
<code>WKDAY</code> is defined at [[Day of the week#RPL|Day of the week]]
{{works with|HP|48}}
≪ → year
≪ { }
.02 .13 '''FOR''' month
1 month .13 == DUP .01 month IFTE SWAP year + 1000000 / + + <span style="color:grey">@ Generate 1st day of the following month</span>
DUP <span style="color:blue">WKDAY</span> NEG DATE+ +
.01 '''STEP'''
2 FIX <span style="color:grey">@ Display October Sunday as ##.10 and not as ##.1</span>
≫ ≫ '<span style="color:blue">LSTSU</span>' STO
 
2013 <span style="color:blue">LSTSU</span>
{{out}}
<pre>
1: { 27.01 24.02 24.03 28.04 26.05 30.06 28.07 25.08 29.09 27.10 24.11 29.12 }
</pre>
 
Line 4,986 ⟶ 5,150:
=={{header|Wren}}==
{{libheader|Wren-date}}
<syntaxhighlight lang="ecmascriptwren">import "os" for Process
import "./date" for Date
var args = Process.arguments
Line 5,010 ⟶ 5,174:
{{out}}
<pre>
$ wren last_sundayFind_the_last_Sunday_of_each_month.wren 2013
The dates of the last Sundays in the month for 2013 are:
2013-01-27
Line 5,025 ⟶ 5,189:
2013-12-29
 
$ wren last_sundayFind_the_last_Sunday_of_each_month.wren 2020
The dates of the last Sundays in the month for 2020 are:
2020-01-26
1,983

edits