Project Euler Problem #19

February 8, 2010

Read the details of the problem here

Summary

How many Sundays fell on the first of the month during the twentieth century?

Solution

If you’ve got it, flaunt it. Here’s a brute-force solution that simply uses the Java built-in calendar classes for a very quickly derived answer.

def (answer, cal)  = [ 0, new GregorianCalendar() ]

1901.upto(2000) { y ->
    12.times { m ->
        cal.set(y, m , 1)
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) answer++
    }
}

Runs in 0.57 seconds. Seems slow for what it’s doing but that’s Groovy for you…

Conclusion

Groovy didn’t make a lot of impact here, apart from keeping the syntax a little cleaner than Java would have been.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: