日本語を見たときは意味がわからなかったけど、月の初め、つまり1901年から
2000年の終わりまでの間に 1日が日曜日の月が何回あるかって問題。
モジュール番と数え上げる版を書いてみた。まあ大学一年生でやりそうな問題かな。
#!/usr/bin/perl use strict; use warnings; use Date::Simple; print "Answer is ", p19(1901, 2000), "\n"; # Answer is 171 sub p19 { my ($start, $end) = @_; my $count = 0; for my $year ($start..$end) { for my $month (1..12) { my $date = Date::Simple->new($year, $month, 1); $count++ if $date->day_of_week == 0; } } return $count; } sub p19_2 { my ($start, $end) = @_; my @day = (31,28,31,30,31,30,31,31,30,31,30,31); my $count = 0; my $s = 365 % 7 + 1; for my $year ($start..$end) { for my $month (1..12) { $s += $day[$month - 1]; if ($month == 2){ $s += 1 if (($year % 4 == 0) || ($year % 400 == 0 && $year % 100 != 0)); } $s = $s % 7; $count++ if $s == 0; } } return $count; }