fibonacci_k-th_order_odd_primes_indices.pl 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu and M. F. Hasler
  3. # Date: 20 April 2018
  4. # Edit: 23 April 2018
  5. # https://github.com/trizen
  6. # Find the first index of the odd prime number in the nth-order Fibonacci sequence.
  7. # See also:
  8. # https://oeis.org/A302990
  9. use 5.020;
  10. use strict;
  11. use warnings;
  12. use Math::GMPz;
  13. my $ONE = Math::GMPz->new(1);
  14. use ntheory qw(is_prob_prime);
  15. use experimental qw(signatures);
  16. sub nth_order_prime_fibonacci_index ($n = 2, $min = 0) {
  17. # Algorithm after M. F. Hasler from https://oeis.org/A302990
  18. my @a = map { $_ < $n ? ($ONE << $_) : $ONE } 1 .. ($n + 1);
  19. for (my $i = 2 * ($n += 1) - 2 ; ; ++$i) {
  20. my $t = $i % $n;
  21. $a[$t] = ($a[$t-1] << 1) - $a[$t];
  22. if ($i >= $min and Math::GMPz::Rmpz_odd_p($a[$t])) {
  23. #say "Testing: $i";
  24. if (is_prob_prime($a[$t])) {
  25. #say "\nFound: $t -> $i\n";
  26. return $i;
  27. }
  28. }
  29. }
  30. }
  31. # a(33) = 94246
  32. # a(36) = ?
  33. # a(37) = 758
  34. # a(38) = ?
  35. # a(39) = ?
  36. # a(36) > 170050 (M. F. Hasler)
  37. # a(38) > 40092
  38. # a(41) > 142000 (M. F. Hasler)
  39. # a(100) > 48076
  40. # Example for computing the terms a(2)-a(26):
  41. say join ", ", map{ nth_order_prime_fibonacci_index($_) } 2..26;
  42. # Searching for a(36)
  43. # say nth_order_prime_fibonacci_index(36, 170051);