146 Investigating a Prime Pattern.pl 605 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # https://projecteuler.net/problem=146
  6. # Runtime: 14.922s
  7. use 5.010;
  8. use strict;
  9. use integer;
  10. use ntheory qw(is_prime next_prime);
  11. my $sum = 0;
  12. for (my $i = 10; $i < 150_000_000; $i += 10) {
  13. my $x = $i*$i;
  14. if ( is_prime($x + 1)
  15. and next_prime($x + 1) == $x + 3
  16. and next_prime($x + 3) == $x + 7
  17. and next_prime($x + 7) == $x + 9
  18. and next_prime($x + 9) == $x + 13
  19. and next_prime($x + 13) == $x + 27) {
  20. $sum += $i;
  21. }
  22. }
  23. say $sum;