continued_fractions_prime_constant.pl 509 B

12345678910111213141516171819202122232425262728
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 10 May 2016
  5. # Website: https://github.com/trizen
  6. # Continued fraction constant for primes.
  7. use 5.010;
  8. use strict;
  9. use ntheory qw(nth_prime);
  10. sub prime_constant {
  11. my ($i, $limit) = @_;
  12. my $p = nth_prime($i);
  13. $limit > 0 ? ($p / ($p + prime_constant($i + 1, $limit - 1))) : 0;
  14. }
  15. my $pc = prime_constant(1, 10000);
  16. say $pc;
  17. say 1 / (1 + $pc); # "1" is considered prime here
  18. __END__
  19. 0.71961651193526
  20. 0.581525004592215