/********************************************************* http://projecteuler.net/problem=19 You are given the following information, but you may prefer to do some research for yourself. * 1 Jan 1900 was a Monday. * Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. * A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ *********************************************************/ main => go. go => euler19. % , euler19b. euler19 => Sum = 0, foreach(D in date2julian(1901,1,1)..date2julian(2000,12,31)) DD = julian2date(D), if DD[3] == 1, dow(DD[1],DD[2],DD[3]) == 0 then Sum := Sum+1 end end, print(Sum), nl. % Just one list comprehension. euler19b => Sum = sum([1 : D in date2julian(1901,1,1)..date2julian(2000,12,31), DD = julian2date(D), DD[3] == 1, dow(DD[1],DD[2],DD[3]) == 0]), print(Sum), nl. % % Converting Julian date <-> Gregorian date from: % http:%www.hermetic.ch/cal_stud/jdn.htm % date2julian(Y,M,D) = ( 1461 * ( Y + 4800 + ( M - 14 ) div 12 ) ) div 4 + ( 367 * ( M - 2 - 12 * ( ( M - 14 ) div 12 ) ) ) div 12 - ( 3 * ( ( Y + 4900 + ( M - 14 ) div 12 ) div 100 ) ) div 4 + D - 32075. % % Julian date to Gregorian date % julian2date(JD) = YMD => L = JD + 68569, N = ( 4 * L ) div 146097, L := L - ( 146097 * N + 3 ) div 4, I = ( 4000 * ( L + 1 ) ) div 1461001, L := L - ( 1461 * I ) div 4 + 31, J = ( 80 * L ) div 2447, D = L - ( 2447 * J ) div 80, L := J div 11, M = J + 2 - ( 12 * L ), Y = 100 * ( N - 49 ) + I + L, YMD := [Y, M, D]. % % Day of week, Sakamoto's method % http:%en.wikipedia.org/wiki/Weekday_determination#Sakamoto.27s_Method % dow(Y, M, D) = Dow => T = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4], YY = Y, if M < 3 then YY := YY - 1 end, Dow := (YY + YY div 4 - YY div 100 + YY div 400 + T[M] + D) mod 7.