Project Euler - Problem 10
Question
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Solution
$sum = 2;
for ($i = 2; $i < 2000000; $i++) {
if ($i % 2 != 1) {
continue;
}
$d = 3;
$x = sqrt($i);
while (($i % $d != 0) && $d < $x) {
$d += 2;
}
if ((($i % $d == 0 && $i != $d) * 1) == 0) {
$sum += $i;
}
}
echo $sum;Time taken to execute: 30.5757 second.
Project Euler - Problem 6
Question
The sum of the squares of the first ten natural numbers is,
1² + 2² + ... + 10² = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)² = 55² = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
Project Euler - Problem 1
Question
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Solution
$sum = 0;
for ($i = 0; $i < 1000; $i++) {
if (!($i % 3) || !($i % 5)) {
$sum += $i;
}
}
echo $sum;Time taken to execute: 0.0007 second.
Answer
233168