Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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
like image 318
Joncom Avatar asked Nov 06 '12 05:11

Joncom


People also ask

What language is used in Project Euler?

I think Python is popular among Project Euler solvers because of its clean syntax. If you are a registered member of Project Euler, you should go to the Statistics page and take a look at the most used languages. You'll find Python, C/C++, Java and C# to be the most popular.

Does Project Euler show solutions?

Projecteuler-solutions provides merely a list of answers -- that is, it does not provide solutions or code for individual problems.

Where can I solve Project Euler problems?

You can now solve the classic Project Euler programming problems using the Rust language. Each of these problems comes with a user-friendly test suite. Here's the full Project Euler Rust GitHub repository. If you do not know Rust, and want to learn it, you can start with freeCodeCamp's interactive Rust course.


1 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).

like image 51
Emil Vikström Avatar answered Oct 24 '22 11:10

Emil Vikström