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;
}

Project Euler – Problem 14

Problem

The following iterative sequence is defined for the set of positive integers:

nn/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

Solution

$longest = 0;
$j = 0;

for ($i = 1; $i < 1000000; $i++) {
  $temp = termOfProblem14($i);

  if ($longest < $temp) {
    $longest = $temp;
    $j = $i;
  }
}

echo $j;

function termOfProblem14($num) {
  $i = 1;

  while ($num > 1) {
    if ($num % 2) {
      $num = 3 * $num + 1;
    } else {
      $num /= 2;
    }

    $i++;
  }

  return $i;
}