Page 1 of 2

[Challenge] Friday the 13th

Posted: Sun Jul 18, 2010 1:02 pm
by AbraCadaver
Calculate the number of Friday the 13th occurrences from the first day in 1900 through the end of 2010.

Re: [Challenge] Friday the 13th

Posted: Sun Jul 18, 2010 4:28 pm
by liljester

Code: Select all

<?php

$fri13 = 0;
for($year = 1900; $year <= 2010; $year++) {
	for($month = 1; $month <= 12; $month++) {
		if(date('l', mktime(0, 0, 0, $month, 13, $year)) == "Friday") {
			//print date("F Y",  mktime(0, 0, 0, $month, 13, $year)) ." has a friday the 13th.\n";
			$fri13++;
		}
	}
}
print"there are {$fri13} friday 13ths between 01-01-1900 and 31-12-2010.\n";
?>

Re: [Challenge] Friday the 13th

Posted: Sun Jul 18, 2010 5:12 pm
by AbraCadaver
You're missing 4 8O

Re: [Challenge] Friday the 13th

Posted: Sun Jul 18, 2010 5:40 pm
by liljester
bah no fair! mktime() only goes back to 1902.

Re: [Challenge] Friday the 13th

Posted: Sun Jul 18, 2010 6:44 pm
by liljester

Code: Select all

<?php
$fri13 = 0;

for($year = 1900; $year <= 2010; $year++) {
	for($month = 1; $month <= 12; $month++) {
		$date = date_create("{$year}-{$month}-13");
		if(date_format($date, "l") == "Friday") {
			//print date_format($date, "Y m") ." has a friday the 13th.\n";
			$fri13++;
		}
	}	
}
print"there are {$fri13} friday 13ths between 01-01-1900 and 31-12-2010.\n";
?>
fixed :)

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 2:45 am
by VladSun

Code: Select all

class Friday13Calculator
{

	private static $fridays13inLeapYearWithWeekOffsets = array
	(
		1	=> 2,
		2	=> 1,
		3	=> 2,
		4	=> 2,
		5	=> 1,
		6	=> 1,
		7	=> 3,
	);

	private static $fridays13inNonLeapYearWithWeekOffsets = array
	(
		1	=> 2,
		2	=> 2,
		3	=> 1,
		4	=> 3,
		5	=> 1,
		6	=> 1,
		7	=> 2,
	);


	public static function get($startYear, $endYear)
	{
		$numberOfFridays13 = 0;
		
		for ($year = $startYear; $year <= $endYear; $year++)
		{
			if (self::isLeapYear($year))
				$fridays13InYear = self::$fridays13inLeapYearWithWeekOffsets;
			else
				$fridays13InYear = self::$fridays13inNonLeapYearWithWeekOffsets;

			$numberOfFridays13 += $fridays13InYear[self::getWeekOffset($year)];
		}

		return $numberOfFridays13;
	}

	private static function isLeapYear($year)
	{
		if ($year % 4 != 0)
			return false;
		if ($year % 100 != 0)
			return true;
		if ($year % 400 != 0)
			return false;
		return true;
	}


	private static function getWeekOffset($year)
	{
		return date_format(date_create('01-01-'.$year), 'N');
	}

}

echo Friday13Calculator::get(1900, 2010);

:)

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 1:25 pm
by AbraCadaver
Nice work guys! Here's what I came up with:

Code: Select all

for($i=0, $start=new DateTime('1900-01-13'), $end=new DateTime('2010-12-31'); $start<=$end; $start->modify('13 of next month')) {
	if($start->format('N') == 5) {
		$i++;
		//$dates[] = $start->format('Y-m-d');  //if you need the dates
	}
}
echo $i;

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 2:36 pm
by VladSun
Nice one :)

I've tried to speed up the process. I think the date_* functions are computational expensive.

I've been thinking about how to calculate the next weekOffset without using date_* functions - I just wanted to use the previous calculated value. It should be simple :)

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 2:43 pm
by VladSun
Hm, I'm getting a warning with your code:
Warning: DateTime::modify() [datetime.modify]: Failed to parse time string (13 of next month) at position 0 (1): Unexpected character ...
EDIT: PHP v5.3.2-1ubuntu4.2

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 2:45 pm
by VladSun
Some speed benchmarks:
mine: 0.00524377822876 seconds
liljester: 0.0471768379211 seconds
AbraCadaver: Sorry, could not run it ...

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 2:50 pm
by AbraCadaver
VladSun wrote:Hm, I'm getting a warning with your code:
Warning: DateTime::modify() [datetime.modify]: Failed to parse time string (13 of next month) at position 0 (1): Unexpected character ...
EDIT: PHP v5.3.2-1ubuntu4.2
That's strange, runs great for me. Seems like they broke something between 5.2.4 and 5.3.2 :(

PHP 5.2.4-2ubuntu5.10 with Suhosin-Patch 0.9.6.2 (cli)

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 3:42 pm
by Weirdan

Code: Select all

echo 190;
Benchmark please :D

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 3:46 pm
by AbraCadaver
AbraCadaver wrote:You're missing 4 8O
Same for you Weirdan :D

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 3:52 pm
by Weirdan
AbraCadaver wrote:Same for you Weirdan :D
Sorry, can't reproduce, please provide proper bugreport :P

Re: [Challenge] Friday the 13th

Posted: Tue Jul 20, 2010 4:01 pm
by VladSun
2 and a half (Weirdan's code) people say it's 190 ... find your bug :P