-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.php
More file actions
41 lines (31 loc) · 690 Bytes
/
3.php
File metadata and controls
41 lines (31 loc) · 690 Bytes
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
<?php
/**
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* What is the largest prime factor of the number 600851475143 ?
*/
$start = microtime(true);
// Factorizo usando división por tentativa
// http://es.wikipedia.org/wiki/Divisi%C3%B3n_por_tentativa
function factores($n) {
$p = 2;
$factores = array();
while ($p * $p <= $n) {
if (fmod($n, $p) == 0) {
$factores[] = $p;
$n = $n / $p;
} else {
$p++;
}
}
if ($n != 1) {
$factores[] = $n;
}
return $factores;
}
//$n = 13195;
$n = 600851475143;
$factores = factores($n);
rsort($factores);
echo reset($factores) . "\n";
printf("Resultado obtenido en %.4f segundos\n", microtime(true) - $start);