mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
Add Project Euler problem 3
This commit is contained in:
parent
e0e0eac014
commit
4b41b472e0
@ -21,6 +21,7 @@
|
||||
## Projecteuler
|
||||
* [Problem1](./ProjectEuler/Problem1.php)
|
||||
* [Problem2](./ProjectEuler/Problem2.php)
|
||||
* [Problem3](./ProjectEuler/Problem3.php)
|
||||
* [Problem5](./ProjectEuler/Problem5.php)
|
||||
* [Problem7](./ProjectEuler/Problem7.php)
|
||||
* [Problem9](./ProjectEuler/Problem9.php)
|
||||
|
30
ProjectEuler/Problem3.php
Normal file
30
ProjectEuler/Problem3.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Problem:
|
||||
*
|
||||
* The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
*
|
||||
* What is the largest prime factor of the number 600851475143?
|
||||
*
|
||||
*
|
||||
* Answer:
|
||||
*
|
||||
* 6857
|
||||
*
|
||||
*
|
||||
* @link https://projecteuler.net/problem=3
|
||||
*/
|
||||
function problem3(): int
|
||||
{
|
||||
$n = 600851475143;
|
||||
$i = 2;
|
||||
while ($i * $i < $n) {
|
||||
while ($n % $i == 0) {
|
||||
$n /= $i;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
|
||||
return $n;
|
||||
}
|
@ -5,6 +5,7 @@ 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/Problem7.php';
|
||||
require_once __DIR__.'/../ProjectEuler/Problem9.php';
|
||||
@ -22,6 +23,11 @@ class ProjectEulerTest extends TestCase
|
||||
$this->assertSame(4613732, problem2());
|
||||
}
|
||||
|
||||
public function testProblem3(): void
|
||||
{
|
||||
$this->assertSame(6857, problem3());
|
||||
}
|
||||
|
||||
public function testProblem5(): void
|
||||
{
|
||||
$this->assertSame(232792560, problem5());
|
||||
|
Loading…
x
Reference in New Issue
Block a user