In the first two rows we remember the day-count of each month. First row is normal years, second row leap years. Then we just enumerate through all days and test if its a Sunday and the first of month.