PERL   18

primes.pl

Guest on 7th May 2021 04:42:06 PM

  1. #    This program is free software: you can redistribute it and/or modify
  2. #    it under the terms of the GNU General Public License as published by
  3. #    the Free Software Foundation, either version 3 of the License, or
  4. #    (at your option) any later version.
  5.  
  6. #    This program is distributed in the hope that it will be useful,
  7. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. #    GNU General Public License for more details.
  10.  
  11. #    You should have received a copy of the GNU General Public License
  12. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  13.  
  14.  
  15. #!/usr/bin/perl
  16.  
  17. use Test::More;
  18.  
  19. # For the sake of mathamathical ease, we'll start with 3 since 0, 1 and 2 are odd numbers for primes.
  20. $start = 3;
  21. $end = 46585431447987332144685;
  22.  
  23. print("Starting search for prime numbers at: ", $start, "\nPrime numbers found:\n");
  24.  
  25. while (1) {
  26.       $dev = 2;   # This is the number we'll devide by.
  27.       while ($dev <= $start) {
  28.            
  29.             if ($dev == $start) {
  30.                   print($start, "\n");
  31.                   $dev++;
  32.             } else {
  33.                   if  (is_int($start / $dev)) {
  34.                         $dev = 2;
  35.                         $start = $start + 2;
  36.                   } else {
  37.                         $dev++;
  38.                   }
  39.             }
  40.       }
  41. $start = $start + 2;
  42. if($start > $end) {
  43.       quit();
  44. }
  45. }
  46.  
  47.  
  48. sub is_int {
  49.     return ( $_[0] eq int( $_[0] ) && $_[0] > 0 );
  50. }

Raw Paste


Login or Register to edit or fork this paste. It's free.