Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. <?php
  2. //Prime Number calculato (just for fun)
  3. //DATABASE DETAILS
  4. $dbhost = 'localhost'; //host
  5. $dbname = "prime"; //name of database
  6. $dbusername = "root"; //username
  7. $dbpassword = "dbpass"; //pass
  8. $db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8",$dbusername,$dbpassword,array(PDO::ATTR_EMULATE_PREPARES => false,PDO::ATTR_ERRMODE =>PDO::ERRMODE_EXCEPTION));
  9.  
  10. ini_set('memory_limit', '150M');
  11. error_reporting(0);
  12. ignore_user_abort(true);
  13. set_time_limit(0);
  14. $startnum = 834149;
  15. $maxnum = 9999999999999999999999999;
  16. $file = '/prime.txt';
  17. $data = "NEW TEST \n starting number: ".$startnum."\n Ending number: ".$maxnum."\n";
  18. file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
  19. echo "starting";
  20.  
  21. for($i = $startnum; $i < $maxnum; ++$i) {
  22. if (!file_exists("/stop")) {
  23. $a = 0;
  24. while ($a < sqrt($i) + 1) {
  25. if ($a == 0) { //continue
  26. $a++;
  27. }
  28. elseif ($a == 1) { //continue
  29. $a++;
  30. }
  31. elseif ($a == $i) { //continue
  32. $a++;
  33. }
  34. else {
  35. $testnum = $i / $a;
  36. if ( strpos( $testnum, "." ) !== false ) {
  37.  
  38.  
  39. //Answer is decimal, continue
  40. if ($a > sqrt($i)) {
  41. $a++;
  42. $data = "\n PRIME NUMBER FOUND: ".$i." \n Allocated Memory: ".(memory_get_peak_usage(true)/1024/1024)." MiB \n Memory Usage: ".(memory_get_peak_usage(false)/1024/1024)." MiB\n\n";
  43. file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
  44. $insert = $db->prepare("INSERT INTO `prime`(`number`) VALUES (:n)");
  45. $insert->bindParam(':n', $i);
  46. $insert->execute();
  47. usleep(2000);
  48. break;
  49.  
  50. }
  51. $a++;
  52. }
  53.  
  54. else {
  55. $a++;
  56. $data = "\n Not prime: ".$i;
  57. file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
  58. break; //Not decimal, so number isn't prime
  59. }
  60.  
  61. }
  62. }
  63.  
  64. }
  65. else {
  66. file_put_contents($file, "\nSTOPPING TEST\n", FILE_APPEND | LOCK_EX);
  67. exit();
  68. }
  69. }
  70.  
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement