Home » Php » php – Project Euler #19 code seems right. What am I missing?

# php – Project Euler #19 code seems right. What am I missing?

Posted by: admin July 12, 2020 Leave a comment

Questions:

Problem 19:

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 thought that using PHP for this would be a breeze since it has so many built-in time and date functions. My code is really quite simple, so I’m having a hard time seeing what I’m doing that’s wrong.

My code:

``````<?php
echo "<pre>";
\$sunday_count = 0;
for( \$year = 1901; \$year <= 2000; \$year++ ) {
for( \$month = 1; \$month <= 12; \$month++ ) {
// Produces a date in format: 1/1/2000
\$date = \$month . "/1/" . \$year;
\$time = strtotime( \$date );
\$is_sunday = ( date( 'l', \$time ) == "Sunday" );
echo "\$date "
. ( \$is_sunday ? 'was a Sunday. ' : '' )
. "<br>";
if( \$is_sunday ) \$sunday_count++;
}
}
echo "Answer: \$sunday_count";
echo "</pre>";
?>
``````

The solution my code comes up with is 169, which is not correct. Any idea?

Edit 1

The solution is supposed to be 171.

Using Wolfram Alpha and my Windows clock, I’ve doubled checked several of the Sundays which my code reports. All of them check out OK.

So it seems my code is reporting valid and legitimate Sundays, but somehow it has missed two of them.

Edit 2

I made the following minor change to the formatting of the date in my code:

``````\$date = sprintf('%d-%02d-01', \$year, \$month); // formats yyyy-mm-dd
``````

I then used @MadaraUchiha’s code to generate an array containing the 171 correct dates.

After comparing his dates with mine, these are the two missed dates:

1901-09-01
1901-12-01

Edit 3

Codepad also shows that these dates are not Sundays (but they really should be).

And I am certain that the dates are correctly being interpreted as YYYY-MM-DD because one of the dates my code offers to the solution is 2000-10-01, which would only be a Sunday if 10 is the month, not the day.

Edit 4

So apparently if on a 32 bit system, Unix timestamps won’t work outside of the range:

``````Fri, 13 Dec 1901 20:45:54 GMT
``````

to

``````Tue, 19 Jan 2038 03:14:07 GMT
``````
How to&Answers:

The reason it might not work on some systems using timestamps is that the range of a Unix timestamp on 32-bit systems is from `Fri, 13 Dec 1901 20:45:54 GMT` to `Tue, 19 Jan 2038 03:14:07 GMT`, so you miss almost all months in the first year.

64-bit systems have larger integers which makes the range bigger (in PHP).

### Answer：

I get 171 with this (much shorter, and more readable) code, using DateTime objects:

``````header("Content-type: text/plain"); //Browser display

\$time = new DateTime("1901-01-01");
\$end = new DateTime("2000-12-31");

\$counter = 0;

while (!\$time->diff(\$end)->invert) { //\$time isn't greater than \$end
\$time->modify("+1 month");
if (\$time->format("l") == "Sunday") {
\$counter++;
echo \$time->format("Y-m-d") . " was a Sunday!\n";
}
}

echo "\nTotal number of Sundays: \$counter";
``````

When using DateTime objects, you’re treating dates as Dates and not as numbers or strings. This gives you a huge flexibility advantage over any other approaches.

### Answer：

The problem on this code is the line

`\$date = \$month . "/1/" . \$year;`

this should be

`\$date = "1/".\$month."/" . \$year;`

You mixed the places of months and days.