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 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 12

Problem

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

Solution


					

Project Euler – Problem 3

Problem

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Solution

$num = 600851475143;

for ($i = floor(sqrt($num)); $i >= 2; $i--) {
  if ($num % $i == 0 && isPrime($i)) {
    echo $i;
    break;
  }
}

function isPrime($num) {
  $isPrime = TRUE;

  for ($i = 3; $i < $num; $i += 2) {
    if ($num % $i == 0) {
      $isPrime = FALSE;
    }
  }

  return $isPrime;
}