fork(1) download
  1. <?php
  2.  
  3. /**
  4.  * Project Euler Problem 3: Largest prime factor
  5.  *
  6.  * The prime factors of 13195 are 5, 7, 13 and 29.
  7.  *
  8.  * What is the largest prime factor of the number 600851475143 ?
  9.  *
  10.  * Problem: https://p...content-available-to-author-only...r.net/problem=3
  11.  * Solution: https://g...content-available-to-author-only...b.com/potherca-blog/ProjectEuler/blob/master/src/PHP/Solutions/Problem003.php
  12.  * Live code: https://i...content-available-to-author-only...e.com/O2tMUJ
  13.  */
  14. namespace Potherca\ProjectEuler\Solutions\Problem003
  15. {
  16. use Potherca\ProjectEuler\Calculators\PrimeFactorCalculator as Calculator;
  17.  
  18. $number = 600851475143;
  19.  
  20. $solution = (new Calculator())->getLargestPrimeFactor($number);
  21.  
  22. echo $solution;
  23. }
  24.  
  25. namespace Potherca\ProjectEuler\Calculators
  26. {
  27. class PrimeFactorCalculator
  28. {
  29. final public function getPrimeFactors(int $subject): array
  30. {
  31. $factors = [];
  32.  
  33. $currentPrime = 2;
  34.  
  35. $limit = gmp_strval(gmp_sqrt($subject));
  36.  
  37. while ($currentPrime < $limit) {
  38. if ($subject % $currentPrime === 0) {
  39. $factors[] = $currentPrime;
  40. }
  41. $currentPrime = gmp_strval(gmp_nextprime($currentPrime));
  42. }
  43.  
  44. return $factors;
  45. }
  46.  
  47. final public function getLargestPrimeFactor(int $subject): int
  48. {
  49. return end($this->getPrimeFactors($subject));
  50. }
  51. }
  52. }
  53.  
Success #stdin #stdout #stderr 0.8s 24616KB
stdin
Standard input is empty
stdout
6857
stderr
PHP Notice:  Only variables should be passed by reference in /home/LxTty9/prog.php on line 49