Project Euler – Problem 19

Problem

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

Solution

$count = 0;

for ($year = 1901; $year <= 2000; $year++) {
  for ($month = 1; $month <= 12; $month++) {
    $day = date('w', mktime(0, 0, 0, $month, 1, $year));

    if ($day == 0) {
      $count++;
    }
  }
}

echo $count;

Project Euler – Problem 18

Problem

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.

3
7 4
2 4 6
8 5 9 3

That is, 3 + 7 + 4 + 9 = 23.

Find the maximum total from top to bottom of the triangle below:

75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23

NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)

Solution

$triangle = '75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23';

$arrTemp1 = explode("\n", $triangle);
$arrTemp2 = array();
$arrTemp3 = array();

foreach ($arrTemp1 as $arrTemp) {
  $arrTemp2[] = explode(" ", $arrTemp);
}

for ($i = count($arrTemp2) - 2; $i >= 0; $i--) {
  for ($j = 0; $j < count($arrTemp2[$i]); $j++) {
    $arrTemp2[$i][$j] = ($arrTemp2[$i + 1][$j] > $arrTemp2[$i + 1][$j + 1] ? $arrTemp2[$i][$j] + $arrTemp2[$i + 1][$j] : $arrTemp2[$i][$j] + $arrTemp2[$i + 1][$j + 1]);
    if ($i == 0 && $j == 0) {
      echo $arrTemp2[0][0];
    }
  }
  unset($arrTemp2[$i + 1]);
}

Project Euler – Problem 17

Problem

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.

Solution

$countChar = 0;

for ($i = 1; $i <= 1000; $i++) {
  $tempChar = strlen(str_replace(' ', '', readable($i)));
  $countChar += $tempChar;
}

echo $countChar;

function readable($num) {
  $dict = array(
    0 => 'Zero',
    1 => 'One',
    2 => 'Two',
    3 => 'Three',
    4 => 'Four',
    5 => 'Five',
    6 => 'Six',
    7 => 'Seven',
    8 => 'Eight',
    9 => 'Nine',
    10 => 'Ten',
    11 => 'Eleven',
    12 => 'Twelve',
    13 => 'Thirteen',
    14 => 'Fourteen',
    15 => 'Fifteen',
    16 => 'Sixteen',
    17 => 'Seventeen',
    18 => 'Eighteen',
    19 => 'Nineteen',
    20 => 'Twenty',
    30 => 'Thirty',
    40 => 'Forty',
    50 => 'Fifty',
    60 => 'Sixty',
    70 => 'Seventy',
    80 => 'Eighty',
    90 => 'Ninety',
    100 => 'Hundred',
  );

  $readable = array();

  if ($num == 1000) {
    $num = 0;
    $readble[] = 'One Thousand';
  }

  if ($num >= 100) {
    $tempHund = floor($num / 100);
    $num %= 100;
    $readble[] = $dict[$tempHund];
    $readble[] = $dict[100];
    if ($num > 0) {
      $readble[] = 'And';
    }
  }

  if ($num >= 20) {
    $tempTen = floor($num / 10) * 10;
    $num %= 10;
    $readble[] = $dict[$tempTen];
  }

  if ($num > 0 && $num < 20) {
    $readble[] = $dict[$num];
  }

  return implode(' ', $readble);
}

Project Euler – Problem 16

Problem

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?

Solution

echo problem16(1000);

function problem16($num) {
  $sum = 0;
  $temp = 1;

  for ($i = 0; $i < $num; $i++) {
    $temp = bcmul($temp, 2);
  }

  $arrDigit = str_split($temp);

  foreach ($arrDigit as $digit) {
    $sum += $digit;
  }

  return $sum;
}

Project Euler – Problem 15

Problem

Starting in the top left corner of a 2×2 grid, there are 6 routes (without backtracking) to the bottom right corner.

How many routes are there through a 20×20 grid?

Solution

$numOfGrid = 20;

echo problem15(2 * $numOfGrid) / problem15($numOfGrid) / problem15($numOfGrid);

function problem15($num) {
  $val = 1;

  for ($i = 2; $i <= $num; $i++) {
    $val *= $i;
  }

  return $val;
}