mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
Add project euler p1 and p2
This commit is contained in:
parent
8d382c9031
commit
570f58f168
@ -19,6 +19,8 @@
|
||||
* [Perfectsquare](https://github.com/TheAlgorithms/PHP/blob/master/Maths/PerfectSquare.php)
|
||||
|
||||
## Projecteuler
|
||||
* [Problem1](https://github.com/TheAlgorithms/PHP/blob/master/ProjectEuler/Problem1.php)
|
||||
* [Problem2](https://github.com/TheAlgorithms/PHP/blob/master/ProjectEuler/Problem2.php)
|
||||
* [Problem5](https://github.com/TheAlgorithms/PHP/blob/master/ProjectEuler/Problem5.php)
|
||||
* [Problem7](https://github.com/TheAlgorithms/PHP/blob/master/ProjectEuler/Problem7.php)
|
||||
* [Problem9](https://github.com/TheAlgorithms/PHP/blob/master/ProjectEuler/Problem9.php)
|
||||
|
42
ProjectEuler/Problem1.php
Normal file
42
ProjectEuler/Problem1.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Problem:
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*
|
||||
* Answer:
|
||||
*
|
||||
* 233168
|
||||
*
|
||||
*
|
||||
* @link https://projecteuler.net/problem=1
|
||||
*/
|
||||
|
||||
function problem1a(): int
|
||||
{
|
||||
$maxNumber = 999; // below 1000
|
||||
|
||||
$numbers = range(1, $maxNumber);
|
||||
|
||||
return array_reduce($numbers, function($carry, $number) {
|
||||
$shouldCarry = $number % 3 == 0 || $number % 5 == 0;
|
||||
return $carry += $shouldCarry ? $number : 0;
|
||||
});
|
||||
}
|
||||
|
||||
function problem1b(): int
|
||||
{
|
||||
$maxNumber = 999; // below 1000
|
||||
|
||||
$numbersMultByThree = range(3, $maxNumber, 3);
|
||||
$numbersMultByFive = range(5, $maxNumber, 5);
|
||||
|
||||
$numbers = array_merge($numbersMultByThree, $numbersMultByFive);
|
||||
|
||||
return array_sum(array_unique($numbers));
|
||||
}
|
36
ProjectEuler/Problem2.php
Normal file
36
ProjectEuler/Problem2.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Problem:
|
||||
*
|
||||
* Each new term in the Fibonacci sequence is generated by adding the previous
|
||||
* two terms. By starting with 1 and 2, the first 10 terms will be:
|
||||
*
|
||||
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
|
||||
*
|
||||
* By considering the terms in the Fibonacci sequence whose values do not
|
||||
* exceed four million, find the sum of the even-valued terms.
|
||||
*
|
||||
*
|
||||
* Answer:
|
||||
*
|
||||
* 4613732
|
||||
*
|
||||
*
|
||||
* @link https://projecteuler.net/problem=2
|
||||
*/
|
||||
|
||||
function problem2(): int
|
||||
{
|
||||
$maxNumber = 4000000;
|
||||
$currNumber = 1;
|
||||
$nextNumber = 2;
|
||||
$answer = 0;
|
||||
|
||||
while ($currNumber <= $maxNumber) {
|
||||
$answer += $currNumber % 2 == 0 ? $currNumber : 0;
|
||||
[$currNumber, $nextNumber] = [$nextNumber, $currNumber + $nextNumber];
|
||||
}
|
||||
|
||||
return $answer;
|
||||
}
|
@ -3,12 +3,25 @@
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once __DIR__.'/../vendor/autoload.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem1.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem2.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem5.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem7.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem9.php';
|
||||
|
||||
class ProjectEulerTest extends TestCase
|
||||
{
|
||||
public function testProblem1(): void
|
||||
{
|
||||
$this->assertSame(233168, problem1a());
|
||||
$this->assertSame(233168, problem1b());
|
||||
}
|
||||
|
||||
public function testProblem2(): void
|
||||
{
|
||||
$this->assertSame(4613732, problem2());
|
||||
}
|
||||
|
||||
public function testProblem5(): void
|
||||
{
|
||||
$this->assertSame(232792560, problem5());
|
||||
|
Loading…
x
Reference in New Issue
Block a user