-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-3.php
More file actions
48 lines (40 loc) · 1.07 KB
/
Copy pathproblem-3.php
File metadata and controls
48 lines (40 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
require_once 'generators/prime.php';
/**
* Problem 3 :
* ===========
*
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* What is the largest prime factor of the number 600851475143 ?
*
* @see https://projecteuler.net/problem=3
*
* @param int $number
*
* @return int
*/
function problem3(int $number = 600851475143): int
{
/** @var float $quotient We need to keep track of the quotient between iterations. */
$quotient = $number;
// Default value.
$biggestPrime = 2;
while (true) {
$limit = sqrt($quotient);
foreach (primeGenerator(null, $limit) as $prime) {
// End when the quotient is a prime.
if ($quotient === $prime) {
$biggestPrime = max($biggestPrime, $prime);
break 2;
}
// Search for a prime divisor.
if ($quotient % $prime === 0) {
$quotient = $quotient / $prime;
$biggestPrime = max($biggestPrime, $prime);
}
}
}
return $biggestPrime;
}