Jump to content

Project Euler 19

Wictorian

The problem:

Spoiler

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)?

 I have found out the answer is 171 online. But how is this possible? January either has 4 or 5 sundays, right?  Then the answer must be between 400-500, wrong?

Link to comment
Share on other sites

Link to post
Share on other sites

I think you missed the "first of the month" part of your question. You don't count just any Sunday. You only count Sundays if they happen to be on Jan. 1st, Feb. 1st, Mar 1st, … and so on.

 

As a rough estimate:

1901 – 2000 is 99 years

Each year has 12 months, or twelve 1st of the month

So that's a total of 99 x 12 = 1188 months

The probability that a first of the month is a Sunday is 1 / 7

1188 / 7 = 169.1, so 171 seems more likely than 400-500

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

43 minutes ago, Eigenvektor said:

I think you missed the "first of the month" part of your question. You don't count just any Sunday. You only count Sundays if they happen to be on Jan. 1st, Feb. 1st, Mar 1st, … and so on.

 

As a rough estimate:

1901 – 2000 is 99 years

Each year has 12 months, or twelve 1st of the month

So that's a total of 99 x 12 = 1188 months

The probability that a first of the month is a Sunday is 1 / 7

1188 / 7 = 169.1, so 171 seems more likely than 400-500

Ohh. I thought it was "the first month", thus, January. 

Also is it 99 years or a hundred years?

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Wictorian said:

Ohh. I thought it was "the first month", thus, January. 

Also is it 99 years or a hundred years?

Oops, mistake on my part, it's 100 years. So 100 x 12 / 7 = 171.4

 

1901 = 1st year 

1999 = 99th year

2000 = 100th year

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

yeah, 171... 

 

basic, ugly php code as proof

 

<?php

$date_inc = new DateTime('1901-01-01 12:00:00');
$date_finish = new DateTime('2000-12-31 12:00:00');
$oneday = new DateInterval('P1D');

$results = 0;
while ($date_inc <= $date_finish) {
		$day_number = intval($date_inc->format('j'));
		$is_sunday = intval($date_inc->format('w')); // 0 if sunday
		if (($is_sunday===0) && ($day_number==1)) { 
			$results++; 
			echo str_pad($results,4,' ',STR_PAD_LEFT).'.'.$date_inc->format('Y-m-d')."\n";
		}
		$date_inc->add($oneday);
}

?>

 

Outputs

 


   1.1901-09-01
   2.1901-12-01
   3.1902-06-01
   4.1903-02-01
   5.1903-03-01
   6.1903-11-01
   7.1904-05-01
   8.1905-01-01
   9.1905-10-01
  10.1906-04-01
  11.1906-07-01
  12.1907-09-01
  13.1907-12-01
  14.1908-03-01
  15.1908-11-01
  16.1909-08-01
  17.1910-05-01
  18.1911-01-01
  19.1911-10-01
  20.1912-09-01
  21.1912-12-01
  22.1913-06-01
  23.1914-02-01
  24.1914-03-01
  25.1914-11-01
  26.1915-08-01
  27.1916-10-01
  28.1917-04-01
  29.1917-07-01
  30.1918-09-01
  31.1918-12-01
  32.1919-06-01
  33.1920-02-01
  34.1920-08-01
  35.1921-05-01
  36.1922-01-01
  37.1922-10-01
  38.1923-04-01
  39.1923-07-01
  40.1924-06-01
  41.1925-02-01
  42.1925-03-01
  43.1925-11-01
  44.1926-08-01
  45.1927-05-01
  46.1928-01-01
  47.1928-04-01
  48.1928-07-01
  49.1929-09-01
  50.1929-12-01
  51.1930-06-01
  52.1931-02-01
  53.1931-03-01
  54.1931-11-01
  55.1932-05-01
  56.1933-01-01
  57.1933-10-01
  58.1934-04-01
  59.1934-07-01
  60.1935-09-01
  61.1935-12-01
  62.1936-03-01
  63.1936-11-01
  64.1937-08-01
  65.1938-05-01
  66.1939-01-01
  67.1939-10-01
  68.1940-09-01
  69.1940-12-01
  70.1941-06-01
  71.1942-02-01
  72.1942-03-01
  73.1942-11-01
  74.1943-08-01
  75.1944-10-01
  76.1945-04-01
  77.1945-07-01
  78.1946-09-01
  79.1946-12-01
  80.1947-06-01
  81.1948-02-01
  82.1948-08-01
  83.1949-05-01
  84.1950-01-01
  85.1950-10-01
  86.1951-04-01
  87.1951-07-01
  88.1952-06-01
  89.1953-02-01
  90.1953-03-01
  91.1953-11-01
  92.1954-08-01
  93.1955-05-01
  94.1956-01-01
  95.1956-04-01
  96.1956-07-01
  97.1957-09-01
  98.1957-12-01
  99.1958-06-01
 100.1959-02-01
 101.1959-03-01
 102.1959-11-01
 103.1960-05-01
 104.1961-01-01
 105.1961-10-01
 106.1962-04-01
 107.1962-07-01
 108.1963-09-01
 109.1963-12-01
 110.1964-03-01
 111.1964-11-01
 112.1965-08-01
 113.1966-05-01
 114.1967-01-01
 115.1967-10-01
 116.1968-09-01
 117.1968-12-01
 118.1969-06-01
 119.1970-02-01
 120.1970-03-01
 121.1970-11-01
 122.1971-08-01
 123.1972-10-01
 124.1973-04-01
 125.1973-07-01
 126.1974-09-01
 127.1974-12-01
 128.1975-06-01
 129.1976-02-01
 130.1976-08-01
 131.1977-05-01
 132.1978-01-01
 133.1978-10-01
 134.1979-04-01
 135.1979-07-01
 136.1980-06-01
 137.1981-02-01
 138.1981-03-01
 139.1981-11-01
 140.1982-08-01
 141.1983-05-01
 142.1984-01-01
 143.1984-04-01
 144.1984-07-01
 145.1985-09-01
 146.1985-12-01
 147.1986-06-01
 148.1987-02-01
 149.1987-03-01
 150.1987-11-01
 151.1988-05-01
 152.1989-01-01
 153.1989-10-01
 154.1990-04-01
 155.1990-07-01
 156.1991-09-01
 157.1991-12-01
 158.1992-03-01
 159.1992-11-01
 160.1993-08-01
 161.1994-05-01
 162.1995-01-01
 163.1995-10-01
 164.1996-09-01
 165.1996-12-01
 166.1997-06-01
 167.1998-02-01
 168.1998-03-01
 169.1998-11-01
 170.1999-08-01
 171.2000-10-01

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×