mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 07:08:13 +01:00
meta: add tests/Maths, move ProjectEuler into Maths, code style updates
This commit is contained in:
parent
6c627c5589
commit
7da4a850fd
@ -10,7 +10,7 @@ function fastExponentiation(int $base, int $exponent)
|
||||
{
|
||||
if ($exponent == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if ($exponent == 1) {
|
||||
return $base;
|
||||
|
@ -1,30 +1,11 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Run script and test execution time with following script
|
||||
$executionTime = New ExecutionTime();
|
||||
print_r(fibonacciRecursive(10));
|
||||
/**
|
||||
* Fibonacci recursive
|
||||
*
|
||||
* @param int $num
|
||||
* @return int
|
||||
*/
|
||||
|
||||
class ExecutionTime
|
||||
{
|
||||
private $start_time = 0;
|
||||
private $end_time = 0;
|
||||
private $execution_time = 0;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->start_time = microtime(true);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->end_time = microtime(true);
|
||||
$this->execution_time = $this->end_time - $this->start_time;
|
||||
echo "Executed in $this->execution_time seconds\n";
|
||||
}
|
||||
}
|
||||
|
||||
function fibonacciRecursive(int $num)
|
||||
{
|
||||
/*
|
||||
@ -38,6 +19,10 @@ function fibonacciRecursive(int $num)
|
||||
return $fibonacciRecursive;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $num
|
||||
* @return int
|
||||
*/
|
||||
function recursive(int $num)
|
||||
{
|
||||
if ($num < 0) {
|
||||
@ -76,4 +61,4 @@ function fibonacciWithBinetFormula(int $num)
|
||||
}
|
||||
|
||||
return $fib_series;
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@
|
||||
* @param Decimal $number A decimal input
|
||||
* @return boolean whether the number is perfect square or not
|
||||
*/
|
||||
function is_perfect_square($number)
|
||||
function isPerfectSquare($number)
|
||||
{
|
||||
$root = (int) sqrt($number);
|
||||
return (($root * $root) === $number); // If number's square root is an integer then it's a perfect square else not
|
||||
|
@ -17,6 +17,9 @@
|
||||
* @link https://projecteuler.net/problem=1
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
function problem1a(): int
|
||||
{
|
||||
$maxNumber = 999; // below 1000
|
||||
@ -29,6 +32,9 @@ function problem1a(): int
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
function problem1b(): int
|
||||
{
|
||||
$maxNumber = 999; // below 1000
|
@ -20,6 +20,9 @@
|
||||
* @link https://projecteuler.net/problem=2
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
function problem2(): int
|
||||
{
|
||||
$maxNumber = 4000000;
|
@ -15,6 +15,10 @@
|
||||
*
|
||||
* @link https://projecteuler.net/problem=3
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
function problem3(): int
|
||||
{
|
||||
$n = 600851475143;
|
@ -7,6 +7,10 @@
|
||||
* 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
|
||||
* What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
function problem5(): int
|
||||
{
|
||||
$number = 20;
|
@ -26,6 +26,13 @@
|
||||
*
|
||||
* @link https://projecteuler.net/problem=6
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param int $maxNumber Range from 1 to <maxNumber>
|
||||
*
|
||||
* @return int Difference between the sum of the squares and
|
||||
* the square of the sum
|
||||
*/
|
||||
function problem6(int $maxNumber = 100): int
|
||||
{
|
||||
$sumOfSquares = 0;
|
@ -7,6 +7,10 @@
|
||||
* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
|
||||
* What is the 10 001st prime number?
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
function problem7(): int
|
||||
{
|
||||
$numberOfPrimes = 0;
|
@ -14,6 +14,10 @@
|
||||
*
|
||||
* There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
function problem9(): int
|
||||
{
|
||||
for ($i = 0; $i <= 300; $i++) {
|
25
Utils/ExecutionTime.php
Normal file
25
Utils/ExecutionTime.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Run script and test execution time with following script
|
||||
$executionTime = New ExecutionTime();
|
||||
print_r(fibonacciRecursive(10));
|
||||
*/
|
||||
class ExecutionTime
|
||||
{
|
||||
private $start_time = 0;
|
||||
private $end_time = 0;
|
||||
private $execution_time = 0;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->start_time = microtime(true);
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
$this->end_time = microtime(true);
|
||||
$this->execution_time = $this->end_time - $this->start_time;
|
||||
echo "Executed in $this->execution_time seconds\n";
|
||||
}
|
||||
}
|
@ -5,14 +5,15 @@ use function PHPUnit\Framework\assertFalse;
|
||||
use function PHPUnit\Framework\assertTrue;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
require_once __DIR__ . '/../Maths/Factorial.php';
|
||||
require_once __DIR__ . '/../Maths/CheckPrime.php';
|
||||
require_once __DIR__ . '/../Maths/AbsoluteMax.php';
|
||||
require_once __DIR__ . '/../Maths/AbsoluteMin.php';
|
||||
require_once __DIR__ . '/../Maths/PerfectSquare.php';
|
||||
require_once __DIR__ . '/../Maths/FastExponentiation.php';
|
||||
require_once __DIR__ . '/../Maths/Fibonacci.php';
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
require_once __DIR__ . '/../../Maths/AbsoluteMax.php';
|
||||
require_once __DIR__ . '/../../Maths/AbsoluteMin.php';
|
||||
require_once __DIR__ . '/../../Maths/CheckPrime.php';
|
||||
require_once __DIR__ . '/../../Maths/Factorial.php';
|
||||
require_once __DIR__ . '/../../Maths/FastExponentiation.php';
|
||||
require_once __DIR__ . '/../../Maths/Fibonacci.php';
|
||||
require_once __DIR__ . '/../../Maths/Fibonacci2.php';
|
||||
require_once __DIR__ . '/../../Maths/PerfectSquare.php';
|
||||
|
||||
class MathTest extends TestCase
|
||||
{
|
||||
@ -76,4 +77,13 @@ class MathTest extends TestCase
|
||||
assertEquals([0, 1, 1, 2, 3], fibonacciWithBinetFormula(5));
|
||||
assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], fibonacciWithBinetFormula(10));
|
||||
}
|
||||
|
||||
public function testFibonacciGenerator()
|
||||
{
|
||||
assertEquals([0, 1, 1, 2, 3], loop(5, fib()));
|
||||
assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], loop(10, fib()));
|
||||
|
||||
assertEquals([0, 1, 1, 2, 3], loop(5, loop()));
|
||||
assertEquals([0, 1, 1, 2, 3, 5, 8, 13, 21, 34], loop(10, fib()));
|
||||
}
|
||||
}
|
@ -2,14 +2,14 @@
|
||||
|
||||
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/Problem3.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem5.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem6.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem7.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem9.php';
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
require_once __DIR__ . '/../../ProjectEuler/Problem1.php';
|
||||
require_once __DIR__ . '/../../ProjectEuler/Problem2.php';
|
||||
require_once __DIR__ . '/../../ProjectEuler/Problem3.php';
|
||||
require_once __DIR__ . '/../../ProjectEuler/Problem5.php';
|
||||
require_once __DIR__ . '/../../ProjectEuler/Problem6.php';
|
||||
require_once __DIR__ . '/../../ProjectEuler/Problem7.php';
|
||||
require_once __DIR__ . '/../../ProjectEuler/Problem9.php';
|
||||
|
||||
class ProjectEulerTest extends TestCase
|
||||
{
|
Loading…
x
Reference in New Issue
Block a user