Some solutions JordiB 26th March, 2009 07:33 (UTC)

I really don't have a lot of time, but I'll take a crack at it.

- How many seconds were there in the year 2007, and how many seconds were there in the year 2008? This is probably the obvious answer you're referring to, but I'm anxious to see what's wrong with it: 2007: 60 * 60 * 24 * 365 = 31 536 000 2008: 60 * 60 * 24 * 36b6/b = 31 622 400 (leap year) I think this should be right, unless you want to use the definition that every year has 365.2425 days, which would mean that both years have 31 556 952 seconds.

- In the dates from 1st January 1901 to 31st December 2000 how many Sundays were there in New York, London and Moscow? I actually misread this, so I just solved (I think) the Project Euler question that doesn't specify a timezone. I'm in the Netherlands, which is closest to London (1 hour time difference). I doubt if the timezones make any difference in the answer to this question, unless some of these cities/countries decided they should be should be in a different timezone somewhere in the 20th century. Or if they decided they wanted daylight savings or something. In short: I didn't research history and this algorithm is naive:

<?php

$sundays = 0; // number of Sundays on first of month // will contain first days (numeric) for the first of all months in the next year. $nextYear = array();

// Fill nextYear with days of 2001 for ($i = 0; $i < 12; $i++) { $month = $i+1; $nextYear$i = date('w', strtotime(“2001/$month/01”)); }

for($y = 2000; $y > 1900; $y—) { for ($m = 0; $m < 12; $m++) { if ($y%4==0 && $m > 1) { // if leap year and after February $day = $nextYear$m-2; } elseif (($y-1)%4==0 && $m < 2) { // if last year was leap, and before March $day = $nextYear$m-2; } else { $day = $nextYear$m-1; } $nextYear$m = $day % 7; // now actually this year if ($nextYear$m == 0) { $sundays++; } } } echo $sundays;

?>

The answer is 171, which isn't very surprising, since that is the number of days that you would expect if you just do 12*100/7.

- What was the year number for 1901 CE in Thailand? Like I said, I don't have a lot of time, so I didn't get around to figuring out how the Thai calender works.

PS sorry about how the code looks. Is there a way to format it better in the comments?

Some solutions JordiB 26th March, 2009 07:52 (UTC)
I can't find an edit button, so I'll just correct my mistake in a new reply. The leap year calculations were off by 1. It should have been ($y+1) and $y instead of $y and ($y-1). The answer (171) stays the same.
Some solutions Kirit Sælensminde 26th March, 2009 08:01 (UTC)
JordiB said

I really don't have a lot of time, but I'll take a crack at it. - How many seconds were there in the year 2007, and how many seconds were there in the year 2008? This is probably the obvious answer you're referring to, but I'm anxious to see what's wrong with it: 2007: 60 * 60 * 24 * 365 = 31 536 000 2008: 60 * 60 * 24 * 366 = 31 622 400 (leap year) I think this should be right, unless you want to use the definition that every year has 365.2425 days, which would mean that both years have 31 556 952 seconds.

The obvious answer to 2007 looks right, the one for 2008 is, rather surprisingly, not right. I won't give the game away just yet though :) except to say that the fractional days isn't the right thing to be looking for.

- In the dates from 1st January 1901 to 31st December 2000 how many Sundays were there in New York, London and Moscow? I actually misread this, so I just solved (I think) the Project Euler question that doesn't specify a timezone. I'm in the Netherlands, which is closest to London (1 hour time difference). I doubt if the timezones make any difference in the answer to this question, unless some of these cities/countries decided they should be should be in a different timezone somewhere in the 20th century. Or if they decided they wanted daylight savings or something. In short: I didn't research history and this algorithm is naive:

The answer is 171, which isn't very surprising, since that is the number of days that you would expect if you just do 12*100/7.

I'm not sure about that. The Project Euler one specifically wants to know how many “Sunday 1st”s there were, I'm only after the number of Sundays (I don't care which day of the month they fall on, only how many there were). Thinking about time zones is a red herring.

PS sorry about how the code looks. Is there a way to format it better in the comments?

Yeah, I need to write a new parser for this. Prefix each code line with four spaces should help, and you might find you also have to put whitespace just inside [ ] brackets to stop them from looking like links.


To join in the discussion you should register or log in
Some solutions JordiB 26th March, 2009 08:37 (UTC)

In that case I'm going to say 31 622 401 seconds in 2008, since apparently there was a leap second.

It should be easy to alter my algorithm to just look at what day January 1st of each year is and from that calculate whether there are 52 or 53 Sundays in that year. If it's a Sunday, or if it's a Saturday and a leap year, it should be 53, otherwise it should be 52. I can't program it now though.

I don't think timezones should matter. (Sun)days start earlier in Moscow than they do in New York, but so do new years. If a year starts on Sunday in Moscow, it also starts on Sunday in New York. It will just be like 15(?) hours later or something.

Some solutions JordiB 27th March, 2009 13:26 (UTC)

Matlab code:

function sundays = kirit()
sundays = 0;
day = 6; % Saturday 01-01-2000
for y=2000:-1:1901
    if day == 0 || day == 6 && leap(y)
        sundays = sundays + 53;
    else
        sundays = sundays + 52;
    end
    
    if leap(y-1)
        day = mod(day-2, 7);
    else
        day = mod(day-1, 7);
    end
end
function leap = leap(y)
leap = mod(y,4)==0 && mod(y,400)~=0;

The answer is 5217.

With respect to your last question: 1901 AD would have started in 1347 CE and on April 6 continued into 1348 CE.