}
} ;
-HELP: zeller-congruence
+HELP: (day-of-week)
{ $values { "year" integer } { "month" integer } { "day" integer } { "n" integer } }
-{ $description "An implementation of an algorithm that computes the day of the week given a date. Days are indexed starting from Sunday, which is index 0." }
+{ $description "An implementation of the Zeller's congruence algorithm that computes the day of the week given a date. Days are indexed starting from Sunday, which is index 0." }
{ $notes "User code should use the " { $link day-of-week } " word, which takes a " { $snippet "timestamp" } " instead of integers." } ;
HELP: days-in-year
: same-day? ( ts1 ts2 -- ? )
[ >gmt slots{ year month day } ] same? ;
-: zeller-congruence ( year month day -- n )
+: (day-of-week) ( year month day -- n )
! Zeller Congruence
! http://web.textfiles.com/computers/formulas.txt
! good for any date since October 15, 1582
] dip 1 + + 7 mod ;
: day-of-week ( timestamp -- n )
- >date< zeller-congruence ;
+ >date< (day-of-week) ;
: (week-number) ( timestamp -- [0,53] )
[ day-of-year ] [ day-of-week [ 7 ] when-zero ] bi - 10 + 7 /i ;
day-abbreviations2 " " join print ;
: days. ( year month -- )
- [ 1 zeller-congruence dup [ " " write ] times ]
+ [ 1 (day-of-week) dup [ " " write ] times ]
[ (days-in-month) ] 2bi [1,b] [
[ day. ] [ + 7 mod zero? [ nl ] [ bl ] if ] bi
] with each nl ;
! --------
! Use Zeller congruence, which is implemented in the "calendar" module
-! already, as "zeller-congruence ( year month day -- n )" where n is
+! already, as "(day-of-week) ( year month day -- n )" where n is
! the day of the week (Sunday is 0).
: euler019 ( -- answer )
1901 2000 [a,b] [
- 12 [1,b] [ 1 zeller-congruence ] with map
+ 12 [1,b] [ 1 (day-of-week) ] with map
] map concat [ 0 = ] count ;
! [ euler019 ] 100 ave-time