Add Project Euler problem 3

This commit is contained in:
Nima HeydariNasab 2021-10-22 20:57:24 +03:30
parent e0e0eac014
commit 4b41b472e0
3 changed files with 37 additions and 0 deletions

View File

@ -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
View 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;
}

View File

@ -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());