Posted By

cobra90nj on 04/30/09


Tagged

project Euler seven


Versions (?)

Project euler 7


 / Published in: PHP
 

  1. <?php
  2.  
  3. /**
  4. cobra90nj@cobra90nj:~/Scrivania$ time php ll.php
  5. 104760
  6. real 0m46.014s
  7. user 0m42.267s
  8. sys 0m0.056s
  9. */
  10.  
  11. function isPrime($num)
  12. {
  13. if ($num == 2 || $num == 3) { return 1; }
  14. if (!($num%2) || $num<1) { return 0; }
  15.  
  16. for ($n = 3; $n <= $num/2; $n += 2) {
  17. if (!($num%$n)) {
  18. return 0;
  19. }
  20. }
  21.  
  22. return 1;
  23. }
  24.  
  25. $obb = 1;
  26. $ispr = 4;
  27.  
  28. while ($obb < 10001) {
  29.  
  30. if (isPrime($ispr)) {
  31. $obb++;
  32. $ispr++;
  33. }
  34. else {
  35. $ispr++;
  36. }
  37. }
  38.  
  39. echo $ispr;

Report this snippet  

You need to login to post a comment.