Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Doomsday-rule/00-META.yaml
Normal file
4
Task/Doomsday-rule/00-META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Date and time
|
||||
from: http://rosettacode.org/wiki/Doomsday_rule
|
||||
59
Task/Doomsday-rule/00-TASK.txt
Normal file
59
Task/Doomsday-rule/00-TASK.txt
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
; About the task
|
||||
John Conway (1937-2020), was a mathematician who also invented several mathematically
|
||||
oriented computer pastimes, such as the famous Game of Life cellular automaton program.
|
||||
Dr. Conway invented a simple algorithm for finding the day of the week, given any date.
|
||||
The algorithm was based on calculating the distance of a given date from certain
|
||||
"anchor days" which follow a pattern for the day of the week upon which they fall.
|
||||
|
||||
; Algorithm
|
||||
The formula is calculated assuming that Sunday is 0, Monday 1, and so forth with Saturday 7, and
|
||||
|
||||
doomsday = (Tuesday(or 2) + 5(y mod 4) + 4(y mod 100) + 6(y mod 400)) % 7
|
||||
|
||||
which, for 2021, is 0 (Sunday).
|
||||
|
||||
To calculate the day of the week, we then count days from a close doomsday,
|
||||
with these as charted here by month, then add the doomsday for the year,
|
||||
then get the remainder after dividing by 7. This should give us the number
|
||||
corresponding to the day of the week for that date.
|
||||
|
||||
Month Doomsday Dates for Month
|
||||
--------------------------------------------
|
||||
January (common years) 3, 10, 17, 24, 31
|
||||
January (leap years) 4, 11, 18, 25
|
||||
February (common years) 7, 14, 21, 28
|
||||
February (leap years) 1, 8, 15, 22, 29
|
||||
March 7, 14, 21, 28
|
||||
April 4, 11, 18, 25
|
||||
May 2, 9, 16, 23, 30
|
||||
June 6, 13, 20, 27
|
||||
July 4, 11, 18, 25
|
||||
August 1, 8, 15, 22, 29
|
||||
September 5, 12, 19, 26
|
||||
October 3, 10, 17, 24, 31
|
||||
November 7, 14, 21, 28
|
||||
December 5, 12, 19, 26
|
||||
|
||||
; Task
|
||||
|
||||
Given the following dates:
|
||||
|
||||
* 1800-01-06 (January 6, 1800)
|
||||
* 1875-03-29 (March 29, 1875)
|
||||
* 1915-12-07 (December 7, 1915)
|
||||
* 1970-12-23 (December 23, 1970)
|
||||
* 2043-05-14 (May 14, 2043)
|
||||
* 2077-02-12 (February 12, 2077)
|
||||
* 2101-04-02 (April 2, 2101)
|
||||
|
||||
<br>
|
||||
Use Conway's Doomsday rule to calculate the day of the week for each date.
|
||||
<br><br>
|
||||
|
||||
; see also
|
||||
* [[wp:Doomsday rule|Doomsday rule]]
|
||||
* [https://www.archim.org.uk/eureka/archive/Eureka-36.pdf Tomorrow is the Day After Doomsday (p.28)]
|
||||
|
||||
|
||||
|
||||
|
||||
22
Task/Doomsday-rule/11l/doomsday-rule.11l
Normal file
22
Task/Doomsday-rule/11l/doomsday-rule.11l
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
F isleap(year)
|
||||
R year % 4 == 0 & (year % 100 != 0 | year % 400 == 0)
|
||||
|
||||
F weekday(year, month, day)
|
||||
V days = [‘Sunday’, ‘Monday’, ‘Tuesday’,
|
||||
‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’]
|
||||
V dooms = [[3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5],
|
||||
[4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]]
|
||||
|
||||
V c = year I/ 100
|
||||
V r = year % 100
|
||||
V s = r I/ 12
|
||||
V t = r % 12
|
||||
V c_anchor = (5 * (c % 4) + 2) % 7
|
||||
V doomsday = (s + t + (t I/ 4) + c_anchor) % 7
|
||||
V anchorday = dooms[isleap(year)][month - 1]
|
||||
V weekday = (doomsday + day - anchorday + 7) % 7
|
||||
R days[weekday]
|
||||
|
||||
L(year, month, day) [(1800, 1, 6), (1875, 3, 29), (1915, 12, 7),
|
||||
(1970, 12, 23), (2043, 5, 14), (2077, 2, 12), (2101, 4, 2)]
|
||||
print(‘#.-#02-#02 -> #.’.format(year, month, day, weekday(year, month, day)))
|
||||
47
Task/Doomsday-rule/ALGOL-68/doomsday-rule.alg
Normal file
47
Task/Doomsday-rule/ALGOL-68/doomsday-rule.alg
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
BEGIN # find the day of the week of dates using John Conway's Doomsday rule #
|
||||
# returns the day of the week (Sunday = 0, Monday = 1,...) for the date #
|
||||
# specified by ccyy, mm and dd #
|
||||
PROC dow = ( INT ccyy, mm, dd )INT:
|
||||
BEGIN
|
||||
INT doomsday = ( # Tuesday # 2
|
||||
+ ( 5 * ( ccyy MOD 4 ) )
|
||||
+ ( 4 * ( ccyy MOD 100 ) )
|
||||
+ ( 6 * ( ccyy MOD 400 ) )
|
||||
)
|
||||
MOD 7;
|
||||
BOOL is leap year = ccyy MOD 4 = 0
|
||||
AND ( ccyy MOD 100 /= 0 OR ccyy MOD 400 = 0 );
|
||||
INT anchor day = CASE mm
|
||||
IN IF is leap year THEN 4 ELSE 3 FI
|
||||
, IF is leap year THEN 1 ELSE 7 FI
|
||||
, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5
|
||||
ESAC;
|
||||
( doomsday + ( dd - anchor day ) ) MOD 7
|
||||
END # dow # ;
|
||||
BEGIN # task test cases #
|
||||
# prints a test date and its day of the week #
|
||||
PROC test dow = ( INT ccyy, mm, dd )VOID:
|
||||
BEGIN
|
||||
[]CHAR digit = "0123456789"[ AT 0 ];
|
||||
[]STRING day name =
|
||||
[]STRING( "Sunday", "Monday", "Tuesday", "Wednesday"
|
||||
, "Thursday", "Friday", "Saturday"
|
||||
)[ AT 0 ];
|
||||
print( ( whole( ccyy, 0 )
|
||||
, "-", digit[ mm OVER 10 ], digit[ mm MOD 10 ]
|
||||
, "-", digit[ dd OVER 10 ], digit[ dd MOD 10 ]
|
||||
, ": ", day name[ dow( ccyy, mm, dd ) ]
|
||||
, newline
|
||||
)
|
||||
)
|
||||
END # test dow # ;
|
||||
test dow( 1800, 1, 6 );
|
||||
test dow( 1875, 3, 29 );
|
||||
test dow( 1915, 12, 7 );
|
||||
test dow( 1970, 12, 23 );
|
||||
test dow( 2043, 5, 14 );
|
||||
test dow( 2077, 2, 12 );
|
||||
test dow( 2101, 4, 2 );
|
||||
test dow( 2022, 6, 19 )
|
||||
END
|
||||
END
|
||||
43
Task/Doomsday-rule/ALGOL-W/doomsday-rule.alg
Normal file
43
Task/Doomsday-rule/ALGOL-W/doomsday-rule.alg
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
begin % find the day of the week of dates using John Conway's Doomsday rule %
|
||||
% returns the day of the week (Sunday = 0, Monday = 1,...) for the date %
|
||||
% specified by ccyy, mm and dd %
|
||||
integer procedure dow ( integer value ccyy, mm, dd ) ;
|
||||
begin
|
||||
integer doomsday, anchorDay;
|
||||
logical isLeapYear;
|
||||
doomsday := ( % Tuesday % 2
|
||||
+ ( 5 * ( ccyy rem 4 ) )
|
||||
+ ( 4 * ( ccyy rem 100 ) )
|
||||
+ ( 6 * ( ccyy rem 400 ) )
|
||||
)
|
||||
rem 7;
|
||||
isLeapYear := ccyy rem 4 = 0 and ( ccyy rem 100 not = 0 or ccyy rem 400 = 0 );
|
||||
anchorDay := case mm of ( if isLeapYear then 4 else 3
|
||||
, if isLeapYear then 1 else 7
|
||||
, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5
|
||||
);
|
||||
( doomsday + ( dd - anchorDay ) ) rem 7
|
||||
end dow ;
|
||||
begin % task test cases %
|
||||
% prints a test date and its day of the week %
|
||||
procedure testDow ( integer value ccyy, mm, dd ) ;
|
||||
write( i_w := 1, s_w := 0
|
||||
, ccyy
|
||||
, "-", mm div 10, mm rem 10
|
||||
, "-", dd div 10, dd rem 10
|
||||
, ": ", dayName( dow( ccyy, mm, dd ) )
|
||||
);
|
||||
string(9) array dayName ( 0 :: 6 );
|
||||
dayName( 0 ) := "Sunday"; dayName( 1 ) := "Monday"; dayName( 2 ) := "Tuesday";
|
||||
dayName( 3 ) := "Wednesday"; dayName( 4 ) := "Thursday"; dayName( 5 ) := "Friday";
|
||||
dayName( 6 ) := "Saturday";
|
||||
testDow( 1800, 1, 6 );
|
||||
testDow( 1875, 3, 29 );
|
||||
testDow( 1915, 12, 7 );
|
||||
testDow( 1970, 12, 23 );
|
||||
testDow( 2043, 5, 14 );
|
||||
testDow( 2077, 2, 12 );
|
||||
testDow( 2101, 4, 2 );
|
||||
testDow( 2022, 6, 19 )
|
||||
end
|
||||
end.
|
||||
13
Task/Doomsday-rule/APL/doomsday-rule.apl
Normal file
13
Task/Doomsday-rule/APL/doomsday-rule.apl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
weekday←{⎕IO←1
|
||||
days←'Sunday' 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday'
|
||||
leap←4 7∊⍨2⊥0=4 100 400∘|
|
||||
ld←4 7 1 4 2 6 4 1 5 3 7 5
|
||||
nd←3 7 7 4 2 6 4 1 5 3 7 5
|
||||
y m d←⍵
|
||||
c←⌊y÷100 ⋄ r←100|y
|
||||
s←⌊r÷12 ⋄ t←12|r
|
||||
can←7|2+5×4|c
|
||||
doom←7|s+t+can+⌊t÷4
|
||||
anchor←m⊃(1+leap y)⊃nd ld
|
||||
(1+7|7+doom+d-anchor)⊃days
|
||||
}
|
||||
28
Task/Doomsday-rule/AppleScript/doomsday-rule-1.applescript
Normal file
28
Task/Doomsday-rule/AppleScript/doomsday-rule-1.applescript
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
on dayOfWeek(yyyymmdd)
|
||||
tell yyyymmdd to set {y, m, d} to {word 1 as integer, word 2 as integer, word 3 as integer}
|
||||
set doomsdayForYear to (y + y div 4 - y div 100 + y div 400 + 2) -- (mod 7 further down anyway)
|
||||
if ((m < 3) and ((y mod 4 = 0) and (y mod 100 > 0) or (y mod 400 = 0))) then set m to m + 12
|
||||
set doomsdayInMonth to item m of {3, 28, 7, 4, 2, 6, 4, 8, 5, 10, 7, 12, 4, 29}
|
||||
|
||||
return item ((doomsdayForYear + d - doomsdayInMonth + 28) mod 7 + 1) of ¬
|
||||
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
|
||||
end dayOfWeek
|
||||
|
||||
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 dateStrings to {"1800-01-06", "1875-03-29", "1915-12-07", "1970-12-23", "2043-05-14", "2077-02-12", "2101-04-02"}
|
||||
set output to {}
|
||||
repeat with yyyymmdd in dateStrings
|
||||
set end of output to yyyymmdd & " --> " & dayOfWeek(yyyymmdd)
|
||||
end repeat
|
||||
return join(output, linefeed)
|
||||
end task
|
||||
|
||||
task()
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
"1800-01-06 --> Monday
|
||||
1875-03-29 --> Monday
|
||||
1915-12-07 --> Tuesday
|
||||
1970-12-23 --> Wednesday
|
||||
2043-05-14 --> Thursday
|
||||
2077-02-12 --> Friday
|
||||
2101-04-02 --> Saturday"
|
||||
15
Task/Doomsday-rule/BASIC/doomsday-rule.basic
Normal file
15
Task/Doomsday-rule/BASIC/doomsday-rule.basic
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
10 DIM D$(7): FOR I=1 TO 7: READ D$(I): NEXT I
|
||||
20 DIM D(12,1): FOR I=0 TO 1: FOR J=1 TO 12: READ D(J,I): NEXT J,I
|
||||
30 READ Y: IF Y=0 THEN END ELSE READ M,D
|
||||
40 PRINT USING "##/##/####: ";M;D;Y;
|
||||
50 C=Y\100: R=Y MOD 100
|
||||
60 S=R\12: T=R MOD 12
|
||||
70 A=(5*(C AND 3)+2) MOD 7
|
||||
80 B=(S+T+(T\4)+A) MOD 7
|
||||
90 PRINT D$((B+D-D(M,-(Y MOD 4=0 AND (Y MOD 100<>0 OR Y MOD 400=0)))+7) MOD 7+1)
|
||||
100 GOTO 30
|
||||
110 DATA Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday
|
||||
120 DATA 3,7,7,4,2,6,4,1,5,3,7,5
|
||||
130 DATA 4,1,7,4,2,6,4,1,5,3,7,5
|
||||
140 DATA 1800,1,6, 1875,3,29, 1915,12,7, 1970,12,23
|
||||
150 DATA 2043,5,14, 2077,2,12, 2101,4,2, 0
|
||||
37
Task/Doomsday-rule/BCPL/doomsday-rule.bcpl
Normal file
37
Task/Doomsday-rule/BCPL/doomsday-rule.bcpl
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
get "libhdr"
|
||||
|
||||
let dayname(n) =
|
||||
n = 0 -> "Sunday",
|
||||
n = 1 -> "Monday",
|
||||
n = 2 -> "Tuesday",
|
||||
n = 3 -> "Wednesday",
|
||||
n = 4 -> "Thursday",
|
||||
n = 5 -> "Friday",
|
||||
n = 6 -> "Saturday",
|
||||
dayname(n rem 7)
|
||||
|
||||
let leap(year) = year rem 4 = 0 & (year rem 100 ~= 0 | year rem 400 = 0)
|
||||
|
||||
let weekday(y, m, d) = valof
|
||||
$( let leapdoom = table 4,1,7,4,2,6,4,1,5,3,7,5
|
||||
let normdoom = table 3,7,7,4,2,6,4,1,5,3,7,5
|
||||
|
||||
let c = y / 100 and r = y rem 100
|
||||
let s = r / 12 and t = r rem 12
|
||||
|
||||
let an = (5 * (c rem 4) + 2) rem 7
|
||||
let doom = (s + t + (t/4) + an) rem 7
|
||||
|
||||
let anchor = (leap(y) -> leapdoom, normdoom)!(m-1)
|
||||
resultis (doom + d - anchor + 7) rem 7
|
||||
$)
|
||||
|
||||
let start() be
|
||||
$( writef("January 6, 1800 was on a %S.*N", dayname(weekday(1800, 1, 6)))
|
||||
writef("March 29, 1875 was on a %S.*N", dayname(weekday(1875, 3, 29)))
|
||||
writef("December 7, 1915 was on a %S.*N", dayname(weekday(1915, 12, 7)))
|
||||
writef("December 23, 1970 was on a %S.*N", dayname(weekday(1970, 12, 23)))
|
||||
writef("May 14, 2043 will be on a %S.*N", dayname(weekday(2043, 5, 14)))
|
||||
writef("February 12, 2077 will be on a %S.*N", dayname(weekday(2077, 2, 12)))
|
||||
writef("April 2, 2101 will be on a %S.*N", dayname(weekday(2101, 4, 2)))
|
||||
$)
|
||||
50
Task/Doomsday-rule/C++/doomsday-rule.cpp
Normal file
50
Task/Doomsday-rule/C++/doomsday-rule.cpp
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <iostream>
|
||||
#include <cstdint>
|
||||
|
||||
struct Date {
|
||||
std::uint16_t year;
|
||||
std::uint8_t month;
|
||||
std::uint8_t day;
|
||||
};
|
||||
|
||||
constexpr bool leap(int year) {
|
||||
return year%4==0 && (year%100!=0 || year%400==0);
|
||||
}
|
||||
|
||||
const std::string& weekday(const Date& date) {
|
||||
static const std::uint8_t leapdoom[] = {4,1,7,2,4,6,4,1,5,3,7,5};
|
||||
static const std::uint8_t normdoom[] = {3,7,7,4,2,6,4,1,5,3,7,5};
|
||||
|
||||
static const std::string days[] = {
|
||||
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
|
||||
"Friday", "Saturday"
|
||||
};
|
||||
|
||||
unsigned const c = date.year/100, r = date.year%100;
|
||||
unsigned const s = r/12, t = r%12;
|
||||
|
||||
unsigned const c_anchor = (5 * (c%4) + 2) % 7;
|
||||
unsigned const doom = (s + t + t/4 + c_anchor) % 7;
|
||||
unsigned const anchor = (leap(date.year) ? leapdoom : normdoom)[date.month-1];
|
||||
return days[(doom+date.day-anchor+7)%7];
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const std::string months[] = {"",
|
||||
"January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December"
|
||||
};
|
||||
|
||||
const Date dates[] = {
|
||||
{1800,1,6}, {1875,3,29}, {1915,12,7}, {1970,12,23}, {2043,5,14},
|
||||
{2077,2,12}, {2101,4,2}
|
||||
};
|
||||
|
||||
for (const Date& d : dates) {
|
||||
std::cout << months[d.month] << " " << (int)d.day << ", " << d.year;
|
||||
std::cout << (d.year > 2021 ? " will be " : " was ");
|
||||
std::cout << "on a " << weekday(d) << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
53
Task/Doomsday-rule/C/doomsday-rule.c
Normal file
53
Task/Doomsday-rule/C/doomsday-rule.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
} Date;
|
||||
|
||||
bool leap(uint16_t year) {
|
||||
return year%4==0 && (year%100!=0 || year%400==0);
|
||||
}
|
||||
|
||||
const char *weekday(Date date) {
|
||||
static const uint8_t leapdoom[] = {4,1,7,2,4,6,4,1,5,3,7,5};
|
||||
static const uint8_t normdoom[] = {3,7,7,4,2,6,4,1,5,3,7,5};
|
||||
static const char *days[] = {
|
||||
"Sunday", "Monday", "Tuesday", "Wednesday",
|
||||
"Thursday", "Friday", "Saturday"
|
||||
};
|
||||
|
||||
unsigned c = date.year/100, r = date.year%100;
|
||||
unsigned s = r/12, t = r%12;
|
||||
|
||||
unsigned c_anchor = (5 * (c%4) + 2) % 7;
|
||||
unsigned doom = (s + t + (t/4) + c_anchor) % 7;
|
||||
unsigned anchor = (leap(date.year) ? leapdoom : normdoom)[date.month-1];
|
||||
return days[(doom+date.day-anchor+7)%7];
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
const char *past = "was", *future = "will be";
|
||||
const char *months[] = { "",
|
||||
"January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December"
|
||||
};
|
||||
|
||||
const Date dates[] = {
|
||||
{1800,1,6}, {1875,3,29}, {1915,12,7}, {1970,12,23}, {2043,5,14},
|
||||
{2077,2,12}, {2101,4,2}
|
||||
};
|
||||
|
||||
int i;
|
||||
for (i=0; i < sizeof(dates)/sizeof(Date); i++) {
|
||||
printf("%s %d, %d %s on a %s.\n",
|
||||
months[dates[i].month], dates[i].day, dates[i].year,
|
||||
dates[i].year > 2021 ? future : past,
|
||||
weekday(dates[i]));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
47
Task/Doomsday-rule/CLU/doomsday-rule.clu
Normal file
47
Task/Doomsday-rule/CLU/doomsday-rule.clu
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
leap_year = proc (year: int) returns (bool)
|
||||
return(year//4=0 & (year//100=0 | year//400=0))
|
||||
end leap_year
|
||||
|
||||
weekday = proc (d: date) returns (string)
|
||||
own leapdoom: array[int] := array[int]$[4,1,7,2,4,6,4,1,5,3,7,5]
|
||||
own normdoom: array[int] := array[int]$[3,7,7,4,2,6,4,1,5,3,7,5]
|
||||
own days: array[string] := array[string]$[0:
|
||||
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
|
||||
"Friday", "Saturday"
|
||||
]
|
||||
|
||||
c: int := d.year/100
|
||||
r: int := d.year//100
|
||||
s: int := r/12
|
||||
t: int := r//12
|
||||
|
||||
c_anchor: int := (5 * (c//4) + 2) // 7
|
||||
doom: int := (s + t + t/4 + c_anchor) // 7
|
||||
anchor: int
|
||||
if leap_year(d.year)
|
||||
then anchor := leapdoom[d.month]
|
||||
else anchor := normdoom[d.month]
|
||||
end
|
||||
return(days[(doom+d.day-anchor+7)//7])
|
||||
end weekday
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
dates: array[date] := array[date]$
|
||||
[date$create( 1, 6,1800,0,0,0),
|
||||
date$create(29, 3,1875,0,0,0),
|
||||
date$create( 7,12,1915,0,0,0),
|
||||
date$create(23,12,1970,0,0,0),
|
||||
date$create(14, 5,2043,0,0,0),
|
||||
date$create(12, 2,2077,0,0,0),
|
||||
date$create( 2, 4,2101,0,0,0)]
|
||||
|
||||
for d: date in array[date]$elements(dates) do
|
||||
stream$puts(po, date$unparse_date(d))
|
||||
if d<now()
|
||||
then stream$puts(po, " was on a ")
|
||||
else stream$puts(po, " will be on a ")
|
||||
end
|
||||
stream$putl(po, weekday(d))
|
||||
end
|
||||
end start_up
|
||||
53
Task/Doomsday-rule/Cowgol/doomsday-rule.cowgol
Normal file
53
Task/Doomsday-rule/Cowgol/doomsday-rule.cowgol
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
include "cowgol.coh";
|
||||
|
||||
record Date is
|
||||
year: uint16;
|
||||
month: uint8;
|
||||
day: uint8;
|
||||
end record;
|
||||
|
||||
sub weekday(d: [Date]): (dayname: [uint8]) is
|
||||
var daynames: [uint8][] := {
|
||||
"Sunday", "Monday", "Tuesday", "Wednesday",
|
||||
"Thursday", "Friday", "Saturday"
|
||||
};
|
||||
|
||||
var leapdoom: uint8[] := {4,7,1,4,2,6,4,1,5,3,7,5};
|
||||
var normdoom: uint8[] := {3,7,7,4,2,6,4,1,5,3,7,5};
|
||||
|
||||
var c := d.year / 100;
|
||||
var r := d.year % 100;
|
||||
var s := r / 12;
|
||||
var t := r % 12;
|
||||
|
||||
var can := (5 * (c%4) + 2) % 7;
|
||||
var doom := (s + t + (t/4) + can) % 7;
|
||||
var anchor: int16;
|
||||
if d.year%4 == 0 and (d.year%100 != 0 or d.year%400 == 0) then
|
||||
anchor := leapdoom[d.month-1] as int16;
|
||||
else
|
||||
anchor := normdoom[d.month-1] as int16;
|
||||
end if;
|
||||
|
||||
var dayno := (doom as int16 + d.day as int16 - anchor + 7) % 7;
|
||||
dayname := daynames[dayno as @indexof daynames];
|
||||
end sub;
|
||||
|
||||
sub print_date(d: [Date]) is
|
||||
print_i8(d.month); print_char('/');
|
||||
print_i8(d.day); print_char('/');
|
||||
print_i16(d.year); print(": ");
|
||||
print(weekday(d));
|
||||
print_nl();
|
||||
end sub;
|
||||
|
||||
var dates: Date[] := {
|
||||
{1800,1,6}, {1875,3,29}, {1915,12,7}, {1970,12,23}, {2043,5,14},
|
||||
{2077,2,12}, {2101,4,2}
|
||||
};
|
||||
|
||||
var i: @indexof dates := 0;
|
||||
while i < @sizeof dates loop
|
||||
print_date(&dates[i]);
|
||||
i := i + 1;
|
||||
end loop;
|
||||
38
Task/Doomsday-rule/FOCAL/doomsday-rule.focal
Normal file
38
Task/Doomsday-rule/FOCAL/doomsday-rule.focal
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
01.10 S X(1)=3;S X(2)=7;S X(3)=7;S X(4)=4;S X(5)=2;S X(6)=6
|
||||
01.15 S X(7)=4;S X(8)=1;S X(9)=5;S X(10)=3;S X(11)=7;S X(12)=5
|
||||
01.20 S Y=1800;S M= 1;S D= 6;D 2;D 3
|
||||
01.25 S Y=1875;S M= 3;S D=29;D 2;D 3
|
||||
01.30 S Y=1915;S M=12;S D= 7;D 2;D 3
|
||||
01.35 S Y=1970;S M=12;S D=23;D 2;D 3
|
||||
01.40 S Y=2043;S M= 5;S D=14;D 2;D 3
|
||||
01.45 S Y=2077;S M= 2;S D=12;D 2;D 3
|
||||
01.50 S Y=2101;S M= 4;S D= 2;D 2;D 3
|
||||
01.55 Q
|
||||
|
||||
02.10 S C=FITR(Y/100);S R=Y-C*100
|
||||
02.15 S S=FITR(R/12);S T=R-S*12
|
||||
02.20 S A=5*(C-FITR(C/4)*4)+2;S A=A-FITR(A/7)*7
|
||||
02.25 S B=S+T+FITR(T/4)+A;S B=B-FITR(B/7)*7
|
||||
02.30 S E=X(M)
|
||||
02.35 I (Y-FITR(Y/400)*400)2.4,2.5,2.4
|
||||
02.40 I (Y-FITR(Y/100)*100)2.45,2.7,2.45
|
||||
02.45 I (Y-FITR(Y/4)*4)2.7,2.5,2.7
|
||||
02.50 I (M-1)2.55,2.6,2.55
|
||||
02.55 I (M-2)2.7,2.65,2.7
|
||||
02.60 S E=E+7
|
||||
02.65 S E=E-6
|
||||
02.70 S E=B+D-E+7
|
||||
02.75 S E=E-FITR(E/7)*7
|
||||
|
||||
03.10 T "M",%2,M," "
|
||||
03.20 T "D",%2,D," "
|
||||
03.30 T "Y",%4,Y," "
|
||||
03.40 T "DAY=";D 4;T !
|
||||
|
||||
04.10 I (E-6)4.2;T "SATURDAY";R
|
||||
04.20 I (E-5)4.3;T "FRIDAY";R
|
||||
04.30 I (E-4)4.4;T "THURSDAY";R
|
||||
04.40 I (E-3)4.5;T "WEDNESDAY";R
|
||||
04.50 I (E-2)4.6;T "TUESDAY";R
|
||||
04.60 I (E-1)4.7;T "MONDAY";R
|
||||
04.70 T "SUNDAY"
|
||||
33
Task/Doomsday-rule/Factor/doomsday-rule.factor
Normal file
33
Task/Doomsday-rule/Factor/doomsday-rule.factor
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
USING: accessors calendar calendar.english formatting
|
||||
generalizations kernel math math.order math.vectors sequences ;
|
||||
|
||||
: check-year ( year -- ) ! months and days checked by <date>
|
||||
1582 9999 between?
|
||||
[ "Year must be between 1582 and 9999." throw ] unless ;
|
||||
|
||||
: doomsday ( year -- n )
|
||||
{ 4 100 400 } [ mod ] with map { 5 4 6 } vdot 2 + 7 mod ;
|
||||
|
||||
: anchorday ( year month -- m )
|
||||
1 - swap leap-year? { 4 1 } { 3 7 } ?
|
||||
{ 7 4 2 6 4 1 5 3 7 5 } append nth ;
|
||||
|
||||
: weekday ( date -- str )
|
||||
[ year>> dup check-year doomsday ] [ day>> + ]
|
||||
[ dup year>> swap month>> anchorday - 7 + 7 mod ] tri
|
||||
day-names nth ;
|
||||
|
||||
: test ( date -- )
|
||||
[ "%B %d, %Y" strftime ]
|
||||
[ now before? "was" "will be" ? ]
|
||||
[ weekday ] tri
|
||||
"%s %s on a %s.\n" printf ;
|
||||
|
||||
1800 1 6
|
||||
1875 3 29
|
||||
1915 12 7
|
||||
1970 12 23
|
||||
2043 5 14
|
||||
2077 2 12
|
||||
2101 4 2
|
||||
[ <date> test ] 3 7 mnapply
|
||||
35
Task/Doomsday-rule/FreeBASIC/doomsday-rule.basic
Normal file
35
Task/Doomsday-rule/FreeBASIC/doomsday-rule.basic
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
dim shared as ubyte fdoom(0 to 1, 1 to 12) = {_
|
||||
{ 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 }, _
|
||||
{ 4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5 } } 'the first doomsday in
|
||||
'each month for common
|
||||
'and leap years
|
||||
|
||||
dim shared as string*10 days(0 to 6) = {"Sunday", "Monday", "Tuesday", "Wednesday",_
|
||||
"Thursday", "Friday", "Saturday"}
|
||||
|
||||
function doomsday(y as uinteger) as ubyte
|
||||
' John Conway's doomsday formula
|
||||
return (2 + 5*(y mod 4) + 4*(y mod 100) + 6*(y mod 400)) mod 7
|
||||
end function
|
||||
|
||||
function leap(y as uinteger) as ubyte
|
||||
'is it a leap year?
|
||||
'return 0 for common years, 1 for leap years
|
||||
if y mod 4 > 0 then return 0
|
||||
if y mod 100 = 0 and y mod 400 > 0 then return 0
|
||||
return 1
|
||||
end function
|
||||
|
||||
function get_day(y as uinteger, m as ubyte, d as ubyte) as string
|
||||
dim as ubyte c = doomsday(y), diff
|
||||
diff = (7 + d - fdoom( leap(y), m )) mod 7
|
||||
return days( (c+diff) mod 7 )
|
||||
end function
|
||||
|
||||
print get_day( 1800, 01, 06 )
|
||||
print get_day( 1875, 03, 29 )
|
||||
print get_day( 1915, 12, 07 )
|
||||
print get_day( 1970, 12, 23 )
|
||||
print get_day( 2043, 05, 14 )
|
||||
print get_day( 2077, 02, 12 )
|
||||
print get_day( 2101, 04, 02 )
|
||||
28
Task/Doomsday-rule/FutureBasic/doomsday-rule.basic
Normal file
28
Task/Doomsday-rule/FutureBasic/doomsday-rule.basic
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
local fn IsLeapYear( year as NSUInteger ) as BOOL
|
||||
BOOL result = YES
|
||||
if ( year mod 4 > 0 ) then result = NO : exit fn
|
||||
if ( year mod 100 = 0 ) and ( year mod 400 > 0 ) then result = NO : exit fn
|
||||
end fn = result
|
||||
|
||||
local fn DoomsdayWeekdayForDate( month as NSUInteger, day as NSUInteger, year as NSUInteger ) as CFStringRef
|
||||
CFArrayRef weekdayNames = @[@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday"]
|
||||
CFArrayRef normalYearDays = @[@4, @1, @7, @4, @2, @6, @4, @1, @5, @3, @7, @5]
|
||||
CFArrayRef leapYearDays = @[@3, @7, @7, @4, @2, @6, @4, @1, @5, @3, @7, @5]
|
||||
CFStringRef dayOfWeek = @""
|
||||
NSUInteger anchorday
|
||||
|
||||
NSUInteger doomsday = ( 2 + 5 * ( year mod 4 ) + 4 * ( year mod 100 ) + 6 * ( year mod 400) ) mod 7
|
||||
if ( fn IsLeapYear( year ) ) then anchorday = intVal( normalYearDays[month-1] ) else anchorday = intVal( leapYearDays[month-1] )
|
||||
NSUInteger weekday = ( doomsday + day - anchorday + 7 ) mod 7
|
||||
if ( weekday == 0 ) then dayOfWeek = @"Sunday" else dayOfWeek = weekdayNames[weekday]
|
||||
end fn = dayOfWeek
|
||||
|
||||
printf @"01-06-1800 : %@", fn DoomsdayWeekdayForDate( 1, 6, 1800 )
|
||||
printf @"03-29-1875 : %@", fn DoomsdayWeekdayForDate( 3, 29, 1875 )
|
||||
printf @"12-07-1915 : %@", fn DoomsdayWeekdayForDate( 12, 7, 1915 )
|
||||
printf @"12-23-1970 : %@", fn DoomsdayWeekdayForDate( 12, 23, 1970 )
|
||||
printf @"05-14-2043 : %@", fn DoomsdayWeekdayForDate( 5, 14, 2043 )
|
||||
printf @"02-12-2077 : %@", fn DoomsdayWeekdayForDate( 2, 12, 2077 )
|
||||
printf @"04-02-2101 : %@", fn DoomsdayWeekdayForDate( 4, 2, 2101 )
|
||||
|
||||
HandleEvents
|
||||
48
Task/Doomsday-rule/Go/doomsday-rule.go
Normal file
48
Task/Doomsday-rule/Go/doomsday-rule.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var days = []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
|
||||
|
||||
func anchorDay(y int) int {
|
||||
return (2 + 5*(y%4) + 4*(y%100) + 6*(y%400)) % 7
|
||||
}
|
||||
|
||||
func isLeapYear(y int) bool { return y%4 == 0 && (y%100 != 0 || y%400 == 0) }
|
||||
|
||||
var firstDaysCommon = []int{3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5}
|
||||
var firstDaysLeap = []int{4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5}
|
||||
|
||||
func main() {
|
||||
dates := []string{
|
||||
"1800-01-06",
|
||||
"1875-03-29",
|
||||
"1915-12-07",
|
||||
"1970-12-23",
|
||||
"2043-05-14",
|
||||
"2077-02-12",
|
||||
"2101-04-02",
|
||||
}
|
||||
|
||||
fmt.Println("Days of week given by Doomsday rule:")
|
||||
for _, date := range dates {
|
||||
y, _ := strconv.Atoi(date[0:4])
|
||||
m, _ := strconv.Atoi(date[5:7])
|
||||
m--
|
||||
d, _ := strconv.Atoi(date[8:10])
|
||||
a := anchorDay(y)
|
||||
f := firstDaysCommon[m]
|
||||
if isLeapYear(y) {
|
||||
f = firstDaysLeap[m]
|
||||
}
|
||||
w := d - f
|
||||
if w < 0 {
|
||||
w = 7 + w
|
||||
}
|
||||
dow := (a + w) % 7
|
||||
fmt.Printf("%s -> %s\n", date, days[dow])
|
||||
}
|
||||
}
|
||||
44
Task/Doomsday-rule/Haskell/doomsday-rule.hs
Normal file
44
Task/Doomsday-rule/Haskell/doomsday-rule.hs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import Text.Printf
|
||||
|
||||
data Date = Date {year :: Int, month :: Int, day :: Int}
|
||||
|
||||
instance Show Date where
|
||||
show Date {year = y, month = m, day = d} =
|
||||
printf "%4d-%02d-%02d" y m d
|
||||
|
||||
leap :: Int -> Bool
|
||||
leap year =
|
||||
year `mod` 4 == 0
|
||||
&& (year `mod` 100 /= 0 || year `mod` 400 == 0)
|
||||
|
||||
weekday :: Date -> Int
|
||||
weekday Date {year = y, month = m, day = d} =
|
||||
let doom = (s + t + (t `div` 4) + c_anchor) `mod` 7
|
||||
anchor = dooms !! pred m
|
||||
c_anchor = (5 * mod c 4 + 2) `mod` 7
|
||||
dooms =
|
||||
(if leap y then [4, 1] else [3, 7])
|
||||
<> [7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
(c, r) = y `divMod` 100
|
||||
(s, t) = r `divMod` 12
|
||||
in (doom + d - anchor + 7) `mod` 7
|
||||
|
||||
days :: [String]
|
||||
days = words "Sunday Monday Tuesday Wednesday Thursday Friday Saturday"
|
||||
|
||||
dates :: [Date]
|
||||
dates =
|
||||
[ Date {year = 1800, month = 1, day = 6},
|
||||
Date {year = 1875, month = 3, day = 29},
|
||||
Date {year = 1915, month = 12, day = 7},
|
||||
Date {year = 1970, month = 12, day = 23},
|
||||
Date {year = 2043, month = 5, day = 14},
|
||||
Date {year = 2077, month = 2, day = 12},
|
||||
Date {year = 2101, month = 4, day = 2}
|
||||
]
|
||||
|
||||
dateAndDay :: Date -> String
|
||||
dateAndDay d = printf "%s: %s" (show d) (days !! weekday d)
|
||||
|
||||
main :: IO ()
|
||||
main = putStr $ unlines $ map dateAndDay dates
|
||||
4
Task/Doomsday-rule/J/doomsday-rule-1.j
Normal file
4
Task/Doomsday-rule/J/doomsday-rule-1.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
1+7|1++/\.31 28 31 30 31 30 31 31 30 31 30 31
|
||||
3 7 7 4 2 6 4 1 5 3 7 5
|
||||
1+7|1++/\.31 29 31 30 31 30 31 31 30 31 30 31
|
||||
4 1 7 4 2 6 4 1 5 3 7 5
|
||||
7
Task/Doomsday-rule/J/doomsday-rule-2.j
Normal file
7
Task/Doomsday-rule/J/doomsday-rule-2.j
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
get_weekday=: {{ 'Y M D'=. y
|
||||
Y0=. todayno Y,1 1
|
||||
Y1=. todayno 1+Y,0 0
|
||||
aday=. M{_,1+7|1++/\.>.//./}.|:todate Y0+i.Y1-Y0
|
||||
dday=. 7|2 5 4 6+/ .*1,4 100 400|/Y
|
||||
'day',~;(7|D+dday-aday){;:'Sun Mon Tues Wednes Thurs Fri Satur'
|
||||
}}
|
||||
6
Task/Doomsday-rule/J/doomsday-rule-3.j
Normal file
6
Task/Doomsday-rule/J/doomsday-rule-3.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
get_weekday=: {{ 'Y M D'=. y
|
||||
leap=. 0~:/ .=4 100 400|/Y
|
||||
aday=. 1+7|(M*leap*3>M)+M{_ 2 6 6 3 1 5 3 0 4 2 6 4
|
||||
dday=. 7|2 5 4 6+/ .*1,4 100 400|/Y
|
||||
'day',~;(7|D+dday-aday){;:'Sun Mon Tues Wednes Thurs Fri Satur'
|
||||
}}
|
||||
14
Task/Doomsday-rule/J/doomsday-rule-4.j
Normal file
14
Task/Doomsday-rule/J/doomsday-rule-4.j
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
get_weekday 1800 1 6
|
||||
Monday
|
||||
get_weekday 1875 3 29
|
||||
Monday
|
||||
get_weekday 1915 12 7
|
||||
Tuesday
|
||||
get_weekday 1970 12 23
|
||||
Wednesday
|
||||
get_weekday 2043 5 14
|
||||
Thursday
|
||||
get_weekday 2077 2 12
|
||||
Friday
|
||||
get_weekday 2101 4 2
|
||||
Saturday
|
||||
56
Task/Doomsday-rule/Java/doomsday-rule.java
Normal file
56
Task/Doomsday-rule/Java/doomsday-rule.java
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
class Doom {
|
||||
public static void main(String[] args) {
|
||||
final Date[] dates = {
|
||||
new Date(1800,1,6),
|
||||
new Date(1875,3,29),
|
||||
new Date(1915,12,7),
|
||||
new Date(1970,12,23),
|
||||
new Date(2043,5,14),
|
||||
new Date(2077,2,12),
|
||||
new Date(2101,4,2)
|
||||
};
|
||||
|
||||
for (Date d : dates)
|
||||
System.out.println(
|
||||
String.format("%s: %s", d.format(), d.weekday()));
|
||||
}
|
||||
}
|
||||
|
||||
class Date {
|
||||
private int year, month, day;
|
||||
|
||||
private static final int[] leapdoom = {4,1,7,4,2,6,4,1,5,3,7,5};
|
||||
private static final int[] normdoom = {3,7,7,4,2,6,4,1,5,3,7,5};
|
||||
public static final String[] weekdays = {
|
||||
"Sunday", "Monday", "Tuesday", "Wednesday",
|
||||
"Thursday", "Friday", "Saturday"
|
||||
};
|
||||
|
||||
public Date(int year, int month, int day) {
|
||||
this.year = year;
|
||||
this.month = month;
|
||||
this.day = day;
|
||||
}
|
||||
|
||||
public boolean isLeapYear() {
|
||||
return year%4 == 0 && (year%100 != 0 || year%400 == 0);
|
||||
}
|
||||
|
||||
public String format() {
|
||||
return String.format("%02d/%02d/%04d", month, day, year);
|
||||
}
|
||||
|
||||
public String weekday() {
|
||||
final int c = year/100;
|
||||
final int r = year%100;
|
||||
final int s = r/12;
|
||||
final int t = r%12;
|
||||
|
||||
final int c_anchor = (5 * (c%4) + 2) % 7;
|
||||
final int doom = (s + t + t/4 + c_anchor) % 7;
|
||||
final int anchor =
|
||||
isLeapYear() ? leapdoom[month-1] : normdoom[month-1];
|
||||
|
||||
return weekdays[(doom + day - anchor + 7) % 7];
|
||||
}
|
||||
}
|
||||
39
Task/Doomsday-rule/Jq/doomsday-rule.jq
Normal file
39
Task/Doomsday-rule/Jq/doomsday-rule.jq
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
def weekdaynames: ["Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
||||
|
||||
# For months 1 through 12, the date of the first doomsday that month.
|
||||
def leapyear_firstdoomsdays: [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5];
|
||||
def nonleapyear_firstdoomsdays: [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5];
|
||||
|
||||
# get_weekday(year::Int; month::Int; day::Int)::String
|
||||
# Return the weekday of a given date in the past or future subject to 1582<=$year<=9999.
|
||||
# Uses Conway's doomsday rule (see also https://en.wikipedia.org/wiki/Doomsday_rule)
|
||||
|
||||
def assert(exp; msg):
|
||||
if env.JQ_ASSERT then
|
||||
(exp as $e | if $e then . else . as $in | "assertion violation: \(msg) => \($e)" | error end)
|
||||
else . end;
|
||||
|
||||
def get_weekday($year; $month; $day):
|
||||
# sanity checks
|
||||
assert(1582 <= $year and $year <= 9999; "Invalid year (\($year) - should be after 1581 and 4 digits)")
|
||||
| assert( 1 <= $month and $month <= 12; "Invalid month, should be between 1 and 12")
|
||||
| assert( 1 <= $day and $day <= 31; "Invalid day, should be between 1 and 31")
|
||||
|
||||
# Conway's doomsday algorithm
|
||||
| ((2 + 5 * ($year % 4) + 4 * ($year % 100) + 6 * ($year % 400)) % 7) as $doomsday
|
||||
# leap year determination:
|
||||
| (if (($year % 4 != 0) or ($year % 100 == 0 and $year % 400 != 0))
|
||||
then nonleapyear_firstdoomsdays[$month - 1]
|
||||
else leapyear_firstdoomsdays[$month - 1]
|
||||
end) as $anchorday
|
||||
| (($doomsday + $day - $anchorday + 7) % 7) as $weekday # IO==0
|
||||
| weekdaynames[$weekday] ;
|
||||
|
||||
("January 6, 1800 was on a " + get_weekday(1800; 1; 6)),
|
||||
("March 29, 1875 was on a " + get_weekday(1875; 3; 29)),
|
||||
("December 7, 1915 was on a " + get_weekday(1915; 12; 7)),
|
||||
("December 23, 1970 was on a " + get_weekday(1970; 12; 23)),
|
||||
("May 14, 2043 will be on a " + get_weekday(2043; 5; 14)),
|
||||
("February 12, 2077 will be on a "+ get_weekday(2077; 2; 12)),
|
||||
("April 2, 2101 will be on a " + get_weekday(2101; 4; 2)),
|
||||
("April 2, 21011 will be on a " + get_weekday(21011; 4; 2))
|
||||
39
Task/Doomsday-rule/Julia/doomsday-rule.julia
Normal file
39
Task/Doomsday-rule/Julia/doomsday-rule.julia
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
module DoomsdayRule
|
||||
export get_weekday
|
||||
|
||||
const weekdaynames = ["Sunday", "Monday","Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
||||
# For months 1 through 12, the date of the first doomsday that month.
|
||||
const leapyear_firstdoomsdays = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
const nonleapyear_firstdoomsdays = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
|
||||
"""
|
||||
get_weekday(year::Int, month::Int, day::Int)::String
|
||||
|
||||
Return the weekday of a given date in past or future.
|
||||
Uses Conway's doomsday rule (see also https://en.wikipedia.org/wiki/Doomsday_rule)
|
||||
"""
|
||||
function get_weekday(year::Int, month::Int, day::Int)::String
|
||||
# sanity checks
|
||||
@assert 1582 <= year <= 9999 "Invalid year (should be after 1581 and 4 digits)"
|
||||
@assert 1 <= month <= 12 "Invalid month, should be between 1 and 12"
|
||||
@assert 1 <= day <= 31 "Invalid day, should be between 1 and 31"
|
||||
|
||||
# Conway's doomsday algorithm
|
||||
doomsday = (2 + 5 * (year % 4) + 4 * (year % 100) + 6 * (year % 400)) % 7
|
||||
anchorday = (year % 4 != 0) || (year % 100 == 0 && year % 400 != 0) ? # leap year determination
|
||||
nonleapyear_firstdoomsdays[month] : leapyear_firstdoomsdays[month]
|
||||
weekday = (doomsday + day - anchorday + 7) % 7 + 1
|
||||
return weekdaynames[weekday]
|
||||
end
|
||||
|
||||
end # module
|
||||
|
||||
using .DoomsdayRule
|
||||
|
||||
println("January 6, 1800 was on a ", get_weekday(1800, 1, 6))
|
||||
println("March 29, 1875 was on a ", get_weekday(1875, 3, 29))
|
||||
println("December 7, 1915 was on a ", get_weekday(1915, 12, 7))
|
||||
println("December 23, 1970 was on a ", get_weekday(1970, 12, 23))
|
||||
println("May 14, 2043 will be on a ", get_weekday(2043, 5, 14))
|
||||
println("February 12, 2077 will be on a ", get_weekday(2077, 2, 12))
|
||||
println("April 2, 2101 will be on a ", get_weekday(2101, 4, 2))
|
||||
22
Task/Doomsday-rule/Nim/doomsday-rule.nim
Normal file
22
Task/Doomsday-rule/Nim/doomsday-rule.nim
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import strformat, times
|
||||
|
||||
const
|
||||
NormDoom = [1: 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
LeapDoom = [1: 4, 1, 7, 2, 4, 6, 4, 1, 5, 3, 7, 5]
|
||||
|
||||
proc weekday(year, month, day: int): WeekDay =
|
||||
let doom = (2 + 5 * (year mod 4) + 4 * (year mod 100) + 6 * (year mod 400)) mod 7
|
||||
let anchor = if year.isLeapYear: LeapDoom[month] else: NormDoom[month]
|
||||
let wd = (doom + day - anchor + 7) mod 7
|
||||
result = if wd == 0: dSun else: WeekDay(wd - 1)
|
||||
|
||||
const Dates = ["1800-01-06", "1875-03-29", "1915-12-07",
|
||||
"1970-12-23", "2043-05-14", "2077-02-12", "2101-04-02"]
|
||||
|
||||
for date in Dates:
|
||||
let dt = date.parse("yyyy-MM-dd")
|
||||
let wday = weekday(dt.year, ord(dt.month), dt.monthday)
|
||||
if wday != dt.weekday:
|
||||
echo &"For {date}, expected {dt.weekday}, found {wday}."
|
||||
else:
|
||||
echo date, " → ", wday
|
||||
82
Task/Doomsday-rule/PL-M/doomsday-rule.plm
Normal file
82
Task/Doomsday-rule/PL-M/doomsday-rule.plm
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
100H:
|
||||
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
|
||||
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
|
||||
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
|
||||
PRINT$CHAR: PROCEDURE (C); DECLARE C BYTE; CALL BDOS(2,C); END PRINT$CHAR;
|
||||
|
||||
PRINT$NUMBER: PROCEDURE (N);
|
||||
DECLARE S (6) BYTE INITIAL ('.....$');
|
||||
DECLARE (N, P) ADDRESS, C BASED P BYTE;
|
||||
P = .S(5);
|
||||
DIGIT:
|
||||
P = P-1;
|
||||
C = N MOD 10 + '0';
|
||||
N = N / 10;
|
||||
IF N >0 THEN GO TO DIGIT;
|
||||
CALL PRINT(P);
|
||||
END PRINT$NUMBER;
|
||||
|
||||
PRINT$DATE: PROCEDURE (YEAR, MONTH, DAY);
|
||||
DECLARE YEAR ADDRESS, (MONTH, DAY) BYTE;
|
||||
CALL PRINT$NUMBER(MONTH);
|
||||
CALL PRINT$CHAR('/');
|
||||
CALL PRINT$NUMBER(DAY);
|
||||
CALL PRINT$CHAR('/');
|
||||
CALL PRINT$NUMBER(YEAR);
|
||||
END PRINT$DATE;
|
||||
|
||||
DAY$NAME: PROCEDURE (N) ADDRESS;
|
||||
DECLARE N BYTE;
|
||||
DO CASE N;
|
||||
RETURN .'SUNDAY$';
|
||||
RETURN .'MONDAY$';
|
||||
RETURN .'TUESDAY$';
|
||||
RETURN .'WEDNESDAY$';
|
||||
RETURN .'THURSDAY$';
|
||||
RETURN .'FRIDAY$';
|
||||
RETURN .'SATURDAY$';
|
||||
END;
|
||||
END DAY$NAME;
|
||||
|
||||
LEAP$YEAR: PROCEDURE (YEAR) BYTE;
|
||||
DECLARE YEAR ADDRESS;
|
||||
RETURN (YEAR MOD 4 = 0) AND ((YEAR MOD 100 <> 0) OR (YEAR MOD 400 = 0));
|
||||
END LEAP$YEAR;
|
||||
|
||||
WEEK$DAY: PROCEDURE (YEAR, MONTH, DAY) BYTE;
|
||||
DECLARE LEAP$DOOM DATA (4,1,7,4,2,6,4,1,5,3,7,5);
|
||||
DECLARE NORM$DOOM DATA (3,7,7,4,2,6,4,1,5,3,7,5);
|
||||
DECLARE YEAR ADDRESS, (MONTH, DAY) BYTE;
|
||||
DECLARE (C, R, S, T, C$ANCHOR, DOOM$DAY, ANCHOR$DAY) BYTE;
|
||||
|
||||
C = YEAR / 100;
|
||||
R = YEAR MOD 100;
|
||||
S = R / 12;
|
||||
T = R MOD 12;
|
||||
C$ANCHOR = (5 * (C MOD 4) + 2) MOD 7;
|
||||
DOOM$DAY = (S + T + T/4 + C$ANCHOR) MOD 7;
|
||||
IF LEAP$YEAR(YEAR)
|
||||
THEN ANCHOR$DAY = LEAP$DOOM(MONTH-1);
|
||||
ELSE ANCHOR$DAY = NORM$DOOM(MONTH-1);
|
||||
|
||||
RETURN (DOOM$DAY + DAY - ANCHOR$DAY + 7) MOD 7;
|
||||
END WEEK$DAY;
|
||||
|
||||
FORMAT$OUT: PROCEDURE (YEAR, MONTH, DAY);
|
||||
DECLARE YEAR ADDRESS, (MONTH, DAY) BYTE;
|
||||
CALL PRINT$DATE(YEAR, MONTH, DAY);
|
||||
CALL PRINT(.': $');
|
||||
CALL PRINT(DAY$NAME(WEEK$DAY(YEAR, MONTH, DAY)));
|
||||
CALL PRINT(.(13,10,'$'));
|
||||
END FORMAT$OUT;
|
||||
|
||||
DECLARE YEARS (7) ADDRESS INITIAL (1800, 1875, 1915, 1970, 2043, 2077, 2101);
|
||||
DECLARE MONTHS (7) BYTE INITIAL ( 1, 3, 12, 12, 5, 2, 4);
|
||||
DECLARE DAYS (7) BYTE INITIAL ( 6, 29, 7, 23, 14, 12, 2);
|
||||
|
||||
DECLARE I BYTE;
|
||||
DO I=0 TO LAST(DAYS);
|
||||
CALL FORMAT$OUT(YEARS(I), MONTHS(I), DAYS(I));
|
||||
END;
|
||||
CALL EXIT;
|
||||
EOF
|
||||
21
Task/Doomsday-rule/Perl/doomsday-rule.pl
Normal file
21
Task/Doomsday-rule/Perl/doomsday-rule.pl
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# 20210602 Perl programming solution
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub dow ($) {
|
||||
my ($year, $month, $day) = split /-/;
|
||||
my @D = $year%($year%25?4:16) ? (3,7,7,4,2,6,4,1,5,3,7,5) : (4,1,7,2,4,6,4,1,5,3,7,5);
|
||||
my $c = int($year / 100);
|
||||
my $s = ($year % 100) / 12;
|
||||
my $t = ($year % 100) % 12;
|
||||
my $a = ( 5 * ($c % 4) + 2 ) % 7;
|
||||
my $b = ( $s + $t + int($t / 4) + $a ) % 7;
|
||||
|
||||
qw ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday ) [
|
||||
($b + $day - $D[$month - 1] + 7) % 7 ]
|
||||
}
|
||||
|
||||
for (qw( 1800-01-06 1875-03-29 1915-12-07 1970-12-23 2043-05-14 2077-02-12 2101-04-02 )) {
|
||||
print $_, " is a : ", dow $_, "\n";
|
||||
}
|
||||
39
Task/Doomsday-rule/Phix/doomsday-rule.phix
Normal file
39
Task/Doomsday-rule/Phix/doomsday-rule.phix
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
|
||||
<span style="color: #008080;">include</span> <span style="color: #000000;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">doomsday_rule</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">yr4</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">yr100</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">yr400</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">400</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">anchor_day</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">*</span><span style="color: #000000;">yr4</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">*</span><span style="color: #000000;">yr100</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">*</span><span style="color: #000000;">yr400</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">bool</span> <span style="color: #000000;">leap_year</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">yr4</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">yr100</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">yr400</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">anchors</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">leap_year</span><span style="color: #0000FF;">?{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #0000FF;">:{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">anchors</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">w</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">w</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">7</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">dow</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">anchor_day</span><span style="color: #0000FF;">+</span><span style="color: #000000;">w</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- return dow</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dow</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #000000;">7</span><span style="color: #0000FF;">:</span><span style="color: #000000;">dow</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- to agree with ISO 8601, and Phix.</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">dates</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"1800-01-06"</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #008000;">"1875-03-29"</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #008000;">"1915-12-07"</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #008000;">"1970-12-23"</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #008000;">"2043-05-14"</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #008000;">"2077-02-12"</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #008000;">"2101-04-02"</span><span style="color: #0000FF;">}</span>
|
||||
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dates</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">timedate</span> <span style="color: #000000;">dt</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">parse_date_string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dates</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],{</span><span style="color: #008000;">"YYYY-MM-DD"</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">dt</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">bid</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">day_of_week</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">drd</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">doomsday_rule</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">ds</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">format_timedate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dddd Mmmm D, YYYY"</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">bis</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">day_of_week</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bAsText</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">ok</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">bid</span><span style="color: #0000FF;">==</span><span style="color: #000000;">drd</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"(ok)"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"*** ERROR ***"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n%-30s (%d = %d ? %s)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ds</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bis</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bid</span><span style="color: #0000FF;">,</span><span style="color: #000000;">drd</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ok</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
29
Task/Doomsday-rule/Python/doomsday-rule.py
Normal file
29
Task/Doomsday-rule/Python/doomsday-rule.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from datetime import date
|
||||
from calendar import isleap
|
||||
|
||||
def weekday(d):
|
||||
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
|
||||
"Friday", "Saturday"]
|
||||
dooms = [
|
||||
[3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5],
|
||||
[4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
]
|
||||
|
||||
c = d.year // 100
|
||||
r = d.year % 100
|
||||
s = r // 12
|
||||
t = r % 12
|
||||
c_anchor = (5 * (c % 4) + 2) % 7
|
||||
doomsday = (s + t + (t // 4) + c_anchor) % 7
|
||||
anchorday = dooms[isleap(d.year)][d.month - 1]
|
||||
weekday = (doomsday + d.day - anchorday + 7) % 7
|
||||
return days[weekday]
|
||||
|
||||
dates = [date(*x) for x in
|
||||
[(1800, 1, 6), (1875, 3, 29), (1915, 12, 7), (1970, 12, 23),
|
||||
(2043, 5, 14), (2077, 2, 12), (2101, 4, 2)]
|
||||
]
|
||||
|
||||
for d in dates:
|
||||
tense = "was" if d < date.today() else "is" if d == date.today() else "will be"
|
||||
print("{} {} a {}".format(d.strftime("%B %d, %Y"), tense, weekday(d)))
|
||||
18
Task/Doomsday-rule/REXX/doomsday-rule.rexx
Normal file
18
Task/Doomsday-rule/REXX/doomsday-rule.rexx
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/*REXX program finds the day─of─week for a specified date using Conway's Doomsday rule. */
|
||||
parse arg $ /*obtain optional arguments from the CL*/
|
||||
if $='' | $="," then $= , /*Not specified? Then use the default.*/
|
||||
'01/06/1800 03/29/1875 12/07/1915 12/23/1970 05/14/2043 04/02/2077 04/02/2101'
|
||||
d= 'Sun Mon Tues Wednes Thurs Fri Satur' /*list of days of the week, sans "day".*/
|
||||
y.0= 3 7 7 4 2 6 4 1 5 3 7 5 /*doomsday dates for non-leapyear month*/
|
||||
y.1= 4 1 7 4 2 6 4 1 5 3 7 5 /* " " " leapyear " */
|
||||
|
||||
do j=1 for words($); datum= word($, j) /*process each of the dates. */
|
||||
parse var datum mm '/' dd "/" yy /*parse the date ──► mm dd yy */
|
||||
ly= leapyear(yy) /*get indication of a leapyear. */
|
||||
wd= (doomsday(yy)+dd-word(y.ly, mm) + 7) // 7 + 1 /*obtain a code for the weekday.*/
|
||||
say datum ' falls on ' word(d, wd)"day" /*display day-of-week for date. */
|
||||
end /*j*/
|
||||
exit 0 /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
doomsday: parse arg ?; return (2 + 5 * (?//4) + 4 * (?//100) + 6 * (?//400) ) // 7
|
||||
leapyear: arg #; ly= #//4==0; if ly==0 then return 0; return ((#//100\==0) | #//400==0)
|
||||
25
Task/Doomsday-rule/Raku/doomsday-rule.raku
Normal file
25
Task/Doomsday-rule/Raku/doomsday-rule.raku
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
my @dow = < Sunday Monday Tuesday Wednesday Thursday Friday Saturday >;
|
||||
|
||||
my %doomsday = False => [3,7,7,4,2,6,4,1,5,3,7,5], True => [4,1,7,4,2,6,4,1,5,3,7,5];
|
||||
|
||||
sub conway ($date) {
|
||||
my ($year, $month, $day) = $date.comb(/\d+/)».Int;
|
||||
my $is-leap = ($year %% 4) && (($year % 100) || ($year %% 400));
|
||||
|
||||
my $c = $year div 100;
|
||||
my $s = ($year % 100) div 12;
|
||||
my $t = ($year % 100) % 12;
|
||||
my $a = ( 5 * ($c % 4) + 2 ) % 7;
|
||||
my $b = ( $s + $t + ($t div 4) + $a ) % 7;
|
||||
|
||||
($b + $day - %doomsday{$is-leap}[$month - 1] + 7) % 7
|
||||
}
|
||||
|
||||
|
||||
for < 1800-01-06 1875-03-29 1915-12-07 1970-12-23 2043-05-14 2077-02-12 2101-04-02 >
|
||||
{
|
||||
say "Conway - $_ is a: ", @dow[.&conway];
|
||||
# Or, we could use the method built into the compiler...
|
||||
say "Builtin - $_ is a: ", @dow[Date.new($_).day-of-week];
|
||||
say '';
|
||||
}
|
||||
43
Task/Doomsday-rule/Rust/doomsday-rule.rust
Normal file
43
Task/Doomsday-rule/Rust/doomsday-rule.rust
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
fn day_of_week(year: u32, month: u32, day: u32) -> u32 {
|
||||
const LEAPYEAR_FIRSTDOOMSDAYS: [u32; 12] = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5];
|
||||
const NONLEAPYEAR_FIRSTDOOMSDAYS: [u32; 12] = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5];
|
||||
assert!(year > 1581 && year < 10000);
|
||||
assert!(month >= 1 && month <= 12);
|
||||
assert!(day >= 1 && day <= 31);
|
||||
let doomsday = (2 + 5 * (year % 4) + 4 * (year % 100) + 6 * (year % 400)) % 7;
|
||||
let anchorday = if year % 4 != 0 || (year % 100 == 0 && year % 400 != 0) {
|
||||
NONLEAPYEAR_FIRSTDOOMSDAYS[month as usize - 1]
|
||||
} else {
|
||||
LEAPYEAR_FIRSTDOOMSDAYS[month as usize - 1]
|
||||
};
|
||||
(doomsday + day + 7 - anchorday) % 7
|
||||
}
|
||||
|
||||
fn print_day_of_week(year: u32, month: u32, day: u32) {
|
||||
const DAY_NAMES: [&str; 7] = [
|
||||
"Sunday",
|
||||
"Monday",
|
||||
"Tuesday",
|
||||
"Wednesday",
|
||||
"Thursday",
|
||||
"Friday",
|
||||
"Saturday",
|
||||
];
|
||||
println!(
|
||||
"{:04}-{:02}-{:02}: {}",
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
DAY_NAMES[day_of_week(year, month, day) as usize]
|
||||
);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
print_day_of_week(1800, 1, 6);
|
||||
print_day_of_week(1875, 3, 29);
|
||||
print_day_of_week(1915, 12, 7);
|
||||
print_day_of_week(1970, 12, 23);
|
||||
print_day_of_week(2043, 5, 14);
|
||||
print_day_of_week(2077, 2, 12);
|
||||
print_day_of_week(2101, 4, 2);
|
||||
}
|
||||
45
Task/Doomsday-rule/V-(Vlang)/doomsday-rule.v
Normal file
45
Task/Doomsday-rule/V-(Vlang)/doomsday-rule.v
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
const
|
||||
(
|
||||
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
||||
first_days_common = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
first_days_leap = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
)
|
||||
|
||||
fn main() {
|
||||
dates := [
|
||||
"1800-01-06",
|
||||
"1875-03-29",
|
||||
"1915-12-07",
|
||||
"1970-12-23",
|
||||
"2043-05-14",
|
||||
"2077-02-12",
|
||||
"2101-04-02"
|
||||
]
|
||||
mut y, mut m, mut d, mut a, mut f, mut w, mut dow := 0,0,0,0,0,0,0
|
||||
println("Days of week given by Doomsday rule:")
|
||||
for date in dates {
|
||||
y = date[0..4].int()
|
||||
m = date[5..7].int()
|
||||
m--
|
||||
d = date[8..10].int()
|
||||
a = anchor_day(y)
|
||||
f = first_days_common[m]
|
||||
if is_leap_year(y) {
|
||||
f = first_days_leap[m]
|
||||
}
|
||||
w = d - f
|
||||
if w < 0 {
|
||||
w = 7 + w
|
||||
}
|
||||
dow = (a + w) % 7
|
||||
println('$date -> ${days[dow]}')
|
||||
}
|
||||
}
|
||||
|
||||
fn anchor_day(y int) int {
|
||||
return (2 + 5 *(y % 4) + 4 * (y % 100) + 6 *(y % 400)) % 7
|
||||
}
|
||||
|
||||
fn is_leap_year(y int) bool {
|
||||
return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)
|
||||
}
|
||||
38
Task/Doomsday-rule/Wren/doomsday-rule.wren
Normal file
38
Task/Doomsday-rule/Wren/doomsday-rule.wren
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import "/date" for Date
|
||||
|
||||
var days = ["Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"]
|
||||
|
||||
var anchorDay = Fn.new { |y| (2 + 5 * (y%4) + 4 *(y%100) + 6 * (y%400)) % 7 }
|
||||
|
||||
var isLeapYear = Fn.new { |y| y%4 == 0 && (y%100 != 0 || y%400 == 0) }
|
||||
|
||||
var firstDaysCommon = [3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
var firstDaysLeap = [4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]
|
||||
|
||||
var dates = [
|
||||
"1800-01-06",
|
||||
"1875-03-29",
|
||||
"1915-12-07",
|
||||
"1970-12-23",
|
||||
"2043-05-14",
|
||||
"2077-02-12",
|
||||
"2101-04-02"
|
||||
]
|
||||
|
||||
System.print("Days of week given by Doomsday rule:")
|
||||
for (date in dates) {
|
||||
var y = Num.fromString(date[0..3])
|
||||
var m = Num.fromString(date[5..6]) - 1
|
||||
var d = Num.fromString(date[8..9])
|
||||
var a = anchorDay.call(y)
|
||||
var w = d - (isLeapYear.call(y) ? firstDaysLeap[m] : firstDaysCommon[m])
|
||||
if (w < 0) w = 7 + w
|
||||
var dow = (a + w) % 7
|
||||
System.print("%(date) -> %(days[dow])")
|
||||
}
|
||||
|
||||
System.print("\nDays of week given by Date module:")
|
||||
for (date in dates) {
|
||||
var d = Date.parse(date, Date.isoDate)
|
||||
System.print("%(date) -> %(d.weekDay)")
|
||||
}
|
||||
43
Task/Doomsday-rule/XPL0/doomsday-rule.xpl0
Normal file
43
Task/Doomsday-rule/XPL0/doomsday-rule.xpl0
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
\Finds the day of the week of a date using John Conway's Doomsday rule.
|
||||
\Returns the day of the week (Sunday = 0, Monday = 1,...) for the date
|
||||
\ specified by CCYY, MM and DD.
|
||||
function DOW ( CCYY, MM, DD );
|
||||
integer CCYY, MM, DD;
|
||||
integer Doomsday, AnchorDay, LeapYear, Dooms;
|
||||
begin
|
||||
Doomsday := rem(( \Tuesday \2
|
||||
+ 5 * rem( CCYY/4 )
|
||||
+ 4 * rem( CCYY/100 )
|
||||
+ 6 * rem( CCYY/400 )
|
||||
) / 7);
|
||||
LeapYear := rem(CCYY/4) = 0 and ( rem(CCYY/100) # 0 or rem(CCYY/ 400) = 0 );
|
||||
Dooms := [[4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5],
|
||||
[3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5]];
|
||||
AnchorDay := Dooms(if LeapYear then 0 else 1, MM-1);
|
||||
return rem( ( Doomsday + ( DD - AnchorDay ) + 7 ) / 7);
|
||||
end; \DOW
|
||||
|
||||
\Prints a test date and its day of the week
|
||||
procedure TestDOW ( CCYY, MM, DD );
|
||||
integer CCYY, MM, DD;
|
||||
integer DayName;
|
||||
begin
|
||||
DayName := ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
|
||||
"Saturday"];
|
||||
IntOut(0, CCYY);
|
||||
ChOut(0, ^-); IntOut(0, MM/10); IntOut(0, rem(0));
|
||||
ChOut(0, ^-); IntOut(0, DD/10); IntOut(0, rem(0));
|
||||
Text(0, ": "); Text(0, DayName( DOW( CCYY, MM, DD ) ));
|
||||
CrLf(0);
|
||||
end;
|
||||
|
||||
begin \task test cases
|
||||
TestDOW( 1800, 1, 6 );
|
||||
TestDOW( 1875, 3, 29 );
|
||||
TestDOW( 1915, 12, 7 );
|
||||
TestDOW( 1970, 12, 23 );
|
||||
TestDOW( 2043, 5, 14 );
|
||||
TestDOW( 2077, 2, 12 );
|
||||
TestDOW( 2101, 4, 2 );
|
||||
TestDOW( 2022, 6, 19 )
|
||||
end
|
||||
44
Task/Doomsday-rule/Yabasic/doomsday-rule.basic
Normal file
44
Task/Doomsday-rule/Yabasic/doomsday-rule.basic
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
dim fdoom(1, 12)
|
||||
for x = 0 to arraysize(fdoom(),1)
|
||||
for y = 1 to arraysize(fdoom(),2)
|
||||
read fdoom(x, y)
|
||||
next y
|
||||
next x
|
||||
|
||||
dim days$(6)
|
||||
for x = 0 to arraysize(days$(),1)
|
||||
read days$(x)
|
||||
next x
|
||||
|
||||
sub doomsday(y)
|
||||
// John Conway's doomsday formula
|
||||
return mod((2 + 5*mod(y, 4) + 4*mod(y, 100) + 6*mod(y, 400)), 7)
|
||||
end sub
|
||||
|
||||
sub leap(y)
|
||||
//is it a leap year?
|
||||
//return 0 for common years, 1 for leap years
|
||||
if mod(y, 4) > 0 then return 0 : fi
|
||||
if mod(y, 100) = 0 and mod(y, 400) > 0 then return 0 : fi
|
||||
return 1
|
||||
end sub
|
||||
|
||||
sub get_day$(d, m, y)
|
||||
c = doomsday(y)
|
||||
diff = mod((7 + d - fdoom(leap(y), m)), 7)
|
||||
return days$(mod((c+diff), 7))
|
||||
end sub
|
||||
|
||||
print get_day$(06, 01, 1800)
|
||||
print get_day$(29, 03, 1875)
|
||||
print get_day$(07, 12, 1915)
|
||||
print get_day$(23, 12, 1970)
|
||||
print get_day$(14, 05, 2043)
|
||||
print get_day$(12, 02, 2077)
|
||||
print get_day$(02, 04, 2101)
|
||||
end
|
||||
|
||||
//the first doomsday in each month for common and leap years
|
||||
data 3, 7, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5
|
||||
data 4, 1, 7, 4, 2, 6, 4, 1, 5, 3, 7, 5
|
||||
data "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
|
||||
Loading…
Add table
Add a link
Reference in a new issue