Created
November 19, 2013 06:59
-
-
Save pmgupte/7541348 to your computer and use it in GitHub Desktop.
PHP solution to Project Euler problem # 3.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 3: Largest prime factor | |
* | |
* The prime factors of 13195 are 5, 7, 13 and 29. | |
* What is the largest prime factor of the number 600851475143 ? | |
*/ | |
$n = 600851475143; | |
$factor = 2; | |
$lastFactor = 1; | |
while ($n > 1) { | |
if ($n % $factor == 0) { | |
$lastFactor = $factor; | |
$n = $n / $factor; | |
while ($n % $factor == 0) { | |
$n = $n/$factor; | |
} | |
} | |
$factor++; | |
} | |
echo "largest factor = $lastFactor\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment