Project Euler – Problem 23

Problem

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

Solution


					

Project Euler – Problem 22

Problem

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

Solution

$sum = 0;

for ($i = 1; $i < 10000; $i++) {
  if ($i == d(d($i)) && $i != d($i)) {
    $sum += $i;
  }
}

echo $sum;

function d($num) {
  $temp = 0;

  for ($i = 1; $i <= $num / 2; $i++) {
    if ($num % $i == 0) {
      $temp += $i;
    }
  }

  return $temp;
}

Project Euler – Problem 21

Problem

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where ab, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

Solution

$sum = 0;

for ($i = 1; $i < 10000; $i++) {
  if ($i == d(d($i)) && $i != d($i)) {
    $sum += $i;
  }
}

echo $sum;

function d($num) {
  $temp = 0;

  for ($i = 1; $i <= $num / 2; $i++) {
    if ($num % $i == 0) {
      $temp += $i;
    }
  }

  return $temp;
}

Project Euler – Problem 20

Problem

n! means n × (n − 1) × … × 3 × 2 × 1

For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

Solution

echo problem20(100);

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

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

  $arrDigit = str_split($temp);

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

  return $sum;
}

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;