Advertisement
guedes_acp

First Palindrome Prime after time()

Apr 27th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.60 KB | None | 0 0
  1. #!/usr/env/perl
  2.  
  3. #############################
  4. #   guedes.acp              #
  5. #############################
  6.  
  7. use warnings;
  8. use strict;
  9. use feature 'say';
  10. use Math::Prime::Util ':all';
  11.  
  12. sub main{
  13.     my $time = time();
  14.     say "Current time: $time";
  15.  
  16.     while(!is_prime($time)){
  17.         $time = palindrome(next_prime($time));
  18.     }
  19.     say $time;
  20. }  
  21.  
  22. sub palindrome {
  23.     my $n = shift;
  24.     my $r;
  25.     if($n == reverse $n){$r = $n}
  26.     else{
  27.         my @d = split(//, $n);
  28.         my $s = int $n * (0.1 ** (int scalar @d * 0.5));
  29.         $s++;
  30.         my $s1 = (scalar @d % 2 != 0) ? int $s * 0.1 : $s;
  31.         $r = $s . reverse $s1;
  32.     }
  33.     return $r;
  34. }
  35.  
  36. main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement