From e689e807da3a2f3323f18dd444370ecda297f7ec Mon Sep 17 00:00:00 2001 From: FuryFire Date: Fri, 9 Mar 2012 13:48:12 +0100 Subject: [PATCH] Solved ProjectEuler/019: php, ruby --- ProjectEuler/019/desc.yml | 25 +++++++++++++++++++++++++ ProjectEuler/019/solve.php | 38 ++++++++++++++++++++++++++++++++++++++ ProjectEuler/019/solve.rb | 9 +++++++++ 3 files changed, 72 insertions(+) create mode 100644 ProjectEuler/019/desc.yml create mode 100644 ProjectEuler/019/solve.php create mode 100644 ProjectEuler/019/solve.rb diff --git a/ProjectEuler/019/desc.yml b/ProjectEuler/019/desc.yml new file mode 100644 index 0000000..4debeec --- /dev/null +++ b/ProjectEuler/019/desc.yml @@ -0,0 +1,25 @@ +title: How many Sundays fell on the first of the month during the twentieth century? +url: http://projecteuler.net/problem=19 + +desc: | + You are given the following information, but you may prefer to do some research for yourself. + 1 Jan 1900 was a Monday. + Thirty days has September, + April, June and November. + All the rest have thirty-one, + Saving February alone, + Which has twenty-eight, rain or shine. + And on leap years, twenty-nine. + A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. + How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? + + +solution: Instead of using buildtin DateTime language features this use a homemade algorithm + +solutions: + solve.php: + desc: Basic solution + language: php + solve.rb: + desc: Basic solution + language: ruby diff --git a/ProjectEuler/019/solve.php b/ProjectEuler/019/solve.php new file mode 100644 index 0000000..38a1a0e --- /dev/null +++ b/ProjectEuler/019/solve.php @@ -0,0 +1,38 @@ + 31, + 2 => 28, + 3 => 31, + 4 => 30, + 5 => 31, + 6 => 30, + 7 => 31, + 8 => 31, + 9 => 30, + 10 => 31, + 11 => 30, + 12 => 31 +); +$day_cycle = 0; +for($year=1900; $year <= 2000; $year++) { + for($month=1; $month <= 12; $month++) { + + $cdates = $days_in_month[$month]; + if($month == 2) { + if($year % 4 == 0) { + $cdates = 29; + } + if($years % 100 == 0) { + $cdates = 28; + } + if($years % 400 == 0) { + $cdates = 29; + } + } + $day_cycle += $cdates; + if($day_cycle % 7 == 0) { + if($year >= 1901) {$sum++; } + } + } +} +echo $sum; \ No newline at end of file diff --git a/ProjectEuler/019/solve.rb b/ProjectEuler/019/solve.rb new file mode 100644 index 0000000..a280295 --- /dev/null +++ b/ProjectEuler/019/solve.rb @@ -0,0 +1,9 @@ +require "date" +sundays = 0 +(1901..2000).each do |year| + (1..12).each do |month| + dt = Date.new(year,month,1) + sundays += (dt.wday == 0) ? 1 : 0 + end +end +puts sundays \ No newline at end of file