Euler Solution 19

From ProgSoc Wiki

Jump to: navigation, search

Contents

Solutions for Problem 19

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

Prolog by SanguineV

Runtime: 18.188 ms

% 1 when the day of the week is a Sunday, otherwise 0.
sunday(N,1) :- N mod 7 =:= 0.
sunday(N,0).

% Given a year and a month, determine the next year, month and number of days to offset.
next(Year,Year,jan,feb,31).
next(Year,Year,feb,mar,29) :- Year mod 400 =:= 0.
next(Year,Year,feb,mar,28) :- Year mod 100 =:= 0.
next(Year,Year,feb,mar,29) :- Year mod 4   =:= 0.
next(Year,Year,feb,mar,28).
next(Year,Year,mar,apr,31).
next(Year,Year,apr,may,30).
next(Year,Year,may,jun,31).
next(Year,Year,jun,jul,30).
next(Year,Year,jul,aug,31).
next(Year,Year,aug,sep,31).
next(Year,Year,sep,oct,30).
next(Year,Year,oct,nov,31).
next(Year,Year,nov,dec,30).
next(Year,Year1,dec,jan,31) :- Year1 is Year + 1.

% Iterate through from a starting point until the year 2001 and find all
% the Sundays that are first day of the month.
iter(2001,D,M,0).
iter(Year,Days,Month,Total) :-
  next(Year,Y1,Month,M1,Off),
  sunday(Days,T1),
  D1 is Days + Off,
  iter(Y1,D1,M1,T2),
  Total is T1 + T2.

% Define the main routine to search for Sundays on the first for the 20th century
main :-
  iter(1901,2,jan,Total),
  print(Total).
main.

% Set the code to run "main" when executed
:- initialization(main).

Perhaps not the most blindingly fast solution, but a nice illustration of declarative programming.

Python by Althalus

Runtime: 1 ms

import time, math
start_time = time.time()

month= [
        31,#Jan
        28,#feb
        31,#mar
        30,#apr
        31,#may
        30,#jun
        31,#jul
        31,#aug
        30,#sep
        31,#oct
        30,#nov
        31,#dec
]
day=0 #0 means monday, 6 means sunday.
#1900 jan 1 is monday. We need 1901 jan first. 1900 is not a leap year.
for i in range(0,12):
        day+=month[i]
        day = day%7
print (day)
        

sundays=0
for i in range(1901,2001):
        #100 years in a century. First year is 00.
        for j in range(0,12):
                #twelve months in a year
                if j == 1:
                        if i%4 == 0:
                                if i%100==0 and i%400==0:
                                       day+=29
                                if i%100 != 0:
                                        day+=29 #leap year!
                else:
                        day+=month[j]
                day = day%7
                if day==6: sundays+=1
print(sundays)
run_time = time.time() - start_time
print (run_time)


Ruby by tomchristmas

Runtime: 22ms

day_count = 1
sunday_count = 0

puts "Month/year where the first day was a Sunday:\n"

1901.upto(2000){|year|     
    1.upto(12){|month|
      if day_count % 7 == 6 # the first day of 1901 was a Tuesday so let's start each week from there, and make Sunday our sixth day.
         sunday_count += 1 
         puts "#{month}/#{year}"
      end

      if month == 1
         day_count += 31
      elsif month == 2
         if (year % 4 == 0)
             day_count += 29
         else
             day_count += 28
         end
      elsif month == 3
         day_count += 31
      elsif month == 4
         day_count += 30
      elsif month == 5
         day_count += 31
      elsif month == 6
         day_count += 30
      elsif month == 7
         day_count += 31
      elsif month == 8
         day_count += 31
      elsif month == 9
         day_count += 30
      elsif month == 10
         day_count += 31
      elsif month == 11
         day_count += 30
      elsif month == 12
         day_count += 31
      end
    }
}

puts "Total number of first Sundays: #{sunday_count}"
Personal tools