Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created June 4, 2011 03:50
Show Gist options
  • Save johana-star/1007553 to your computer and use it in GitHub Desktop.
Save johana-star/1007553 to your computer and use it in GitHub Desktop.
Euler Project Problem #019
# Solution to Project Euler's nineteenth problem
# http://projecteuler.net/index.php?section=problems&id=19
# find number of Sundays on the first of the month between Jan 1901 and Dec 2000.
year = 1901
day_of_week = 2 # 0: Sunday, 6: Saturday, 1 Jan 1901 was a Tuesday
month = 0 # 0: January, # 11: December
months = [3, 0, 3, 2, 3, 2, 3, 3, 2, 3, 2, 3]
number_of_sundays = 0
until year == 2000 and month == 0 do
day_of_week += months[month]
month += 1
if month == 12 then month = 0; year += 1 end
if year % 4 == 0 and month == 1 then
if year % 100 != 0 then day_of_week += 1 end
if year % 400 == 0 then day_of_week += 1 end
end
day_of_week = day_of_week % 7
if day_of_week == 0 then number_of_sundays += 1; p "#{number_of_sundays}: #{year} #{month}" end
end
p number_of_sundays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment