goldbach_conjecture_2n_prime.pl 926 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 04 September 2015
  5. # Website: https://github.com/trizen
  6. # Goldbach conjecture as the sum of two primes
  7. # with one prime being in the range of (n, 2n)
  8. # Proving that always there is a prime number between
  9. # n and 2n which can be added with a smaller prime
  10. # such as the sum is 2n, would prove the conjecture.
  11. use 5.010;
  12. use strict;
  13. use warnings;
  14. use List::Util qw(sum);
  15. use ntheory qw(random_prime is_prime);
  16. my $max = 10000;
  17. my @counts;
  18. foreach my $i (2 .. $max) {
  19. my $n = 2 * $i;
  20. my $count = 0;
  21. while (1) {
  22. ++$count;
  23. last if is_prime($n - random_prime($i, $n));
  24. }
  25. push @counts, $count;
  26. }
  27. say "Expected: ", log($max) / 2;
  28. say "Observed: ", sum(@counts) / @counts;
  29. __END__
  30. --------------------------
  31. Example for max=1000000
  32. --------------------------
  33. Expected: 6.90775527898214
  34. Observed: 6.66289466289466