# Project Euler #19: Counting Sundays

# Project Euler #19: Counting Sundays

Blakebrown + 2 comments I was stuck on this question for a while! For people who need some help, look into Zeller's Congruence :)

westsiderz + 0 comments Thanks a lot for the information. It was really helpful.

Nayagam + 0 comments Thanks for the information.

nakuls + 2 comments Guys If you are using python, beware of using datetime module to solve this problem. Code will fail on test case 4th and beyond because datetime.date() function gives 'year is out of range error' on large values of year. and if year further exceeds 2^31 - 1, it gives 'python int too large to convert to C long' error.

rdma398 + 1 comment https://www.hackerrank.com/contests/projecteuler/challenges/euler019/submissions/code/3397697 check this this must work as the calender repeats every 400 years but it fails for some test cases why ?????

r_delic + 1 comment But it doesn't repeat every 400 years, it repeats every 2800 years.

Termii + 1 comment it does repeat every 400 Years. But it only does so since year 1900.

anthony_das + 0 comments [deleted]

Anachor + 0 comments [deleted]

SOKS33 + 1 comment OMGGGGGG

For Python (or not) people struggling with TC 3 & 4 ...

Use this test case :

1 4699 12 12 4710 1 1

You should end up with 18. I made 2 mistakes: - I was counting sundays for 2801 years instead of 2800 ... - I was inverting years after mod 2800, which completely broke things in some case where year1 was less than year2 but mod1 was more than mod2

henge + 0 comments Thanks mate! Your hint did it for me as well. In my case I had a type in an if-statement that only executes when the starting date was beyond Dec 1st...

thebongy + 1 comment To all those who are failing TC 3 and 4, there are several reasons why this could be happening: 1. Make sure both result is calculated with both dates inclusive.

Make sure you cycle through months correctly. I suspect both these cases start from a date in December, so remember to loop correctly to January (depends entirely on your method of solving the problem.)

Trim the outputs correctly (Remove redundant whitespaces).

If you use the floor() function in C++, remember to handle Double --> Int or reverse conversions properly.

If you are using Zeller's Congruence method, ensure that you avoid negative modulus.

There should be no invalid date test cases as such, but if there are, handle appropriately by avoiding the invalid date, but still checking dates in between.

Remember to swap the dates if the second date is before the first date. Read the test cases properly, and you will notice the cases when this can occur.

If you solve all these issues, all test cases should pass.

flat20 + 0 comments I was also not looping correctly to January or more precisely, I counted by error the sunday on the 1st of the month also in 13th non-existent month. So in particular loop from 31.12. 2292 to 1.1. 2293 returned 2 Sundays. Thanks for the tip, after the fix, it worked well :)

pedrov + 3 comments https://www.hackerrank.com/contests/projecteuler/challenges/euler019/submissions/code/1302949095

I can't pass test cases #2, #3, #4, they give wrong answer. My code is efficient and I can't seem to break it with the usual problems. I support big numbers, I invert dates, include the date from the test case, and for invalid dates I tried both advancing them to the next month and giving 0 as output. For example:

5 1988 3 25 1989 7 13 1924 6 6 1925 6 16 1000000000000 2 2 1000000001000 3 2 1925 6 16 1924 6 6 1905 1 1 1905 1 1

output:

2 2 1720 2 1

kilicars + 0 comments I was also getting WA for the same test cases and my solution was giving 1 and 1719 respectively for your following TCs:

1988 3 25 1989 7 13 1000000000000 2 2 1000000001000 3 2

After fixing this I got AC. I wanted to say this as it may help you. Besides I did not need to invert dates since y1 <= y2 is stated in the problem. Lastly, we cannot see other people's submissions as this is an ongoing contest.

munikatoch + 0 comments In 4th test case you wrote y2 less than y1 and question says y1<=y2<=y1+1000 so according to that output should be 0

avinashsp93 + 0 comments Your test cases saved my sleepless nights dude. Thanks :) if(D1 != 1): M1+=1 was the piece of logic that was missing in my code

evan_siroky + 3 comments Was having trouble with tc 3&4, not sure if my problem was related to problems parsing Feb 30 or issues related to when a end date was before a start date. But once I wrote some code to solve those things the test cases passed!

kennethjmyers + 1 comment Do you have any advice for how to handle these? I currently have my code return 0 if the end date is less than the start date or if a date is invalid (feb 30 or feb29 on a non leap year or any other bad month/day combination). But my code is still timing out on cases 3 and 4

evan_siroky + 1 comment My advice is to try to figure out why your code is timing out. Maybe you have some loop that isn't terminating.

kennethjmyers + 0 comments Thanks for the response I actually figured out what was wrong, just too slow

lasydler + 0 comments I also had a problem on tc 3&4 (runtime error). I solved it by "trimming" the input (removing extra spaces etc). If there was a problem with incorrect dates, I think it was corrected: I was unable to find one.

alex_reinking + 0 comments Swapping the start/end if out-of-order worked for me. A little underhanded when we don't get to see the test cases, but the spec

*does*allow for it. :P

l12391835 + 0 comments This is my Java solution.

import java.io.*; import java.util.*; public class Solution { private static int SUNDAY = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); while (t-- > 0) { // input long[][] dateArray = new long[2][3]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { dateArray[i][j] = in.nextLong(); } } // begin adjust(dateArray); // long ak = 0; while (true) { if (dateArray[0][2] == 1) { if (verify(dateArray[0][0], dateArray[0][1], dateArray[0][2])) { ak++; } } // set date dateArray[0][2] = 1; // month plus 1 dateArray[0][1]++; if (dateArray[0][1] > 12) { dateArray[0][1] = 1; dateArray[0][0]++; } if (overDate(dateArray)) { break; } } System.out.println(ak); } in.close(); } /** * check if dateArray[0] is later than dateArray[1] * @param dateArray * @return */ private static boolean overDate(long[][] dateArray) { if (dateArray[0][0] > dateArray[1][0]) { return true; } else if (dateArray[0][0] == dateArray[1][0]) { if (dateArray[0][1] > dateArray[1][1]) { return true; } else if (dateArray[0][1] == dateArray[1][1]) { if (dateArray[0][2] > dateArray[1][2]) return true; } } return false; } /** * let a[0] be earlier than a[1] * @param dateArray */ private static void adjust(long[][] dateArray) { if (overDate(dateArray)) { exchange(dateArray); } } /** * exchange two input date * @param dateArray */ private static void exchange(long[][] dateArray) { long[] tmp = new long[3]; tmp = dateArray[1]; dateArray[1] = dateArray[0]; dateArray[0] = tmp; } private static boolean verify(long year, long month, long day) { // w=(y + (y / 4) + (c / 4) - (2 * c) + ((26 * (month + 1)) / 10) + day - 1) % 7 // long sourceY = year; // long sourceM = month; if (month < 3) { month += 12; year -= 1; } long c = year / 100; long y = year % 100; int w = (int) ((y + (y / 4) + (c / 4) - (2 * c) + ((26 * (month + 1)) / 10) + day - 1) % 7); boolean t = w == SUNDAY; // if (t) { // System.out.println(sourceY + "-" + sourceM + "-" + day); // } return t; } }

hrushikeshvanga1 + 0 comments for those using zeller's rule

check this

for year 1000110

d = 10

**and c = 10001**check this carefully

mike006322 + 0 comments Lessons:

- (Python specific) The python datetime module cannot calculate weekday for large years. Test cases 4 and above will fail.
- The years and days of the week are exactly the same every 2800 days. You could use this to get around the datetime lookup limitation but be careful not to just mod every year by 2800. Instead take the difference of start and end, mod the start and then add the difference back to the first to get the new end year.
- Best: Zeller's Congruence gives a nice algorithm that returns the day of the week for any given date. Test the first of every month within the interval using Zeller's algorithm. This sidesteps the two points above.

filipbacic + 4 comments Hi,

I am solving this challenge in Python 3 and for TC 3 and TC 4 I always get "wrong answer". I tried every example from this discussion and I get correct answers for all of those. I even put code for resolving the wrong dates and stripped input. I really do not know what else to try.

Can you please help me with some examples of test cases that I could try?

eslamsamymeg + 0 comments me also using python 3 and facing the same problem for TC3 and TC4

eslamsamymeg + 1 comment found it , I used the same code in python 2 got all correct answer, try the same just change input() with raw_input() for python 2 , it doesn't make any sense to me but it worked :D

filipbacic + 0 comments Unfortunately, it did not work for me :( Still WA for TC 3 and TC4.

basarugur + 1 comment did you pay attention to the part where it says "the dates are

**both inclusive**"? maybe you are ignoring the start & end dates?filipbacic + 0 comments I include both start and end dates, but thank you for observation. I think there is some problem with formatting in those test cases or something like that. But also some more test cases for me to try would be good.

mahithasree + 1 comment Hey, I'm having the same issue. Tried every example from discussions sections and got correct answers but WA for TC 3 & 4. Did you figure out why?

filipbacic + 2 comments Hey, sorry for late answer, it quite demoralised me for solving Euler problems. I have never found an answer for this problem. Have you solved it?

mahithasree + 0 comments No, couldn't solve it!

flat20 + 0 comments Maybe try:

1

2292 12 31

2293 1 1

I had error in switching between years and had problems with TC2,3,4. But since you have problems only with TC3,4 it may be something else.

Sort 47 Discussions, By:

Please Login in order to post a comment